Girdview 中字段求和
数据是Girdview 显示,使用查询命令得出部分字段,想求下某一列的字段之和?还没啥思路,谁能指点下
[解决办法]
refer : http://blog.csdn.net/baby97/article/details/7264662
[解决办法]
double sum = 0;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex >= 0)
{
sum += Convert.ToDouble(e.Row.Cells[7].Text);
}
else if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[5].Text = "总薪水:";
e.Row.Cells[6].Text = sum.ToString();
e.Row.Cells[3].Text = "平均薪水:";
e.Row.Cells[4].Text = ((int)(sum / GridView1.Rows.Count)).ToString();
}
foreach (TableCell tc in e.Row.Cells)
{
tc.Attributes["style"] = "border-color:Black";
}
if (e.Row.RowIndex != -1)
{
int id = GridView1.PageIndex * GridView1.PageSize + e.Row.RowIndex + 1;
e.Row.Cells[0].Text = id.ToString();
}
}
[解决办法]
最好sql语句完成求和
当然代码也可以
[解决办法]
Girdview数据绑定是来自dataset或者datatable,绑定之前遍历数据。
for(int i=0;i<dt.rows.count;i++)
{
sumNum += convert.toint32(dt.rows[i][合计列].tostring())
}
是要这个吗?