Display total in datagrid.
reference codes: (bellow are wrote by Doug Seven and I just put the main part on this bolg. )
( By Doug Seven
Published: 8/19/2002
Reader Level: Beginner
Rated: 3.33 by 3 member(s). )
The MyGrid_ItemDataBound event is called as each row in the data source is bound to the DataGrid. In this event handler you can work with the data in each row, in the form of a DataGridItem . For you purpose here, you will need to call CalcTotals and pass in the text from the Price column, and then format the Price column as currency for each row (Item or AlternatingItem ), and display the value of runningTotal in the Footer row.
public void MyDataGrid_ItemDataBound(object sender, DataGridItemEventArgs e) from other documents: (for VB)
part of my code: my datagrid:
<
asp:DataGrid
id
="dgReport"
runat
="server"
Width
="936px"
ShowFooter
="True"
AllowSorting
="True"
AutoGenerateColumns ="False" AllowPaging ="True" > < FooterStyle Font-Bold ="True" ></ FooterStyle > < Columns > < asp:TemplateColumn SortExpression ="NET_AMT" HeaderText ="Net Amt" > < ItemTemplate > < asp:Label id ="lblDgReportNetAmt" runat ="server" Text ='<%# DataBinder.Eval(Container.DataItem, "NET_AMT") % > '> </ asp:Label > </ ItemTemplate > </ asp:TemplateColumn > </ Columns > < PagerStyle HorizontalAlign ="Right" ></ PagerStyle > </ asp:DataGrid > part of C# code :
private
void
dgReport_ItemDataBound(
object
sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
... { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) ... { Label lblNetAmt = (Label) e.Item.Cells[ 5 ].FindControl( " lblDgReportNetAmt " ); sumNetAmt += decimal .Parse(lblNetAmt.Text); } else if (e.Item.ItemType == ListItemType.Footer ) ... { totalNetAmt = sumNetAmt + totalNetAmt; e.Item.Cells[ 0 ].Text = " Current Total " ; e.Item.Cells[ 5 ].Text = totalNetAmt.ToString(); } }
|