C#中如何給GridView添加頁腳以實現統計某一列值的和

第一步:

設置GridView的屬性:ShowFooter=true

第二步:

定義一個全局變量,以備使用,如:
private decimal thisTotal=0;//本頁面合計
private decimal total=0;//共計
然後在GridViewShow()函數中計算出這些值。自定義函數GridViewShow()用於實現GridView的數據綁定。
如GridView的格式爲:
<asp:GridView ID="GridView1" AutoGenerateColumns="false" DataKeyNames="ID" runat="server" OnRowDataBound="GridView1_RowDataBound" ShowFooter="True">
	<RowStyle BackColor="#ffffff" />
	<AlternatingRowStyle BackColor="#fffef9" />
	<Columns>
		<asp:TemplateField>
		  <HeaderTemplate>
		     <input type="checkbox" name="BoxIdAll" id="BoxIdAll" οnclick="onclicksel();" />
		  </HeaderTemplate>
		  <ItemTemplate>
		     <input id="BoxId" name="BoxId" value='<%#(Convert.ToString(Eval("ID")))%>' type="checkbox" />
		  </ItemTemplate>
		  <ItemStyle Height="23px" HorizontalAlign="Center" />
	  	  <HeaderStyle Width="3%" BackColor="#80B4CF" Height="25px" />
		</asp:TemplateField>
		<asp:TemplateField HeaderText="序號">
		  <ItemTemplate>
 		    <font color="#000000" style="font-size: 10px">
	              <%#GetCount()%>
	            </font>
		  </ItemTemplate>
 	          <ItemStyle HorizontalAlign="Center" />
                                    <HeaderStyle Width="5%" BackColor="#80B4CF" HorizontalAlign="Center" />
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="單位">
                                    <ItemTemplate>
                                        <font style="font-size: 11px">
                                            <%#strTrim(Convert.ToString(Eval("BranchName")))%>
                                        </font>
                                    </ItemTemplate>
                                    <ItemStyle HorizontalAlign="Center" />
                                    <HeaderStyle HorizontalAlign="center" Width="17%" BackColor="#80B4CF" />
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="類別">
                                    <ItemTemplate>
                                        <font style="font-size: 11px">
                                            <%#getZLB(Convert.ToString(Eval("FYXM")))%>
                                        </font>
                                    </ItemTemplate>
                                    <ItemStyle HorizontalAlign="Center" />
                                    <HeaderStyle HorizontalAlign="center" Width="10%" BackColor="#80B4CF" />
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="具體項目">
                                    <ItemTemplate>
                                        <font style="font-size: 11px">
                                            <%#strTrim(Convert.ToString(Eval("CWMC")))%>
                                        </font>
                                    </ItemTemplate>
                                    <ItemStyle HorizontalAlign="Center" />
                                    <HeaderStyle HorizontalAlign="center" BackColor="#80B4CF" />
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="金額 (單位:元)">
                                    <ItemTemplate>
                                        <font style="font-size: 11px">
                                            <%#strTrim(Convert.ToString(Eval("JE"))) %>
                                        </font>
                                    </ItemTemplate>
                                    <ItemStyle HorizontalAlign="Center" />
                                    <HeaderStyle HorizontalAlign="center" Width="16%" BackColor="#80B4CF" />
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="統計年月">
                                    <ItemTemplate>
                                        <font style="font-size: 11px">
                                            <%#strTrim(Convert.ToString(Eval("NF")))%>年<%#strTrim(Convert.ToString(Eval("YF")))%>月
                                        </font>
                                    </ItemTemplate>
                                    <ItemStyle HorizontalAlign="center" />
                                    <HeaderStyle BackColor="#80B4CF" Width="16%" HorizontalAlign="center" />
                                </asp:TemplateField>
                            </Columns>
</asp:GridView>
示圖爲:


在GridViewShow()函數中:
public void GridViewShow(){
       string sql = "SELECT sum(c.JE) as tot FROM T_CWGL c ";
        sql += " left join T_CWLB d on d.ID = c.FYXM and d.IsUse = '1' ";
        sql += " left join t_sys_Branch b on b.BranchCode = c.DW and b.IsUse = '1' ";
        sql += " where c.IsUse='1' ";
        sql += strWhere;  //strWhere爲三個下拉列表的條件
        DataTable dt = dbSys.ExecuteDataSet(sql, AspNetPager1.PageSize * (AspNetPager1.CurrentPageIndex - 1), AspNetPager1.PageSize).Tables[0];
        DataTable dtall = dbSys.ExecuteDataSet(sql).Tables[0];
        string ss = dt.Rows[0]["tot"].ToString();
        string sall = dtall.Rows[0]["tot"].ToString();
        if (ss == "" || ss == null)
        {
            thisTotal = 0;
        }
        else
        {
            thisTotal = Convert.ToDecimal(dt.Rows[0]["tot"].ToString());
        }
        if (sall == "" || ss == null)
        {
            total = 0;
        }
        else
        {
            total = Convert.ToDecimal(dtall.Rows[0]["tot"].ToString());
        }                                                                                                                                                         //該GridView的數據綁定省略															   }

這樣便可以獲得兩個值了,其中用到的分頁工具爲AspNetPager

第三步:

爲該GridView添加RowDataBound事件:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //----------鼠標移動到每項時顏色交替效果
            e.Row.Attributes["onmouseover"] = "e=this.style.backgroundColor;this.style.backgroundColor='#FFFF99'";
            e.Row.Attributes["onmouseout"] = "this.style.backgroundColor=e";
            //----------設置懸浮鼠標指針形狀爲小手"
            e.Row.Attributes["style"] = "Cursor:hand";
            //----------鼠標點擊某行即選中某行
            e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex);
        }
        if (GridView1.FooterRow != null)
        {
            GridView1.FooterRow.Visible = true;
        }
        if (e.Row.RowType == DataControlRowType.Footer)          // 判斷當前項是否爲頁腳
        {
            e.Row.Cells[4].HorizontalAlign = HorizontalAlign.Right;  //因爲示例中該GridView爲7列
            e.Row.Cells[4].Text = "費用合計:";
            e.Row.Cells[5].Text = thisTotal.ToString()+" 元";
            e.Row.Cells[5].HorizontalAlign = HorizontalAlign.Center;
            e.Row.Cells[6].Text = "共計:"+total+" 元";
            e.Row.Cells[6].HorizontalAlign = HorizontalAlign.Center;
        }
}

這樣,便可以實現統計某一列的值了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章