65.9K
CodeProject is changing. Read more.
Home

ASP.NET DataGrid UpDown Column

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.17/5 (10 votes)

Sep 18, 2004

2 min read

viewsIcon

58571

The ASP.NET DataGrid UpDownColumn represents a DataGrid column up-down control that displays a list's selected values.

Introduction

The ASP DataGrid UpDownColumn represents a DataGrid column up-down control that displays a list's selected values. It allows you to show a UpDown control in a DataGrid cell. The control displays in a DataGrid cell a single value that is selected from a collection by clicking the up or down buttons of the control. The user can also enter text in the control if the string typed in matched an item in the collection to be accepted.

To create a collection of objects to display in the UpDown control, you can add or remove the items individually by using the Add and Remove methods.

You can identify a DataSource parameter for the control. The DataSource is a source for DataGrid UpDown column values list as System.Data.DataTable or System.Data.DataRowView or System.Collections.ArrayList. By defining DisplayMember parameter, you can specify a field to display in UpDown column, as String (name of table column).

Use the ASP DataGrid UpDownColumn to create a single selection up-down list control in each cell of ASP.NET DataGrid column. You can control the appearance of the ASP DataGrid UpDownColumn by setting the BackColor, ForeColor, BorderColor, BorderStyle, and other properties.

The ASP DataGrid UpDownColumn supports data binding. To bind the control to a data source, create a data source, such as a System.Collections.ArrayList object, that contains the items to display in the column. Then, use the DataSource property to bind the data source to the ASP DataGrid UpDownColumn. You can use a DataTable object as a data source for your UpDownColumn. For this kind of a data source, you need to specify ValueMember and DisplayMember properties.

By implementing in your code an interface with the DataGridCommands class, you will be able to save all updates into DataGrid's data source data object. By using UpdateDataSource method of the class, you can easily save all updates that your user made in ASP DataGrid UpDown column into bound data source.

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);