如何在页脚行中显示BoundField Columns值和

时间:2012-10-12 06:25:12

标签: asp.net gridview footer

我绑定了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 | ......

如何实现这一目标?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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();
    }
 }

就是这样。