第一步:
设置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的数据绑定省略 }
第三步:
为该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;
}
}
这样,便可以实现统计某一列的值了。