用union,例如sql语句:select 字段1,字段2 from yourTable where ...
union 
select sum(字段1),sum(字段2) from yourTable where ...

解决方案 »

  1.   

    private double sum = 0;//取指定列的数据和,你要根据具体情况对待可能你要处理的是int
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            
            if (e.Row.RowIndex >= 0)
            {
                sum += Convert.ToDouble(e.Row.Cells[6].Text);
            }
            else if (e.Row.RowType == DataControlRowType.Footer)
            {
                e.Row.Cells[5].Text = "总薪水为:";
                e.Row.Cells[6].Text = sum.ToString();
                e.Row.Cells[3].Text = "平均薪水为:";
                e.Row.Cells[4].Text = ((int)(sum / GridView1.Rows.Count)).ToString();
                
            }
        }后台全部代码:
    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;
    using System.Drawing;public partial class Default7 : System.Web.UI.Page
    {
        SqlConnection sqlcon;
        SqlCommand sqlcom;
        string strCon = "Data Source=(local);Database=北风贸易;Uid=sa;Pwd=sa";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bind();        }
        }
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            bind();
        }
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            sqlcon = new SqlConnection(strCon);
            string sqlstr = "update 飞狐工作室 set 姓名='"
                + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',家庭住址='"
                + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "' where 身份证号码='"
                + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
            sqlcom = new SqlCommand(sqlstr, sqlcon);
            sqlcon.Open();
            sqlcom.ExecuteNonQuery();
            sqlcon.Close();
            GridView1.EditIndex = -1;
            bind();
        }
        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            bind();
        }
        public void bind()
        {
            string sqlstr = "select top 5 * from 飞狐工作室";
            sqlcon = new SqlConnection(strCon);
            SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
            DataSet myds = new DataSet();
            sqlcon.Open();
            myda.Fill(myds, "飞狐工作室");
            GridView1.DataSource = myds;
            GridView1.DataKeyNames = new string[] { "身份证号码" };
            GridView1.DataBind();
            sqlcon.Close();
        }
        private double sum = 0;//取指定列的数据和
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            
            if (e.Row.RowIndex >= 0)
            {
                sum += Convert.ToDouble(e.Row.Cells[6].Text);
            }
            else if (e.Row.RowType == DataControlRowType.Footer)
            {
                e.Row.Cells[5].Text = "总薪水为:";
                e.Row.Cells[6].Text = sum.ToString();
                e.Row.Cells[3].Text = "平均薪水为:";
                e.Row.Cells[4].Text = ((int)(sum / GridView1.Rows.Count)).ToString();
                
            }
        }
    }
    前台:唯一的花头就是设置ShowFooter="True" ,否则默认表头为隐藏的!
    <asp:GridView ID="GridView1" runat="server"    AutoGenerateColumns="False" CellPadding="3" OnRowEditing="GridView1_RowEditing"
                            OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" Font-Size="12px" OnRowDataBound="GridView1_RowDataBound" ShowFooter="True" >
                            <FooterStyle BackColor="White" ForeColor="#000066" />
                            <Columns>
                                <asp:CommandField HeaderText="编辑" ShowEditButton="True" />
                                <asp:BoundField DataField="身份证号码" HeaderText="编号" ReadOnly="True" />
                                <asp:BoundField DataField="姓名" HeaderText="姓名" />
                                <asp:BoundField DataField="出生日期" HeaderText="邮政编码" />
                                <asp:BoundField DataField="家庭住址" HeaderText="家庭住址" />
                                <asp:BoundField DataField="邮政编码" HeaderText="邮政编码" />
                                <asp:BoundField DataField="起薪" HeaderText="起薪" />
                               
                            </Columns>
                            <RowStyle ForeColor="#000066" />
                            <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
                            <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" CssClass="ms-formlabel DataGridFixedHeader"/>
                            <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
                        </asp:GridView>
      

  2.   

    select 字段1,字段2 from yourTable where ...
    union 
    select sum(字段1),sum(字段2) from yourTable where ...
    完全可以
      

  3.   

    怎么统计?没看明白。 
    gridview的第38页的最后一行就是统计值!!
      

  4.   

            protected void GVdetail_RowDataBound(object sender, GridViewRowEventArgs e)
            {            if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    wastageqty += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "wastage_qty"));
                }
                else if (e.Row.RowType == DataControlRowType.Footer)
                {                // e.Row.Cells[5].Text = "单耗合计:";                     //在Footer里对单耗列汇总
                    e.Row.Cells[6].Text = wastageqty.ToString();
                    wastageqty = 0;            }
            }
      

  5.   

      或者:
          protected string Sumqty()
            {
                return dtd.Compute("sum(mat_qty)", "").ToString();
            }
    dtd是你gridvivew绑定和datatable,mat_qty是列名
      

  6.   

    其实涉及界面控件的最好以xhtml设计代码中声明为好,而后台代码中应该仅仅提供数据而并不关心使用它的控件,这样把界面模型和数据模型分开。因此,与其在GVdetail_RowDataBound 去查找控件位置(通常,我不允许想当然地去假设一个控件一定在第6列或者第7列,那样给界面调整制造的更多麻烦)然后再去调用后台数据模型性并且去设置控件的值,不如在将界面上简单地声明一下与数据模型的关系即可。在首列,你可以在模板中插入:<FooterTemplate>
        合计:
    </FooterTemplate>而在相应的数据列的模板插入:<FooterTemplate>
        <asp:Label runat="server" Text="<%# this.Sumqty() %>" />
    </FooterTemplate>如果有多个数据列都要统计,同时你要仅访问后台数据一次,那么可能需要在 DataBinding 事件中先进行统计,将结果写入一个声明为private DataRow的变量(或者Dictionary<string object>等等)保存每一个列名称与统计值的对应数据,而在Sumqty方法中提供一个列名称来查询,在界面设计声明时可以直接写::<FooterTemplate>
        <asp:Label runat="server" Text='<%# this.Sumqty("fld_name") %>' />
    </FooterTemplate>
    使用声明风格,尽量少写代码。
      

  7.   

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
     {
         if (e.Row.RowIndex >= 0)
         {
             sum += Convert.ToDouble(e.Row.Cells[2].Text);
         }
        else if (e.Row.RowType == DataControlRowType.Footer)
         {
            e.Row.Cells[5].Text = "总和为:";
            e.Row.Cells[6].Text = sum.ToString();
            e.Row.Cells[3].Text = "平均值为:";
            e.Row.Cells[4].Text = ((int)(sum / GridView1.Rows.Count)).ToString();
        }
    }
      

  8.   

    俺是初学者,
    能问一下,Sumqty怎么定义?能给提供一个详细代码的定义吗?
      

  9.   

    楼主给你的代码有问题吗?
    是什么问题??
    select 字段1,字段2 from yourTable where ...
    union 
    select sum(字段1),sum(字段2) from yourTable where ...
      

  10.   

     protected void SqlDataSource1_Selecting1(object sender, SqlDataSourceSelectingEventArgs e)
        {
            e.Command.CommandText += " where 1 = 1 ";
            if (TextBox1.Text != "")
            {
                e.Command.CommandText += " and XIO like '%" + TextBox1.Text.Trim() + "%'";
            }
        }
    我绑定上面的代码,使用union不太方便。
      

  11.   

     protected void SqlDataSource1_Selecting1(object sender, SqlDataSourceSelectingEventArgs e)
        {
            e.Command.CommandText += " where 1 = 1 ";
            if (TextBox1.Text != "")
            {
                e.Command.CommandText += " and XIO like '%" + TextBox1.Text.Trim() + "%'";
            }
        }
    我绑定上面的代码,使用union不太方便。
      

  12.   

    楼主是用SqlDataSource
    SqlDataSource对于楼主的问题可以通过界面设置也可以用代码打入,例如:
    代码方式:
    SqlDataSource1.SelectCommand = "SELECT name, score1, score2  FROM Poll union select '总计',sum(score1),sum(score2)  FROM Poll";