【问题标题】:gridview column sum value, and display on webpage asp.netgridview列总和值,并显示在网页asp.net上
【发布时间】:2013-11-27 20:00:39
【问题描述】:

我目前有:

<asp:GridView ID="BalanceCheckDataGridView" runat="server"  AutoGenerateColumns="false">
  <Columns>
         <asp:BoundField ItemStyle-Width="30%" DataField="Company" HeaderText="Company" />
         <asp:BoundField ItemStyle-Width="30%" DataField="Balance" HeaderText="Balance" />
   </Columns>
</asp:GridView>

现在我想从余额中获取信息,并将它们汇总,然后显示它(可以在最后一行,如“Total:sumValue”或一个标签,其文本可以是它的总和值)

我是新手,请你帮忙。

非常感谢。

【问题讨论】:

  • 你可以在你的 sql 查询中这样做吗?
  • @briskovich:最好在我这边做,不想碰数据库。请问您有什么想法吗?
  • 你想在网格的底部吗?
  • @briskovich:那会很好。 :D

标签: c# asp.net gridview


【解决方案1】:
Int32 tot = 0; 
protected void Dg_Source_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        tot = tot + Convert.ToInt32(e.Row.Cells[1].Text);
        lblSum.Text = tot.ToString();
    }
}

【讨论】:

    【解决方案2】:

    在网格上放置一个页脚,然后在绑定网格后,您可以在后面的代码中执行类似的操作。

    ((Label)your_grid.FooterRow.Cells[1].FindControl("your_label_to_diplay_total")).Text = "Total:" + ds.Tables[0].Compute("sum(your_balance_field)", "").ToString();
    

    这不是我的想法。您必须根据您的应用程序对其进行自定义。

    【讨论】:

    • 谢谢你的代码,但我没有 ds 表,我可以知道它是用来做什么的吗?
    【解决方案3】:
        int sum=0;
        protected void GridView2_RowDataBound(object sender,  GridViewRowEventArgs e)
             {
    
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label salary = (Label)e.Row.FindControl("Label3");//take lable id
                sum +=int.Parse(salary.Text);
                lblsum.Text = sum.ToString();
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-29
      • 1970-01-01
      • 1970-01-01
      • 2011-03-27
      • 1970-01-01
      • 2012-07-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多