Wednesday, January 2, 2008

ASP .NET DataGrid DropDown Column - DotNet Zone - DNzone.COM

ASP .NET DataGrid DropDown Column - DotNet Zone - DNzone.COM

Use the ASP DataGrid DropDownColumn to create a single selection drop-down list control in each cell of DataGrid column. You can control the appearance of the ASP DataGrid DropDownColumn by setting the BackColor, ForeColor, BorderColor, BorderStyle, and other properties. The control displays in a datagrid cell a single value that is selected from a collection by clicking the button of the control. Your user can enter text in the control if the string typed in matched an item in the collection to be accepted.

The ASP DataGrid DropDownColumn 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 DropDownColumn. You can use a System.Data.DataTable or System.Data.DataRowView or System.Collections.ArrayList object as a datasource for your DropDownColumn. For this kind of a datasource 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 datasource data object. By using UpdateDataSource method of the class you can easily save all updates that your user made in ASP DataGrid DropDown Column into bound datasource.

Syntax


The ASP DataGrid DropDown Column class has the following useful properties:

DataSource - a source for a DataGrid DropDown column values list as System.Data.DataTable or System.Data.DataRowView or System.Collections.ArrayList

DisplayMember - field to display in a column cell as String (name of a DataTable column)

ValueMember - field with values, which binds to drop-down control as String (name of table column). Specify the contents of the ValueMember property in cases where you bind data.

VB .NET
' Define DropDown variable as an object of DropDownColumn class
' and assign it to the DataGrid1 'State' column.
Dim DropDown As DropDownColumn = DataGrid1.Columns(5)
' Assign DataSource property for DropDown object as tblStates
' data table where states’s names are stored.
DropDown.DataSource = tblStates
' Specify DisplayMember as "Name" field of tblStates table
DropDown.DisplayMember = "Name"
' Specify ValueMember as "State" field of tblStates table
DropDown.ValueMember = "State"
' Identify "State" column's foreground color
DropDown.ForeColor = Color.DarkMagenta
' Identify "State" column characters' font and size
DropDown.Font_Name = "Tahoma"
DropDown.Font_Size = FontUnit.Point(8)
' Adjust 'State' column's width
DropDown.Width = Unit.Point(60)

C#
// Define DropDown variable as an object of DropDownColumn class
// and assign it to the DataGrid1 'State' column.
DropDownColumn DropDown = (DropDownColumn)DataGrid1.Columns[5];
// Assign DataSource property for DropDown object as tblStates
// data table where states’s names are stored.
DropDown.DataSource = tblStates;
// Specify DisplayMember as "Name" field of tblStates table
DropDown.DisplayMember = "Name";
// Specify ValueMember as "State" field of tblStates table
DropDown.ValueMember = "State";
// Identify "State" column's foreground color
DropDown.ForeColor = Color.DarkMagenta;
// Identify "State" column characters' font and size
DropDown.Font_Name = "Tahoma";
DropDown.Font_Size = FontUnit.Point(8);
// Adjust 'State' column's width
DropDown.Width = Unit.Point(60);

No comments: