2015-12-21 71 views
1

我想使總列的總和作爲總計行數據總計。在rowdatabound事件中如何統計總數並在標籤中顯示?如何計算開往事件

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CssClass="gridview" DataKeyNames="Id" Width="633px" OnRowDataBound="GridView1_RowDataBound"> 


        <Columns> 
         <asp:BoundField DataField="Id" HeaderText="Id" ReadOnly="True" SortExpression="Id" /> 
         <asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" /> 
         <asp:BoundField DataField="Quantity" HeaderText="Quantity" SortExpression="Quantity" /> 
         <asp:BoundField DataField="UnitPrice" HeaderText="Unit Price" SortExpression="UnitPrice" /> 
         <asp:BoundField DataField="Total" HeaderText="Total" SortExpression="Total" /> 
        </Columns> 
       </asp:GridView> 

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 

      int value = Convert.ToInt32(e.Row.Cells[2].Text); 
      int value2 = Convert.ToInt32(e.Row.Cells[3].Text); 
      int total = value * value2; 
      e.Row.Cells[4].Text = Convert.ToString(total); 


    int GrandTotal = +total; 
    lblgrandTotal = GrandTotal.ToString(); 

     } 
    } 
+0

'總計'是什麼'總數'? –

+0

是的。我想統計所有總數總和。我親切地更新過 –

+0

在'protected'範圍之外取'int GrandTotal'。使其成爲不是本地的全局變量。 – Prabhat

回答

1

您可以通過總增加了變量這樣每一行做在現有GridView1_RowDataBound事件: -

int grandTotal = 0; 
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      int value = Convert.ToInt32(e.Row.Cells[2].Text); 
      int value2 = Convert.ToInt32(e.Row.Cells[3].Text); 
      int total = value * value2; 
      grandTotal += total; 
      e.Row.Cells[4].Text = Convert.ToString(total); 
      lblgrandTota.Text = grandTotal.ToString(); 
     } 
    } 

我已經放在grandTotal事件處理程序方法之外的變量,以便它不會被初始化爲每一行。

+0

標籤顯示這一點。總計:System.Web.UI.WebControls.Label我想我能做到這一點.. –

+0

@krishnamohan - 我想有_grandTotal_與您_GrandTotal_之間的混淆。糾正。 –

+1

好吧好吧我知道了...對不起...解決了 –