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);
}
}
在首页定义两个变量: 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);
}
}
相关文章推荐
- Win2008文件夹共享遇到问题mark一下
- Win7系统中添加局域网中别人共享的打印机 出错, 找不到驱动程序 的问题的解决
- 问题:MTK手机软件开发平台中字串资源添加进去了,菜单也能用,但是菜单上的字符串显示不出来。
- 关于asp.net中gridview的问题,关于footer,16aspx上下的英语交流网程序,管理员的添加和修改有问题
- GridView添加合计行的实现方案
- 总结一下Gridview隐藏列的问题
- GridView页脚添加合计字段的两种方法
- GridView添加统计(合计)行
- HP-UX 11.31 安装RAC 添加共享磁盘的问题
- 作为一个老师,我也把我回答的一些问题在这里共享一下,希望有所帮助
- IE8 font-awesome问题,不能重绘,在dom加载完后执行一下下面的代码图标就出来了
- pulltorefresh + StaggeredGridView 添加headerView后不能上拉加载更多的问题
- Android GridView添加头部问题的解决
- Android中关于GrideView,ListView的getView中position=0,错乱的问题。(在GridView的最后面添加两个固定item)
- 作为一个老师,我也把我回答的一些问题在这里共享一下,希望有所帮助
- Android ListView GridView添加 padding后padding部分不跟随滑动移动问题解决方案
- HP-UX 11.31 安装RAC 添加共享磁盘的问题(两种办法)
- 通过反射搞定GridViewCollection 的添加新的GridViewRow问题
- GridView动态添加列之后,导致PostBack(回发)页面数据丢失问题解决
- 新手求助:大神们帮帮我,关于在ViewPage中添加GridView的问题