We have nemerous amount of events in datagrid.Here we are going to discussing about ItemCreated
and ItemDataBound Events.
ItemCreated event The ItemCreated event is fired to let applications know that a new item has been created.
The event data includes information about the new item. Once the event handler terminates,
the item is added to the HTML table being generated.
ItemDataBound event The ItemDataBound event is fired, and applications can read the actual values
bound to normal and alternating rows.
<asp:datagrid runat=”server” id=”grid”
onitemcreated=”ItemCreated”
onitemdatabound=”ItemDataBound”
ondatabinding=”TraceDataBinding” />
for ex,
void ItemCreated(Object sender, DataGridItemEventArgs e) {
ListItemType elemType = e.Item.ItemType;
if (elemType == ListItemType.Footer)
{
// Remove all the cells but one
TableCellCollection tcc = e.Item.Cells;
int nTotalCols = tcc.Count;
for (int i=0; i<nTotalCols-1; i++)
e.Item.Cells.RemoveAt(0);
// Only 1 cell left at this time …
TableCell c = e.Item.Cells[0];
c.ColumnSpan = nTotalCols;
// Set any text to be displayed
c.Text = “…”;
}
}
void ItemDataBound(object sender, DataGridItemEventArgs e) {
ListItemType itemType = e.Item.ItemType;
if (itemType == ListItemType.Item ||
itemType == ListItemType.AlternatingItem)
{
DataRowView drv = (DataRowView) e.Item.DataItem;
// Extract the first control in the first cell of the row
WebControl webCtl = (WebControl) e.Item.Cells[0].Controls[0];
WebCtl.ToolTip = drv["unitsinstock"] + ” units in stock” ;
}
}
Hope this helps,
Happy Coding,
Comments are closed.