Display total in datagrid.

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)
{
  if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
  {
    CalcTotal( e.Item.Cells[1].Text );
    e.Item.Cells[1].Text = string.Format("{0:c}" , Convert.ToDouble (e.Item.Cells[1].Text));
  }
  else if (e.Item.ItemType == ListItemType.Footer )
  {
    e.Item.Cells[0].Text="Total" ;
    e.Item.Cells[1].Text = string.Format("{0:c}" , runningTotal);
  }
}

from other documents: (for VB)

PROTECTED  As  sender myDataGrid_ItemDataBound(ByVal  Sub  System.Object,
ByVal e 
As  DataGridItemEventArgs) Handles myDataGrid.ItemDataBound        
Select   Case  e.Item.ItemType
            
Case  ListItemType.Item, ListItemType.AlternatingItem
                interimTotal 
+=  CType(e.Item.Cells( 3 ).Text,  Double )
            
Case  ListItemType.Footer
                e.Item.Cells(
2 ).Text  =   " TOTAL:  "
                e.Item.Cells(
3 ).Text  =  interimTotal.ToString
        
End   Select
End Sub

 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();
               }
 
}

 

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章