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>";
        }

    }

 

posted @ 2011-11-13 23:00  神舟龙  阅读(3753)  评论(5编辑  收藏  举报