您的位置:首页 > 其它

Gridview添加合计行问题,浪费了2小时,写出来共享一下

2011-06-26 09:29 316 查看
首先设置 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);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐