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:
Since we are using ItemDataBound event 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:
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
If you have any questions or comments, please feel free to contact me at ranjiranji@yahoo.com
About the Author:
Ranjit Thayyil did his masters in computer science from Texas A&M - Commerce and is a Microsoft Certified Professional in ASP.NET (C#). He presently works as a programmer for Marlabs Inc., Bethlehem, PA.
Read this Newsletter at: http://www.cprogrammingtrends.com/2004/0721.html |
|
| From the Forum: |
| Relational Tables in MySQL |
| I have three MySQL tables in a database and they are supposed to be of a one-to-one relationship, such that each record in each table is unique, and each line in all three tables are for the same record. i.e. a record on table 1 line 1 is part of a record on table 2 line 1 which are part of a record on table 3 line 1. ...
|
|