Use of datagrid itemdatabase

xiaoxiao2021-03-06  87

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.

. 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 folding c # code: protected datable dt = new data ("itemprice"); dt.columns.add ("item", typeof (string); dt.columns .Add ("Price", TypeOf (Float); DATAROW DR; DR = DT.NEWROW (); DR ["" i "] =" 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 (); after binding to the Datgrid, The Display Looks Like this:

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 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 .Bo (} else if (E.Item.i) Temtype == ListiteMTYPE.FOOTER) {E.Item.cells [0] .Text = "total"; E.Item.cells [1] .text = Total.toTString ();}} The new datagrid Looks like this when you rendered On The Page:

itempricepen3.5camera10.8coffee maker40.2Total54.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 ItemDatabase to Access The Data Item Before It Is Displayed in The Browser.

转载请注明原文地址:https://www.9cbs.com/read-121445.html

New Post(0)