现在的位置: 首页 > 综合 > 正文

Data ItemDataBound event

2012年09月10日 ⁄ 综合 ⁄ 共 3386字 ⁄ 字号 评论关闭
Practical use of the DataGrid's ItemDataBound event.

http://www.codeproject.com/aspnet/ItemCreated.asp

Advanced DataGrid Formating with ItemDataBound
http://www.codeproject.com/aspnet/PrettyDatagrids.asp#xx1115560xx

use of datagrid itemdatabound event (c#)
by ranjit thayyil
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:
  itemprice
pen3.5
camera10.8
coffee maker40.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:
itemprice
pen3.5
camera10.8
coffee maker40.2
total54.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.
reference:
msdn library - january 2004

【上篇】
【下篇】

抱歉!评论已关闭.