<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
                            AutoGenerateColumns="False" HeaderStyle-VerticalAlign="Middle" CellPadding="4"
                            Font-Size="9pt"
                            Height="100%" Width="100%" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDataBound="GridView1_RowDataBound" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnRowDeleting="GridView1_RowDeleting"  GridLines="Horizontal" BackColor="White" BorderColor="#336666" BorderStyle="Double" BorderWidth="3px" >
                            <Columns>            
                                <asp:BoundField DataField="piece" HeaderText="数量" >
                                 </asp:BoundField>
                                  <asp:BoundField DataField="zmt" HeaderText="总价" >
                                 </asp:BoundField>
                                                           </Columns>
问题1、如何把数量列的总和计算并显示出来呢?
问题2、如何把总价列如何显示=数量列的值*用户输入的数量

解决方案 »

  1.   

    直接用select 语句完成,select sum()..
      

  2.   


    private double totalprice = 0;//总价protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
            if (e.Row.RowIndex >= 0)
            {
                totalprice += Convert.ToDouble(e.Row.Cells[1].Text);
            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                e.Row.Cells[0].Text = "合计:";
                e.Row.Cells[1].Text = totalprice.ToString("#0.00");
                
            } 
            
    }
    showfooter=true;
      

  3.   

    select 数量列 as 列1,数量列*用户输入的数量 as 列2 from table
    union
    select sum(数量列) as 列1,sum(数量列*用户输入的数量) as 列1 from table
      

  4.   

    查询出的结果作为你gridview的数据源
      

  5.   

    由于总和这个字段可能不在girdview内显示
            //RowDataBound事件中。。
            lblAmount.Text = DataBinder.Eval(e.Row.DataItem, "Amount").ToString();
      

  6.   


    哥们你求的是总和吗?报错:异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误: 
    行 131:        }
    行 132:
    行 133:        Label1.Text = DataBinder.Eval(e.Row.DataItem, "piece").ToString();
    行 134:
    行 135:
     源文件: d:\cpan\Inetpub\fangan\改版2\sjorder\snet\kaichuang\Update.aspx.cs    行: 133 
      

  7.   


     int count=0;
     count = Convert.ToInt32(txtNum.Text());//这个是你输入的数量
     double piece= 0;
     double zmt= 0;
     // 数量列总和
     foreach (GridViewRow gvr in GridView1.Rows)
     {
         if(null != gvr.Cells[i].Text)
         {
              piece+= Convert.ToDouble(gvr.Cells[i].Text);//i是price所在的列,从0开始
          }
     }
     // 每行的总价
     foreach (GridViewRow gvr in GridView1.Rows)
     {
         if(null != gvr.Cells[i].Text)
         {
              gvr.cell[n].Text = Convert.ToDouble(gvr.Cells[i].Text) * count;
              //i是price所在的列,从0开始 n是总价所在的列      
          }
         else
         {
              gvr.cell[n].Text = "0";
         }
     }
    不知道能不能出来
      

  8.   

    Convert.ToInt32(txtNum.Text());这个怎么对应到GridView的编辑列? 
      

  9.   

    这是我以前发的一个帖子,你可以参考下
    http://topic.csdn.net/u/20090909/22/40ba837f-b68c-4e3f-aa2e-3a5ddf9c5fd9.html
      

  10.   

    你gridview不是一共两列吗?这个是从页面输入的,你要是从gridview里面有数量的列,就取出数量这一列,乘以价格就行了
      

  11.   

    我的前台是这个
         <asp:BoundField DataField="piece"   HeaderText="数量" >
                                     </asp:BoundField>
    点击编辑时,数量变为可编辑状态,如何把这个时候输入的值,对应你的这个代码呢?谢谢count= Convert.ToInt32(txtNum.Text());//这个是你输入的数量
      

  12.   


     double piece= 0;
     double zmt= 0;
     // 数量列总和
     foreach (GridViewRow gvr in GridView1.Rows)
     {
         if(null != gvr.Cells[i].Text)
         {
              piece+= Convert.ToDouble(gvr.Cells[i].Text);//i是price所在的列,从0开始
          }
     }
     // 每行的总价
     foreach (GridViewRow gvr in GridView1.Rows)
     {
         if(null != gvr.Cells[i].Text && null != gvr.Cell[k].Text)
         {
              gvr.cell[n].Text = Convert.ToDouble(gvr.Cells[i].Text) * Convert.ToDouble(gvr.Cell[k].Text);
              //i是price所在的列,从0开始 n是总价所在的列,k是点击编辑的时候那一列   
               //gvr.Cell[k].Text 里面的内容自己验证,只能是整数(根据你项目的要求)
          }
         else
         {
              gvr.cell[n].Text = "0";
         }
     }
      

  13.   

    用sql语句对你要汇总的列进行查询汇总就可以了,然后附值给一个label