DataGrid 汇总行
定义保存变量保存汇总值
private double myTotalMoney;
private double myTotalNum;
在ItemDataBound中处理事件(由于在Footer中显示,必须设置Footer可见)
private void grid_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
switch ((int)(e.Item.ItemType))
{
case (int)ListItemType.Item :
case (int)ListItemType.AlternatingItem:
//Calculate total for the field of each row and alternating row.
myTotalNum += Convert.ToDouble(e.Item.Cells[3].Text);
myTotalMoney += Convert.ToDouble(e.Item.Cells[4].Text);
//Format the data, and then align the text of each cell to the right.
e.Item.Cells[4].Text = Convert.ToDouble(e.Item.Cells[4].Text).ToString("##,##0.00");
e.Item.Cells[4].Attributes.Add("align", "right");
break;
case (int)ListItemType.Footer:
//Use the footer to display the summary row.
e.Item.Cells[1].Text = "总 计";
e.Item.Cells[1].Attributes.Add("align", "left");
e.Item.Cells[3].Attributes.Add("align", "left");
e.Item.Cells[4].Attributes.Add("align", "right");
e.Item.Cells[3].Text = myTotalNum.ToString();
e.Item.Cells[4].Text = myTotalMoney.ToString("c");
break;
}
}

浙公网安备 33010602011771号