說明:有一GridView1如下
月份 薪水
1 22000
2 22000
3 22000
... ...
12 22001
不透過sql匯總,想在GridView的footer顯示薪水總和該怎麼做?
作法:
在RowDataBound事件中將數據做匯總,再顯示在footer中。
//xxx.aspx
<GridView ID="GridView1" ShowFooter="True">
</GridView>
//xxx.aspx.cs
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
int iSumSalary = 0;
foreach (GridViewRow gvr in GridView1.Rows)
{
iSumSalary += Convert.ToInt32(gvr.Cells[1].Text);
}
if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[0].Text = "合計";
e.Row.Cells[1].Text = iSumSalary.ToString();
}
}
參考資料:
http://www.dotblogs.com.tw/puma/archive/2008/10/17/5713.aspx
http://bibby.be/2009/01/gridview.html
沒有留言:
張貼留言