Use of DataGrid ItemDataBound Event (C#)

原创 2004年08月31日 20:04:00

A DataGrid ItemDataBound event is raised each time an item (row) is data bound to the DataGrid. Once this event is raised, an argument of type DataGridItemEventArgs is passed to the event handling method and the data relevant to this event is available. This data is is no longer available once your application exits the event handling method. The eventhandler for ItemDataBound is OnItemDataBound.We can specify the method that should handle the event which takes the event source object (in our case the datagrid) and the DataGridItemEventArgs as arguments.

This can be explained easily with the help of an example. Consider the case where you want to calculate the total of a column in a datagrid.

First, let us create a data table and add rows to it with the following C# code:

protected DataTable dt = new DataTable("itemprice");
dt.Columns.Add("item",typeof (String));
dt.Columns.Add("price",typeof(float));

DataRow dr; dr = dt.NewRow();

dr["item"]="pen";
dr["price"]=3.5;
dt.Rows.Add(dr);

dr = dt.NewRow();
dr["item"]="camera";
dr["price"]=10.8;
dt.Rows.Add(dr);

dr = dt.NewRow();
dr["item"]="coffee maker";
dr["price"]=40.2;
dt.Rows.Add(dr);

dg.DataSource=dt.DefaultView;
dg.DataBind();< /FONT >


 After binding to the datgrid, the display looks like this:

 
item price
pen 3.5
camera 10.8
coffee maker 40.2
   

 

Since we are using ItemDataBound event to to total up the price column in the data grid, we have to specify on the web form (.aspx) page, the method that would handle the ItemDataBound event (Event wiring). This is acheived by mentioning within the datagrid control tag that on the occurence of the event

(OnItemDataBound), itemDataBound method should be called.

<asp:datagrid id="dg" runat= "server"< /FONT> OnItemDataBound="itemDataBound" ShowFooter="True">

On the code behind file (.aspx.cs), we define the 'itemDataBound' method for handling the ItemDataBound Event.

         float total = 0;           protected void itemDataBound(object sender, DataGridItemEventArgs e)         {                   if  (e.Item.ItemType!=ListItemType.Header && e.Item.ItemType!=ListItemType.Footer)                  {
                    &nbs p; total +=
float.
Parse(e.Item.Cells[1].Text);
                      e.Item.ForeColor = System.Drawing.Color.Blue;
                 }
                 else if
(e.Item.ItemType == ListItemType.Footer)
                {
                     e.Item.Cells[0].Text = "Total";
                     e.Item.Cells[1].Text = total.ToString();
                }
          }

The new datagrid looks like this when rendered on the page:

item price
pen 3.5
camera 10.8
coffee maker 40.2
Total 54.5

How does this code work? The itemDataBound method is called  as each row(item) is bound to the datagrid.
Within the method, we check if the current row is a header or a footer row.To do this we use ListItemType enumeration.
For now, all we need to know is that ListItemType enumeration contains different types of items(header, footer,item, alternating item etc. ) that can be included in a list control (in our case, a datagrid). Therefore, if we want to check if the item bound is a datagrid footer, a simple if statement like this would do the trick:

               if (e.Item.ItemType == ListItemType.Footer) { // your code } 

In our method,  we add the value in the price column to the total if the datagrid item is not a header or footer. Finally, when the datagrid footer is encountered, we display our total on the footer row of the datagrid.This is possible because ItemDataBound event is the last oppurtunity to access the data item before it is displayed in the browser.

Repeater数据控件的两个重要事件ItemDataBound 和 ItemCommand

1   ItemDataBound:数据绑定的时候(正在进行时)发生。 2   ItemCommand :用来响应Item模板中的控件的事件。 如下代码 aspx代码: ...
  • zhifeiya
  • zhifeiya
  • 2013-06-04 22:05:51
  • 5908

DataList事件ItemDataBound与DataBind()

      学习控件,往往需要知道控件所拥有的事件,比如说DataList控件吧,以前没有用过,但凭着对其它控件(如:Dropdownlist)的认知,想当然就知道只要给数据源绑定数据就OK了。那样控...
  • csdbfans
  • csdbfans
  • 2010-07-18 10:35:00
  • 2914

DataGrid的<em>ItemC</em>reated和<em>ItemDataBound</em>以及合计平均行

上传者: ouylvr 时间: 2017-08-<em>23</em> 综合评分: 5 积分/C币:3 WPF DataGrid...DataGrid的<em>ItemC</em>reated和<em>ItemDataBound</em>以及合计平均行 3积分 立即下载 ...
  • 2018年03月31日 00:00

DataGrid的ItemDataBound用法实例

鉴于在本版收到好多asp.net初学者因为不了解ItemDataBound事件的用法而提出问题,特写此实例教程,以飨众友。实例:现要把如下的数据集(在查询管理器的查出来的结果集)绑定到DataGrid...
  • redrainy
  • redrainy
  • 2007-02-25 14:16:00
  • 1878

DataGrid的ItemCreated和ItemDataBound事件

DataGrid的ItemCreated和ItemDataBound事件 www.xyhhxx.com  发布者: seo  时间: 2005-11-29DataGrid.ItemDataBound当...
  • CJamie
  • CJamie
  • 2006-04-24 15:40:00
  • 1137

Use of DataGrid ItemDataBound Event (C#)

A DataGrid ItemDataBound event is raised each time an item (row) is data bound to the DataGrid. Once...
  • derny
  • derny
  • 2004-08-31 20:04:00
  • 1185

CancelCommand、EditCommand、UpdateCommand、ItemDataBound事件的设置

//取消  private void dgrd_djsd_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandE...
  • fmwind
  • fmwind
  • 2006-01-23 16:42:00
  • 1326

itemdatabound小结

在DATAGRID中,如果要对某些记录进行格式化或者修饰,用到itemdatabound事件比较方便,比如,要显示某人的存款金额已经少于某个数额了,要用红色来显示等。itemdatabound事件发生...
  • mdot
  • mdot
  • 2004-10-28 16:17:00
  • 875

Use the ItemDataBound event to customize the DataGrid control.

  void Item_Bound(Object sender, DataGridItemEventArgs e)       {             if((e.Item.ItemType ==...
  • littlekeen
  • littlekeen
  • 2005-08-09 14:38:00
  • 612

DataGrid的ItemCreated和ItemDataBound事件使用小技巧!

DataGrid的事件中,ItemCreated和ItemDataBound是尤为重要并且经常要用到的两个,其中的ItemCreated事件在DataGrid创建表格行时触发,而ItemDataBou...
  • shipfi
  • shipfi
  • 2004-12-22 11:19:00
  • 3568
收藏助手
不良信息举报
您举报文章:Use of DataGrid ItemDataBound Event (C#)
举报原因:
原因补充:

(最多只允许输入30个字)