天天看點

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,如需轉載請自行聯系原作者