
6.01.2004
Changing DataGrid contents on the Fly
(updated 6/1/04 now that I'm using this for real work :)
Quick summary:
ItemCreated event looks like it's when the cells (and any controls in those cells) are created. Can add new cells, with their proper content here.
If you want access to the data that is *going to be in that cell*, wait until you get the DataBound event. Here, you have access to the DataItem property that will give you the IDataRecord (if reader-based datasource) or DataRowView (if DataSet-based datasource) object. (see below)
-------
I often try to pull this off, but can't quite figure out where each element is bound, created, etc. Haven't tried this, but found in a mag, handling the following event to add a "line number".
(updated) if want/need access to the data source text to "adjust" the cell contents of a cell coming from the data source, do this in the DataBound event below. Here, we just use the DataSetIndex property to make up a line number:
In another example, they actually did more work in another cell, something like this to confirm a delete button click before action:
Saw this at The Code Project -- add a control to the collection? Use this in the ItemCreated event.
In the ItemCreated event, cast the DataItem property to what data "reader" you are using... (in this case, DataSet and it's sub parts)
Eeesh, just spent an embarrasing amount of time trying to figure out how to look at the data from a OleDbDataReader instead of the DataSet based binding above.
Update 6/1/04 with some other code you can use in these events:
In either event, use ItemType property to determine if this row is a Header, Footer, item, etc.
In the ItemCreated event, use the Cells and Controls collections to access the cells and contents of items in this row.
Someday I'll figure out the difference between ItemCreated and DataBound events on the DataGrid.
Updated 6/1/04: Think I've (sort of) figured this out. See top of this post for updated thoughts.
Quick summary:
ItemCreated event looks like it's when the cells (and any controls in those cells) are created. Can add new cells, with their proper content here.
If you want access to the data that is *going to be in that cell*, wait until you get the DataBound event. Here, you have access to the DataItem property that will give you the IDataRecord (if reader-based datasource) or DataRowView (if DataSet-based datasource) object. (see below)
-------
I often try to pull this off, but can't quite figure out where each element is bound, created, etc. Haven't tried this, but found in a mag, handling the following event to add a "line number".
(updated) if want/need access to the data source text to "adjust" the cell contents of a cell coming from the data source, do this in the DataBound event below. Here, we just use the DataSetIndex property to make up a line number:
public void OnItemCreated( Object sender, DataGridItemEventArgs e )
{
if ( i.Item.ItemType == ListItemType.Item ||
i.Item.ItemType == ListItemType.AlternatingItem )
{
eItem.Cells[1].Text = (e.Item.DataSetIndex+1).ToString();
}
}
In another example, they actually did more work in another cell, something like this to confirm a delete button click before action:
WebControl btn = (WebControl)e.Item.Cells[0];
btn.Attributes.Add( "onclick", "return confirm( \"Delete this row?\"); " );
Saw this at The Code Project -- add a control to the collection? Use this in the ItemCreated event.
CheckBox cb = new CheckBox();
e.Item.Controls[i+dbTableColumnOffset].Controls.Add(cb);
e.Item.Controls[i+dbTableColumnOffset].Controls.RemoveAt(0);
In the ItemCreated event, cast the DataItem property to what data "reader" you are using... (in this case, DataSet and it's sub parts)
cb.ID = ((DataTable)((DataView)((DataRowView)e.Item.
DataItem).DataView).Table).Columns[i].
ColumnName.ToString();
Eeesh, just spent an embarrasing amount of time trying to figure out how to look at the data from a OleDbDataReader instead of the DataSet based binding above.
int count = ((IDataRecord)e.Item.DataItem).FieldCount;
String s = ((IDataRecord)e.Item.DataItem).GetString(0);
int ord = ((IDataRecord)e.Item.DataItem).GetOrdinal("machine_id");
String dtype = ((IDataRecord)e.Item.DataItem).GetDataTypeName(7);
... etc ...
Update 6/1/04 with some other code you can use in these events:
In either event, use ItemType property to determine if this row is a Header, Footer, item, etc.
if ( e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem )
In the ItemCreated event, use the Cells and Controls collections to access the cells and contents of items in this row.
e.Item.Cells
e.Item.Controls
Someday I'll figure out the difference between ItemCreated and DataBound events on the DataGrid.
Updated 6/1/04: Think I've (sort of) figured this out. See top of this post for updated thoughts.