asp.net Gridview 求和多列
asp.net Gridview sum multiple column
我正在尝试对列进行动态求和并添加为标签。
我有一个名为 "GrdDynamic"
的 gridview
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
我如何在代码端执行此操作
任何帮助将不胜感激。
谢谢。
这一行:
i <= GridView1.Columns.Count;
应该是:
i < GridView1.Columns.Count;
或者您将访问一个不存在的列。
我正在尝试对列进行动态求和并添加为标签。
我有一个名为 "GrdDynamic"
的 gridviewGrdDynamic_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
我如何在代码端执行此操作
任何帮助将不胜感激。
谢谢。
这一行:
i <= GridView1.Columns.Count;
应该是:
i < GridView1.Columns.Count;
或者您将访问一个不存在的列。