[ASP.NET][WebControl] GridView列尾(footer)合計
使用GridView在footer中進行資料的合計
在 GridView 的 RowDataBound 事件中進行總和的計算,程式碼如下
int _totalCount = 0;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
switch (e.Row.RowType)
{
case DataControlRowType.Header:
e.Row.Cells[0].Text = "訂單編號";
e.Row.Cells[1].Text = "客戶編號";
e.Row.Cells[2].Text = "貨物";
e.Row.Cells[3].Text = "收貨人";
break;
case DataControlRowType.DataRow:
_totalFreight += decimal.Parse(e.Row.Cells[2].Text);
_totalCount++;
break;
case DataControlRowType.Footer:
// 合計值的呈現
e.Row.Cells[1].Text = "合計";
e.Row.Cells[2].Text = _totalFreight.ToString();
_totalFreight = 0;
_totalCount = 0;
break;
}
}
呈現畫面
範例檔案 GridViewFooter.rar