Data ItemDataBound event


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

优质内容筛选与推荐>>
1、临终前会后悔的25件事
2、Intellij Idea Hibernate 环境搭建
3、mouseover和mouseenter的区别
4、javascript生成随机四位数
5、洛谷P2661 信息传递


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号