天天看点

gridview列的汇总

Gridview 中我们会用到汇总的功能,因为前一段时间用到了,所以在此总结一下,作为以后的参考;

需要用到Gridview的<b>RowDataBound</b>事件,<b> RowDataBound</b>是在数据源与gridview的行绑定完成之后才执行<b>RowDataBound</b>方法,也就是说数据源每和gridview的行绑定完成之后都会触发此事件,首先是要把gridview的showfooter属性设置为true,以便我们利用footer行进行统计

//统计

Decimal mysum3 = 0;

Decimal mysum4 = 0;

Decimal mysum5 = 0;

Decimal mysum6 = 0;

Decimal mysum7 = 0;

protected void gvResult_RowDataBound(object sender, GridViewRowEventArgs e)

{}

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

if (e.Row.RowType == DataControlRowType.DataRow)  

        {  

            //统计  

            if (!string.IsNullOrEmpty(e.Row.Cells[6].Text.ToString()) &amp;&amp; e.Row.Cells[6].Text.ToString() != "&amp;nbsp;")  

            {  

                Decimal sum1 = decimal.Parse(e.Row.Cells[6].Text.ToString());  

                mysum3mysum3 = mysum3+sum1;  

            }  

            if (!string.IsNullOrEmpty(e.Row.Cells[7].Text.ToString()) &amp;&amp; e.Row.Cells[7].Text.ToString() != "&amp;nbsp;")  

                Decimal sum2 = decimal.Parse(e.Row.Cells[7].Text.ToString());  

                mysum4mysum4 = mysum4 + sum2;  

            if (!string.IsNullOrEmpty(e.Row.Cells[8].Text.ToString()) &amp;&amp; e.Row.Cells[8].Text.ToString() != "&amp;nbsp;")  

                Decimal sum3 = decimal.Parse(e.Row.Cells[8].Text.ToString());  

                mysum5mysum5 = mysum5 + sum3;  

            if (!string.IsNullOrEmpty(e.Row.Cells[9].Text.ToString()) &amp;&amp; e.Row.Cells[9].Text!="&amp;nbsp;")  

                Decimal sum4 = decimal.Parse(e.Row.Cells[9].Text.ToString());  

                mysum6mysum6 = mysum6 + sum4;  

            if (!string.IsNullOrEmpty(e.Row.Cells[10].Text.ToString()) &amp;&amp; e.Row.Cells[10].Text.ToString() != "&amp;nbsp;")  

                Decimal sum5 = decimal.Parse(e.Row.Cells[10].Text.ToString());  

                mysum7mysum7 = mysum7 + sum5;  

        } 

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

if (e.Row.RowType == DataControlRowType.Footer)  

       {  

           e.Row.Cells[0].Text = "&lt;span align='center' style='color:red'&gt;合计:&lt;/span&gt;";  

           e.Row.Cells[6].Text ="&lt;span align='center' style='color:red'&gt;"+ mysum3.ToString()+"&lt;/span&gt;";  

           e.Row.Cells[7].Text = "&lt;span align='center' style='color:red'&gt;" + mysum4.ToString() + "&lt;/span&gt;";  

           e.Row.Cells[8].Text = "&lt;span align='center' style='color:red'&gt;" + mysum5.ToString() + "&lt;/span&gt;";  

           e.Row.Cells[9].Text = "&lt;span align='center' style='color:red'&gt;" + mysum6.ToString() + "&lt;/span&gt;";  

           e.Row.Cells[10].Text = "&lt;span align='center' style='color:red'&gt;" + mysum7.ToString() + "&lt;/span&gt;";  

       } 

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

效果如下:

<a href="http://blog.51cto.com/attachment/201111/225630489.jpg" target="_blank"></a>

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