|
|||||||||||||||||||||
|
|||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
Introduction
There are many articles that explain how to add controls to a DataGrid that can be used to represent and edit data in a format other than a common EditBox, however they all appear to require the programmer to edit the html in the .aspx page(s) rather than use code held solely in the .cs Codebehind files. I have in the past had to implement many different controls that can be used to represent and edit controls within a ITemplate implementation
Before we can implement the class that will be used as a column within our DataGrid, a class that derives from public CheckBoxItem(bool editable)
{
readOnly = (editable==true)?false:true;
}
Handling the DataBinding event
Because the CheckBox control is to represent data that is held in the DataGrid then it will need to handle the void ITemplate.InstantiateIn(Control container)
{
CheckBox box = new CheckBox();
box.DataBinding += new EventHandler(this.BindData);
container.Controls.Add(box);
}
Processing the DataBinding event
The handler for the public void BindData(object sender, EventArgs e)
{
CheckBox box = (CheckBox) sender;
DataGridItem container = (DataGridItem) box.NamingContainer;
box.Checked = false;
box.Enabled = (readOnly == true) ? false:true;
string data = ((DataRowView) container.DataItem)[dataField].ToString();
Type type = ((DataRowView)
container.DataItem).DataView.Table.Columns[dataField].DataType;
if (data.Length>0)
{
switch (type.ToString())
{
case "System.Boolean":
if ( data == "True")
{
box.Checked = true;
}
break;
default:
break;
}
}
}
CheckBox Template Column
The class that will be used as the column in our DataGrid will be derived from the public CheckBoxColumn()
{
// set the view one as readonly
viewItem = new CheckBoxItem(false);
this.ItemTemplate = viewItem as ITemplate;
// let the edit check box be editable
editItem = new CheckBoxItem(true);
this.EditItemTemplate = editItem as ITemplate;
}
Adding the Column to the DataGrid
The penultimate step is to add the column to the DataGrid itself. Because of the way we have designed the class this is simplicity itself as it can be used in place of a CheckBoxColumn checkCol = new CheckBoxColumn();
checkCol.HeaderText = "Boolean Field (Editable)";
checkCol.DataField = "Boolean";
...
DataGrid1.Columns.Add(checkCol);
Extracting the Updated State
The final step is extracting the data from the control itself when it comes to updating or inserting a row. Again we use a similar method as that normally employed however instead of a sqlUpdateCommand1.Parameters["@Boolean"].Value
= ((CheckBox)e.Item.Cells[4].Controls[0]).Checked;
AutoPostBack and receiving the CheckedChanged event
It was recently asked if it was possible to receive the
To add a column to our DataGrid that handles this sceanrio we do the following.
CheckBoxColumn checkCol2 = new CheckBoxColumn(true);
checkCol2.HeaderText = "Boolean Field (Always Editable)";
checkCol2.DataField = "Boolean";
// this is our handler for all of the CheckBoxes CheckedChanged events
checkCol2.CheckedChanged += new EventHandler(this.OnCheckChanged);
...
DataGrid2.Columns.Add(checkCol2);
Now because we need to handle the event to change our database entry we need to extract the relevent information from the DataGrid as well as extract the state of the CheckBox. The following sample shows how this could be done.
private void OnCheckChanged(object sender, EventArgs e)
{
CheckBox box = (CheckBox) sender;
DataGridItem container = (DataGridItem) box.NamingContainer;
// get our values
sqlUpdateCommand1.Parameters["@Object_id"].Value
= int.Parse(container.Cells[0].Text);
sqlUpdateCommand1.Parameters["@Boolean"].Value = box.Checked;
...
}
DocumentationThe code has been documented using the /// documentation syntax. I prefer to use NDOC which can be found at Sourceforge for my Documentation generation engine. It also handles all those tags that Microsoft created and then forgot about. Article History
29 July 2002 - Original Article Posted FeedbackYour feedback is important to me and I am always willing to make improvements. If you found this article useful don't forget to vote. </body></html> | ||||||||||||||||||||