动态生成的网格视图的列和
本文关键字:视图 网格 动态 | 更新日期: 2023-09-27 18:06:17
我需要在页脚中获得gridview单元格的总和。GridView
是动态生成的,然后显示在网页上。
如果由cellId
指定,我可以得到任何列的总和,但是当我试图申请循环时,它不起作用。
if (e.Row.RowType == DataControlRowType.Header)
{
}
else
{
for (int i = 18; i < e.Row.Cells.Count; i++)
{
// e.Row.Cells[i].Text = e.Row.Cells[i].Text.Trim().Replace(" ", "0").Replace("amp;", "0");
total[i] += Convert.ToDouble(e.Row.Cells[i].Text);
}
}
18是将计算总和的列数。注释行已经放置,因此没有空或空格....
我不确定你有什么实际问题,但是,为什么不使用DataControlRowType.Footer
代替和这个小LINQ查询:
if (e.Row.RowType == DataControlRowType.Footer)
{
double cellValue = 0;
double sum = e.Row.Cells.Cast<TableCell>().Skip(18)
.Where(cell => double.TryParse(cell.Text, out cellValue))
.Sum(cell => cellValue);
}
经过一番努力,我发现…谢谢你给我指正确的方向。
if (GridView1.Rows.Count > 0)
{
int TtlRows = GridView1.Rows.Count;
int TtlCol = GridView1.Rows[0].Cells.Count;
int FxdCol = 1;
int ComputedCol = TtlCol - FxdCol;
for (int i = FxdCol; i < TtlCol; i++)
{
double sum = 0.000;
for (int j = 0; j < TtlRows; j++)
{
sum += GridView1.Rows[j].Cells[i].Text != " " ? double.Parse(GridView1.Rows[j].Cells[i].Text) : 0.000;
}
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[i].Text = sum.ToString("#0.000");
}
}
}