首先设置 showfooter=true;
在首页定义两个变量: public float Mysum1; public float Mysum2;
然后写 RowDatabind方法:
protected void GridView1_RowCreated(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
base.GridView_RowDataBound(e, 1, 18);
base.GridView_RowDataBound(e, 9, 28);
DataKey Keys = GridView1.DataKeys[e.Row.RowIndex];
TableCell myTableCell;
myTableCell = e.Row.Cells[5];
HyperLink LB = (HyperLink)(myTableCell.Controls[0]);
LB.Enabled = Keys["SZLX"].ToString().Trim() == "0";
DataRowView MyRows=(DataRowView)e.Row.DataItem;
Mysum1 += MyRows["SR"] is DBNull ? 0 : float.Parse(MyRows["SR"].ToString());
Mysum2 += MyRows["ZC"] is DBNull ? 0 : float.Parse(MyRows["ZC"].ToString());
/* 注意这里MyRows["SR"]网上的例子用的是索引号MyRows[6],这是假定你所显示的字段顺序和数据源的顺序是一至的,事实上我们都 是用select* from 这样的语句,所以用序号会出现错误,所以这个地方应该用字段用而不是索引号 ,出现数据类型转换错误我觉得可能就是这个原因造成的*/
}
if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[0].Text = "合计:";
e.Row.Cells[6].Text = string.Format("{0:f2}", Mysum1);
e.Row.Cells[7].Text = string.Format("{0:f2}", Mysum2);
}
}