【问题标题】:asp.net Gridview sum multiple columnasp.net Gridview总和多列
【发布时间】:2015-05-12 14:51:20
【问题描述】:

我正在尝试动态汇总列并添加为标签。

我有一个名为“GrdDynamic”的网格视图

GrdDynamic_RowDataBound

protected void GrdDynamic_RowDataBound(object sender, GridViewRowEventArgs e)
{
int total = 0;

if (e.Row.RowType == DataControlRowType.Footer)
{
foreach (GridViewRow row in GrdDynamic.Rows)
{
   for (int i = 6; i <= GrdDynamic.Columns.Count; i++)
   {

       if (!string.IsNullOrEmpty(row.Cells[i].Text))
       {
        total = total + Convert.ToInt32(row.Cells[i].Text);
       }


      Label label = new Label();
      e.Row.Cells[i].Text = total.ToString();
      label.Text = "total" + " " + total;
      e.Row.Cells[i].Controls.Add(label);

   }
}
}
}

我需要在 6. 列之后根据每一列动态求和。

作为实例:

如果我有 10 列

我必须从 6. 列到 10. 列求和,如下所示

6. column sum is 500

7. column sum is 350

8. column sum is 150

9. column sum is 100

10.column sum is 330

如何在代码方面做到这一点

我们将不胜感激。

谢谢。

【问题讨论】:

  • 什么是aspx,什么是数据源?另外,除了您从第 7 列开始之外,您当前的方法有什么问题?

标签: c# asp.net gridview


【解决方案1】:

这一行:

i <= GridView1.Columns.Count;

应该是:

i < GridView1.Columns.Count;

否则您将访问一个不存在的列。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 2019-02-06
    • 1970-01-01
    • 1970-01-01
    • 2018-01-29
    • 2017-02-02
    • 1970-01-01
    相关资源
    最近更新 更多