开发者社区> 科技小先锋> 正文

gridview列的汇总

简介:
+关注继续查看
Gridview 中我们会用到汇总的功能,因为前一段时间用到了,所以在此总结一下,作为以后的参考;
需要用到Gridview的RowDataBound事件, RowDataBound是在数据源与gridview的行绑定完成之后才执行RowDataBound方法,也就是说数据源每和gridview的行绑定完成之后都会触发此事件,首先是要把gridview的showfooter属性设置为true,以便我们利用footer行进行统计

 

 


  1. //统计
  2. Decimal mysum3 = 0;
  3. Decimal mysum4 = 0;
  4. Decimal mysum5 = 0;
  5. Decimal mysum6 = 0;
  6. Decimal mysum7 = 0;
  7. protected void gvResult_RowDataBound(object sender, GridViewRowEventArgs e)
  8. {}

 

 

因为数据是会带有小数点的,所以定义decimal类型的 全局变量,存放进行数据统计之和,然后触发RowDataBound事件

  1. if (e.Row.RowType == DataControlRowType.DataRow)  
  2.         {  
  3.             //统计  
  4.             if (!string.IsNullOrEmpty(e.Row.Cells[6].Text.ToString()) && e.Row.Cells[6].Text.ToString() != " ")  
  5.             {  
  6.                 Decimal sum1 = decimal.Parse(e.Row.Cells[6].Text.ToString());  
  7.                 mysum3mysum3 = mysum3+sum1;  
  8.             }  
  9.  
  10.             if (!string.IsNullOrEmpty(e.Row.Cells[7].Text.ToString()) && e.Row.Cells[7].Text.ToString() != " ")  
  11.             {  
  12.                 Decimal sum2 = decimal.Parse(e.Row.Cells[7].Text.ToString());  
  13.                 mysum4mysum4 = mysum4 + sum2;  
  14.             }  
  15.  
  16.             if (!string.IsNullOrEmpty(e.Row.Cells[8].Text.ToString()) && e.Row.Cells[8].Text.ToString() != " ")  
  17.             {  
  18.                 Decimal sum3 = decimal.Parse(e.Row.Cells[8].Text.ToString());  
  19.                 mysum5mysum5 = mysum5 + sum3;  
  20.             }  
  21.  
  22.             if (!string.IsNullOrEmpty(e.Row.Cells[9].Text.ToString()) && e.Row.Cells[9].Text!=" ")  
  23.             {  
  24.                 Decimal sum4 = decimal.Parse(e.Row.Cells[9].Text.ToString());  
  25.                 mysum6mysum6 = mysum6 + sum4;  
  26.             }  
  27.             if (!string.IsNullOrEmpty(e.Row.Cells[10].Text.ToString()) && e.Row.Cells[10].Text.ToString() != " ")  
  28.             {  
  29.                 Decimal sum5 = decimal.Parse(e.Row.Cells[10].Text.ToString());  
  30.                 mysum7mysum7 = mysum7 + sum5;  
  31.             }  
  32.             
  33.               
  34.         } 

当源数据行和gridview行绑定完成之后,需要判断行类型是不是数据行,如果是数据行就会进入以上代码,然后需要判断gridview行的列是不是为空,和不等于 然后把当前行的列值和之前已绑定的数据行的列值进行相加;

 


  1. if (e.Row.RowType == DataControlRowType.Footer)  
  2.        {  
  3.            e.Row.Cells[0].Text = "<span align='center' style='color:red'>合计:</span>";  
  4.            e.Row.Cells[6].Text ="<span align='center' style='color:red'>"+ mysum3.ToString()+"</span>";  
  5.            e.Row.Cells[7].Text = "<span align='center' style='color:red'>" + mysum4.ToString() + "</span>";  
  6.            e.Row.Cells[8].Text = "<span align='center' style='color:red'>" + mysum5.ToString() + "</span>";  
  7.            e.Row.Cells[9].Text = "<span align='center' style='color:red'>" + mysum6.ToString() + "</span>";  
  8.            e.Row.Cells[10].Text = "<span align='center' style='color:red'>" + mysum7.ToString() + "</span>";  
  9.        } 

这里也是对绑定行进行判断,如果行类型是脚注行的话,进行脚注行的赋值操作,即把数据行的列之和赋值给脚注行的某列,赋值的同时可以给脚注列添加一些样式,把字体改为红色,更醒目

效果如下:


本文转自shenzhoulong  51CTO博客,原文链接:http://blog.51cto.com/shenzhoulong/713249,如需转载请自行联系原作者

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
列表ListBox、ListView、GridView 排序
列表排序 1.使用控件默认排序方式(推荐) ListControl.Items.SortDescriptions.Clear(); ListControl.Items.SortDescriptions.
820 0
gridview的OnRowDataBound必须存在 <Columns>
                      protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)     {         Response.
701 0
+关注
科技小先锋
文章
问答
视频
文章排行榜
最热
最新
相关电子书
更多
低代码开发师(初级)实战教程
立即下载
阿里巴巴DevOps 最佳实践手册
立即下载
冬季实战营第三期:MySQL数据库进阶实战
立即下载