2012-10-12 47 views
1

我綁定一個gridview,所有列是綁定列:如何diplay綁定列列的值總和尾行

<asp:BoundField DataField="PLAN_SEP" ItemStyle-Width="52px" ItemStyle-HorizontalAlign="Center" HeaderText="Plan For The Month" ReadOnly="True" SortExpression="PLAN_SEP" /> 
<asp:BoundField DataField="ACTUAL_SEP" ItemStyle-Width="52px" ItemStyle-HorizontalAlign="Center" HeaderText="Actual For The Month" ReadOnly="True" SortExpression="ACTUAL_SEP" /> 
<asp:BoundField DataField="SCORE_FORMONTH" ItemStyle-Width="80px" ItemStyle-HorizontalAlign="Center" HeaderText="Score" ReadOnly="True" SortExpression="SCORE_FORMONTH" /> 
<asp:BoundField DataField="PLAN_LIVE" ItemStyle-Width="52px" ItemStyle-HorizontalAlign="Center" HeaderText="Plan Upto Month" ReadOnly="True" SortExpression="PLAN_LIVE" /> 
<asp:BoundField DataField="ACTUAL_LIVE" ItemStyle-Width="52px" ItemStyle-HorizontalAlign="Center" HeaderText="Actual Upto Month" ReadOnly="True" SortExpression="ACTUAL_LIVE" /> 
<asp:BoundField DataField="SCORE_UPTOMONTH" ItemStyle-Width="80px" ItemStyle-HorizontalAlign="Center" HeaderText="Score" ReadOnly="True" SortExpression="SCORE_UPTOMONTH" /> 

現在我需要顯示尾行,它會顯示相應的列值的總和。 合計|共1 |共2 | ......

如何做到這一點?

+0

您是否必須使用BoundFields?你可以使用TemplatesFields嗎?如果是這樣,有一個簡單的方法來做到這一點。 – CoderMarkus

+0

不,因爲我已經將數據保存在數據庫中,只需要顯示它,boundfield是最簡單的使用方法。我不能做這個改變,因爲我沒有控制權。我的任務是在頁腳中顯示總數。 –

回答

3

編輯

有關LY綁定字段

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
     double total = 0; 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      string sNum = e.Row.Cells[1].Text;//just changed the index of cells based on your requirements 
      double sum; 
      if(double.TryParse(sNum,out sum)) 
      { 
       total += sum; 
      } 

      GridView1.FooterRow.Cells[1].Text = total.ToString(); 
     } 
} 

您可以使用databoudnc柱和代碼的這樣

int total = 0; 
protected void gvEmp_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if(e.Row.RowType==DataControlRowType.DataRow) 
    { 
     //replace maounty with the name of property 
    total += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "Amount")); 
    } 
    if(e.Row.RowType==DataControlRowType.Footer) 
    { 
    Label lblamount = (Label)e.Row.FindControl("lblTotal"); 
    lblamount.Text = total.ToString(); 
    } 
} 

檢查turotiral:http://www.aspdotnet-suresh.com/2011/02/how-to-display-sum-of-columns-total-in.html

+0

你能不能也請發佈設計師代碼?我不知道你是如何使用綁定行的腳註行? –

+0

@HiteshGawhade - 嗨請檢查:http://www.aspdotnet-suresh.com/2011/02/how-to-display-sum-of-columns-total-in.html所有代碼鏈接.. –

+0

thnx我得到它的工作..最終必須使用模板字段.. –

0

這可以輕鬆完成如下:

在ASPX:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" onrowdatabound="GridView1_RowDataBound" ShowFooter="true"> <Columns> <asp:BoundField DataField="Qty" /> </Columns> </asp:GridView>

在aspx.cs

//Define global variable 
int totQty = 0; 
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     totQty += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "Qty")); 
    } 
    if (e.Row.RowType == DataControlRowType.Footer) 
    { 
     e.Row.Cells[0].Text = totQty.ToString(); 
    } 
} 

就是這樣。