【问题标题】:How to diplay BoundField Columns values sum in Footer Row如何在页脚行中显示 BoundField 列值总和
【发布时间】:2012-10-02 22:26:18
【问题描述】:

我正在绑定一个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 个 | ......

如何做到这一点?

【问题讨论】:

  • 你必须使用BoundFields吗?你可以使用 TemplatesFields 吗?如果是这样,有一个简单的方法来做到这一点。
  • 不,因为我已经将数据保存在数据库中,只需要显示它,boundfield 是最简单的使用方式。我无法进行此更改,因为我没有控制权。我的任务是在页脚中显示总计。

标签: asp.net gridview footer


【解决方案1】:

编辑

仅限绑定字段

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

【讨论】:

  • 能否请您也发布设计师代码?我不知道你是如何使用带边界字段的页脚行的?
  • @HiteshGawhade - 嗨,请检查:aspdotnet-suresh.com/2011/02/… 链接以获取所有代码..
【解决方案2】:

这可以通过以下方式轻松完成:

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

就是这样。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-08
    • 2020-02-01
    • 2018-01-29
    • 1970-01-01
    • 1970-01-01
    • 2012-07-28
    • 2019-08-30
    • 1970-01-01
    相关资源
    最近更新 更多