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. if (!string.IsNullOrEmpty(e.Row.Cells[7].Text.ToString()) && e.Row.Cells[7].Text.ToString() != " ")
  10. {
  11. Decimal sum2 = decimal.Parse(e.Row.Cells[7].Text.ToString());
  12. mysum4mysum4 = mysum4 + sum2;
  13. }
  14. if (!string.IsNullOrEmpty(e.Row.Cells[8].Text.ToString()) && e.Row.Cells[8].Text.ToString() != " ")
  15. {
  16. Decimal sum3 = decimal.Parse(e.Row.Cells[8].Text.ToString());
  17. mysum5mysum5 = mysum5 + sum3;
  18. }
  19. if (!string.IsNullOrEmpty(e.Row.Cells[9].Text.ToString()) && e.Row.Cells[9].Text!=" ")
  20. {
  21. Decimal sum4 = decimal.Parse(e.Row.Cells[9].Text.ToString());
  22. mysum6mysum6 = mysum6 + sum4;
  23. }
  24. if (!string.IsNullOrEmpty(e.Row.Cells[10].Text.ToString()) && e.Row.Cells[10].Text.ToString() != " ")
  25. {
  26. Decimal sum5 = decimal.Parse(e.Row.Cells[10].Text.ToString());
  27. mysum7mysum7 = mysum7 + sum5;
  28. }
  29. }

当源数据行和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. }

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

效果如下:

完整代码如下:

 

  Decimal mysum3 =  0;
    Decimal mysum4 =  0;
    Decimal mysum5 =  0;
    Decimal mysum6 =  0;
    Decimal mysum7 =  0;
     protected  void gvResult_RowDataBound( object sender, GridViewRowEventArgs e)
    {
                if (e.Row.RowType == DataControlRowType.DataRow)
        {
             // 统计
             if (! string.IsNullOrEmpty(e.Row.Cells[ 6].Text.ToString()) && e.Row.Cells[ 6].Text.ToString() !=  " &nbsp; ")
            {
                Decimal sum1 =  decimal.Parse(e.Row.Cells[ 6].Text.ToString());
                mysum3 = mysum3+sum1;
            }

             if (! string.IsNullOrEmpty(e.Row.Cells[ 7].Text.ToString()) && e.Row.Cells[ 7].Text.ToString() !=  " &nbsp; ")
            {
                Decimal sum2 =  decimal.Parse(e.Row.Cells[ 7].Text.ToString());
                mysum4 = mysum4 + sum2;
            }

             if (! string.IsNullOrEmpty(e.Row.Cells[ 8].Text.ToString()) && e.Row.Cells[ 8].Text.ToString() !=  " &nbsp; ")
            {
                Decimal sum3 =  decimal.Parse(e.Row.Cells[ 8].Text.ToString());
                mysum5 = mysum5 + sum3;
            }

             if (! string.IsNullOrEmpty(e.Row.Cells[ 9].Text.ToString()) && e.Row.Cells[ 9].Text!= " &nbsp; ")
            {
                Decimal sum4 =  decimal.Parse(e.Row.Cells[ 9].Text.ToString());
                mysum6 = mysum6 + sum4;
            }
             if (! string.IsNullOrEmpty(e.Row.Cells[ 10].Text.ToString()) && e.Row.Cells[ 10].Text.ToString() !=  " &nbsp; ")
            {
                Decimal sum5 =  decimal.Parse(e.Row.Cells[ 10].Text.ToString());
                mysum7 = mysum7 + sum5;
            }
          
            
        }
         if (e.Row.RowType == DataControlRowType.Footer)
        {
            e.Row.Cells[ 0].Text =  " <span align='center' style='color:red'>合计:</span> ";
            e.Row.Cells[ 6].Text = " <span align='center' style='color:red'> "+ mysum3.ToString()+ " </span> ";
            e.Row.Cells[ 7].Text =  " <span align='center' style='color:red'> " + mysum4.ToString() +  " </span> ";
            e.Row.Cells[ 8].Text =  " <span align='center' style='color:red'> " + mysum5.ToString() +  " </span> ";
            e.Row.Cells[ 9].Text =  " <span align='center' style='color:red'> " + mysum6.ToString() +  " </span> ";
            e.Row.Cells[ 10].Text =  " <span align='center' style='color:red'> " + mysum7.ToString() +  " </span> ";
        }

    }

 

转载于:https://www.cnblogs.com/shenzhoulong/archive/2011/11/13/2247670.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值