[Download Code]
We included two custom DataGrid columns in this article: the CheckBoxColumn and the DropDownColumn. The custom DataGrid columns included in this article were created in a similar way to the custom columns on the MetaBuilders site and in Marcie Robillard’ MSDN article. We encourage you to read Marcie Robillard’s MSDN article if you are interested in creating custom columns. In this article, we will focus on the feature and the difference in our implementation.
The CheckBoxColumn is a very simple class and has only two properties:
DataField |
The data field to bind to. |
ReadOnly |
The CheckBoxColumn always render data as CheckBox. The CheckBox is only enabled when the item is an edit item and the ReadOnly property is equal to false. |
The design of DropDownColumn is somewhat different to the design of Marcie Robillard and MetaBuilders. Both the Marcie Robillard version and the MetaBuilders version expose a DataSource property for users to populate the data source. Since DataSource is not the only way to populate a DropDownList control, we decide to expose the underlying DropDownList control instead so that users can populate it in any way of their choice. The other important difference is the optimization we made for the population of the DropDownList. Since the DropDownList is used only in edit mode, we should not populate the DropDownList until we need to. For this reason, DropDownColumn exposes the LoadDropDownList event in which we can populate the DropDownList. If we care more about the simplicity than optimization, we could still populate the DropDownList in the Page_Int or Page_Load event using the DropDownList property of the DropDownColumn class. The following is the complete list of DropDownColumn properties:
DataField |
The data field to be displayed. It is also the data field used for to populate the name/value pair used for update if the DataValueField is not supplied. This field is required. |
DataFormatString |
Used to format the data field displayed. |
DataValueField |
If supplied, the DataValueField and selected value of the DropDownList would be used to form the name/value pair for update. |
DropDownList |
Used to access the underlying DropDownList. Note that for optimization purpose, the actually DropDownList object is only instantiated when it is necessary to instantiate the DropDownList, or this property is accessed, whichever occurs first. |
ReadOnly |
If the value of this property is true, the DropDownList is disabled. |
Let’s first take a look of the implement the IDataGridColumn interface in the CheckBoxColumn:
public void GetValues(TableCell cell, Hashtable col)
{
bool val = ((CheckBox)cell.Controls[0]).Checked;
col[_dataField] = val;
}
We simply extract the value of the CheckBox control.
Now let us take a look of the implement the IDataGridColumn interface in the DropDownColumn:
public void GetValues(TableCell cell, Hashtable col)
{
string name;
object val;
if (this.DataValueField == null || this.DataValueField == "")
{
name = this.DataField;
val = _ddl.SelectedItem.Text;
}
else
{
name = this.DataValueField;
val = _ddl.SelectedItem.Value;
}
col[name] = val;
}
Note that we extract the text or value of the SelectedItem depending on whether the DataValueField property is set.
Next, we will discuss how to use an arbitrary custom DataGrid column with the DataGridHelper control.