程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> Display total in datagrid.

Display total in datagrid.

編輯:.NET實例教程

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

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved