65.9K
CodeProject 正在变化。 阅读更多。
Home

ASP.NET DataGrid UpDown 列

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.17/5 (10投票s)

2004年9月18日

2分钟阅读

viewsIcon

58960

ASP.NET DataGrid UpDownColumn 代表一个 DataGrid 列的上下控制,用于显示列表选定的值。

引言

ASP DataGrid UpDownColumn 代表一个 DataGrid 列的上下控制,用于显示列表选定的值。它允许您在 DataGrid 单元格中显示一个 UpDown 控制。该控制在 DataGrid 单元格中显示从集合中选定的单个值,通过单击控制的向上或向下按钮来选择。用户也可以在控制中输入文本,如果输入的字符串与集合中的某个项目匹配,则该项目将被接受。

要创建要在 UpDown 控制中显示的对象的集合,您可以使用 AddRemove 方法单独添加或删除项目。

您可以为该控制指定一个 DataSource 参数。DataSourceDataGrid UpDown 列值列表的来源,可以是 System.Data.DataTableSystem.Data.DataRowViewSystem.Collections.ArrayList。通过定义 DisplayMember 参数,您可以指定要在 UpDown 列中显示的字段,作为 String(表列名)。

使用 ASP DataGrid UpDownColumn 在 ASP.NET DataGrid 列的每个单元格中创建一个单选上下列表控制。您可以通过设置 BackColorForeColorBorderColorBorderStyle 和其他属性来控制 ASP DataGrid UpDownColumn 的外观。

ASP DataGrid UpDownColumn 支持数据绑定。要将控制绑定到数据源,请创建一个数据源,例如 System.Collections.ArrayList 对象,其中包含要在列中显示的项。然后,使用 DataSource 属性将数据源绑定到 ASP DataGrid UpDownColumn。您可以使用 DataTable 对象作为 UpDownColumn 的数据源。对于这种类型的数据源,您需要指定 ValueMemberDisplayMember 属性。

通过在您的代码中实现一个与 DataGridCommands 类接口,您将能够将所有更新保存到 DataGrid 的数据源数据对象中。通过使用该类的 UpdateDataSource 方法,您可以轻松保存用户在 ASP DataGrid UpDown 列中所做的所有更新到绑定的数据源中。

VB .NET

' Define StateColumn variable as an object of UpDownColumn class
' and assign it to the DataGrid1 'State' column.
Dim StateColumn As UpDownColumn = DataGrid1.Columns(3)
Dim al As ArrayList = New ArrayList
SQL = "SELECT * FROM States"
DA = New System.Data.OleDb.OleDbDataAdapter(SQL, conStr)
Dim tblStates As DataTable = New DataTable
DA.Fill(tblStates)
Dim row As DataRow
' Populate tblStates table's records into al ArrayList
For Each row In tblStates.Rows
  al.Add(row("State"))
Next
StateColumn.DataSource = al
' Identify “State” column’s background color
StateColumn.BackColor = Color.LightGray
' Identify “State” column characters’ font and size
StateColumn.Font_Name = "Tahoma"
StateColumn.Font_Size = FontUnit.Point(8)
' Adjust ‘State’ column’s width
StateColumn.Width = Unit.Point(30)

C#

// Define StateColumn variable as an object of UpDownColumn class
// and assign it to the DataGrid1 'State' column.
UpDownColumn StateColumn = (UpDownColumn)DataGrid1.Columns[3];
ArrayList al = new ArrayList();
SQL = "SELECT * FROM States";
DA = new System.Data.OleDb.OleDbDataAdapter(SQL, conStr);
DataTable tblStates = new DataTable();
DA.Fill(tblStates);
// Populate tblStates table's records into al ArrayList
foreach (DataRow row in tblStates.Rows)
{
  al.Add(row["State"]);
}
StateColumn.DataSource = al;
// Identify “State” column’s background color
StateColumn.BackColor = Color.LightGray;
// Identify “State” column characters’ font and size
StateColumn.Font_Name = "Tahoma";
StateColumn.Font_Size = FontUnit.Point(8);
// Adjust ‘State’ column’s width
StateColumn.Width = Unit.Point(30);
© . All rights reserved.