当前位置:编程学习 > C#/ASP.NET >>

gridview控件footer计算总和

设计的家庭财务管理系统,支出统计时,想计算支出总和,如何写啊?以下是我的脚本,
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>无标题页</title>
  <script language="JavaScript" type="text/JavaScript" src="js/common.js"></script>
    <link href="css/style.css" rel="stylesheet" type="text/css">
  
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table width="100%"  border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td height="32" align="left" valign="top" class=""><table width="98%"  border="0" cellspacing="0" cellpadding="0">
        <tr valign="top">
          <td>【 收入报表统计 】</td>
          <td align="right"><span class="text010"> 
          </span>   </td>
        </tr>
      </table>
      <table width="98%" border="0" cellpadding="0" cellspacing="0" class="bor003">
        <tr>
          <td height="40" align="left" valign="top" >       <asp:GridView ID="gvInfo" 
                  runat="server" AllowPaging="True" AutoGenerateColumns="False"
                  BackColor="White" CellPadding="5" DataKeyNames="id" OnPageIndexChanging="gvInfo_PageIndexChanging"
                  OnRowDataBound="gvInfo_RowDataBound" OnRowDeleting="gvInfo_RowDeleting" PageSize="12"
                  Width="100%" ShowFooter="True">
                  <Columns>
                      <asp:TemplateField HeaderText="收支项目">
                          <ItemStyle HorizontalAlign="Left" Width="200px" />
                          <ItemTemplate>
                              <asp:Label ID="Label11" runat="server" Text='<%# Bind("name") %>'></asp:Label>
                          </ItemTemplate>
                      </asp:TemplateField>
                     
                      <asp:TemplateField HeaderText="收支日期">
                          <ItemStyle HorizontalAlign="Left" Width="200px" />
                          <ItemTemplate>
                              <asp:Label ID="Label13" runat="server" Text='<%# Bind("Addtime","{0:d}") %>'></asp:Label>
                          </ItemTemplate>
                      </asp:TemplateField>
                      <asp:TemplateField HeaderText="收支家庭成员">
                          <ItemStyle HorizontalAlign="Left" Width="200px" />
                          <ItemTemplate>
                              <asp:Label ID="Label14" runat="server" Text='<%# Bind("UserName") %>'></asp:Label>
                          </ItemTemplate>
                      </asp:TemplateField>
                      <asp:TemplateField HeaderText="收支金额">
                          <ItemStyle HorizontalAlign="Left" Width="200px" />
                          <ItemTemplate>
                              <asp:Label ID="Label15" runat="server" Text='<%# Bind("AddMoney") %>'></asp:Label>
                          </ItemTemplate>
                      </asp:TemplateField>
                      <asp:TemplateField HeaderText="备注">
                          <ItemStyle HorizontalAlign="Left" Width="200px" />
                          <ItemTemplate>
                              <asp:Label ID="Label16" runat="server" Text='<%# Bind("Ds") %>'></asp:Label>
                          </ItemTemplate>
                      </asp:TemplateField>
                     
                                      
                  </Columns>
                  <PagerSettings FirstPageText="首页" LastPageText="尾页" Mode="NextPreviousFirstLast"
                      NextPageText="下一页" PageButtonCount="12" PreviousPageText="上一页" />
                  <RowStyle HorizontalAlign="Left" />
                  <HeaderStyle BackColor="#3399CC" ForeColor="#CCCCFF" />
                  <PagerTemplate>
                      <table border="0" width="100%">
                          <tr>
                              <td>
                                  <asp:LinkButton ID="LinkButtonFirstPage" runat="server" CommandArgument="First" CommandName="Page"
                                      Visible=" <%# ((GridView)Container.NamingContainer).PageIndex != 0 %>">首页 </asp:LinkButton>
                                  <asp:LinkButton ID="LinkButtonPreviousPage" runat="server" CommandArgument="Prev"
                                      CommandName="Page" Visible=" <%# ((GridView)Container.NamingContainer).PageIndex != 0 %>">上一页 </asp:LinkButton>
                                  <asp:LinkButton ID="LinkButtonNextPage" runat="server" CommandArgument="Next" CommandName="Page"
                                      Visible=" <%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>">下一页 </asp:LinkButton>
                                  <asp:LinkButton ID="LinkButtonLastPage" runat="server" CommandArgument="Last" CommandName="Page"
                                      Visible=" <%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>">尾页 </asp:LinkButton>
                                  共
                                  <asp:Label ID="LabelPageCount" runat="server" Text=" <%# ((GridView)Container.NamingContainer).PageCount %>"> </asp:Label>页
                                  第
                                  <asp:Label ID="Label2" runat="server" Text=" <%# ((GridView)Container.NamingContainer).PageIndex + 1 %>"> </asp:Label>页
                              </td>
                              <td align="right" width="20%">
                              </td>
                          </tr>
                      </table>
                  </PagerTemplate>
              </asp:GridView>
           
                  <table border="0" cellpadding="0" cellspacing="0" width="100%">
                      <tr>
                          <td align="center" class="text006" style="height: 32px" valign="top">
                              <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="导出到EXEl并打印报表" /></td>
                      </tr>
                  </table>
             
            </td>
        </tr>
      </table></td>
  </tr>
</table>
    </div>
    </form>
</body>
</html>

然后是我的代码
public partial class InqueSRMoeyList : System.Web.UI.Page
{
    SqlHelper data = new SqlHelper();
    Alert js = new Alert();
        protected void gvInfo_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
    }
    private void GetInfo()
    {
        try
        {
            gvInfo.DataSource = GetCodeBy(0);
            gvInfo.DataBind();
        }
        catch
        {

        }

    }
    protected void gvInfo_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvInfo.PageIndex = e.NewPageIndex;
        GetInfo();
    }

    

    protected void gvInfo_RowDataBound(object sender, GridViewRowEventArgs e)
    {
      
  
        //鼠标移动变色
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#f6f6f6',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
        }
        //单击行改变行背景颜色 
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onclick", "this.style.backgroundColor='#f6f6f6'; this.style.color='buttontext';this.style.cursor='default';");
        }

    }
    public DataSet GetCodeBy(int iCount)
    {
        string name = Request.QueryString["name"].ToString();
        string UserName = Request.QueryString["UserName"].ToString();
        string bT = Request.QueryString["bT"].ToString();
        string et = Request.QueryString["et"].ToString();
    

        SqlHelper date = new SqlHelper();
        string strTop = "";

        if (iCount > 1)
        {
            strTop = "top " + iCount.ToString();
        }

        string str = "";
        string sql = str;
        sql = "select  " + strTop + "  * from [F_shouru] where  1=1";
        if (name != "所有项目")
        {
            sql += " and   name   like   '%" + name + "%'   ";

        }
        if (UserName != "所有人员")
        {
            sql += " and   UserName   like   '%" + UserName + "%'   ";
        }

        sql += " and   Addtime  between '" + Convert.ToDateTime(bT) + "' and  '" + Convert.ToDateTime(et) + "'";
        SqlConnection con = new SqlConnection(SqlHelper.connstring);
        SqlCommand cmd = new SqlCommand(sql, con);

        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = null;
        try
        {
            con.Open();
            ds = new DataSet();
            da.Fill(ds);

        }
        catch (SqlException ex)
        {
            throw ex;
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            con.Close();
        }
        return ds;
    }
    public override void VerifyRenderingInServerForm(Control control)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        gvInfo.AllowPaging = false; //清除分页
        gvInfo.AllowSorting = false; //清除排序   
        GetInfo(); //你绑定gridview1数据源的那个函数。
        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "GB2312";
        Response.AppendHeader("Content-Disposition", "attachment;filename=DataReport.xls"); //.xls的文件名可修改
        Response.ContentEncoding = System.Text.Encoding.UTF7;
        Response.ContentType = "application/ms-excel"; //设置输出文件类型为excel文件。   
        System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
        gvInfo.RenderControl(oHtmlTextWriter);
        Response.Output.Write(oStringWriter.ToString());
        Response.Flush();
        Response.End();
        gvInfo.AllowSorting = true; //恢复分页 GridView1.AllowPaging = true; //恢复排序
        GetInfo(); //再次绑定
    } --------------------编程问答-------------------- 各位亲,帮帮忙了 --------------------编程问答-------------------- 用public 全局变量统计,然后直接在footer用<%= 全局变量 %>打印 --------------------编程问答-------------------- Refer:

http://www.cnblogs.com/insus/articles/1433589.html --------------------编程问答-------------------- 除
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,