1

我正在绑定一个gridview,所有列都是boundfields:

 <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 个 | ……

如何做到这一点?

4

2 回答 2

3

编辑

仅适用于绑定字段

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

于 2012-10-12T06:31:03.900 回答
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();
    }
 }

就是这样。

于 2014-09-03T08:27:37.097 回答