我使用此代码对列值s
求和int total = 0;
protected void gvEmp_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType==DataControlRowType.DataRow)
{
total += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "Amount"));
}
if(e.Row.RowType==DataControlRowType.Footer)
{
Label lblamount = (Label)e.Row.FindControl("lblTotal");
lblamount.Text = total.ToString();
}
}
我的数据库中有更多数据。所以我决定使用下面的Linq Query
GridView1.Columns[8].FooterText = (from row in dt.AsEnumerable()
select row.Field<int>("amount")).Sum().ToString();
但我在网格中进行了分页,页面大小为50,因此我必须首先将第1页总和相加如果我点击第2页然后我必须显示第2页总数。怎么实现呢?
答案 0 :(得分:2)
试试这个:
GridView1.Columns[8].FooterText = (from row in dt.AsEnumerable()
select row.Field<int>("amount")).Skip(GridView1.PageIndex * GridView1.PageSize).Take(GridView1.PageSize).Sum().ToString();