Friday, June 4, 2010

ItemDataBound the last opportunity to access the data item

The ItemDataBound event is raised after an item is data bound to the DataGrid control. This event gives you with the last opportunity to access the data item before it appears on the client. After this event is raised, the data item is null and is no longer available.



For each item that is data bound, you must check the ItemType property. If ItemType is of type Item or AlternatingItem, you receive the value from the last cell of the item, which contains the SaleAmount value. In this sample, you add this value to the running summary variable. When the ItemType is Footer, you receive the total from all of the rows. Therefore, you assign the value of the summary variable to the text value of the last cell.




Code behind:



protected void dg_ItemDataBound(object sender, DataGridItemEventArgs e)
{
// Use the ItemDataBound event to customize the DataGrid control.
// The ItemDataBound event allows you to access the data before
// the item is displayed in the control. In this example, the
// ItemDataBound event is used to format the items in the
// CurrencyColumn in currency format.
if((e.Item.ItemType == ListItemType.Item) ||
(e.Item.ItemType == ListItemType.AlternatingItem))
{

// Retrieve the text of the CurrencyColumn from the DataGridItem
// and convert the value to a Double.
Double Price = Convert.ToDouble(e.Item.Cells[2].Text);

// Format the value as currency and redisplay it in the DataGrid.
e.Item.Cells[2].Text = Price.ToString("c");

DropDownList dd = (DropDownList)e.Item.FindControl("dd_control");
Label lb = (Label)e.Item.FindControl("lb_control");

// Do somenting here...
}
else
{
// It is a header or footer
// Do something here......
}

}



Source: http://support.microsoft.com/kb/313154

No comments:

Post a Comment