<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title>无标题页</title>
</head>
<body>
  <form id="form1" runat="server">
  <div>
  </div>
  <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"   
  DataKeyNames="ID" DataSourceID="SqlDataSource1"   
  onrowdatabound="GridView1_RowDataBound">
  <Columns>
  <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"   
  ReadOnly="True" SortExpression="ID" />
  <asp:BoundField DataField="name" HeaderText="name" SortExpression="name"   
  ReadOnly="True" />
  <asp:BoundField DataField="dj" HeaderText="dj" SortExpression="dj"   
  ReadOnly="True" />
  <asp:TemplateField HeaderText="sl" SortExpression="sl">
  <EditItemTemplate>
  <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("sl") %>'></asp:TextBox>
  </EditItemTemplate>
  <ItemTemplate>
  <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("sl") %>'></asp:TextBox>
  </ItemTemplate>
  </asp:TemplateField>
  <asp:TemplateField HeaderText="zj" SortExpression="zj">
  <ItemTemplate>
  <asp:Label ID="Label1" runat="server" Text='<%# Convert.ToInt32(Eval("dj").ToString().Trim())*Convert.ToInt32(Eval("sl").ToString().Trim()) %>'></asp:Label>
  </ItemTemplate>
  </asp:TemplateField>
  <asp:BoundField DataField="zj" HeaderText="zj" ReadOnly="True"   
  SortExpression="zj" />
  <asp:CommandField ShowEditButton="True" ShowDeleteButton="True" />
  </Columns>
  </asp:GridView>
  <asp:SqlDataSource ID="SqlDataSource1" runat="server"   
  ConnectionString="<%$ ConnectionStrings:wwConnectionString %>"   
  DeleteCommand="DELETE FROM [aa] WHERE [ID] = @ID"   
  InsertCommand="INSERT INTO [aa] ([name], [dj], [sl], [zj]) VALUES (@name, @dj, @sl, @zj)"   
  SelectCommand="SELECT * FROM [aa]"   
  UpdateCommand="UPDATE [aa] SET [sl] = @sl WHERE [ID] = @ID">
  <DeleteParameters>
  <asp:Parameter Name="ID" Type="Int32" />
  </DeleteParameters>
  <UpdateParameters>
  <asp:Parameter Name="sl" Type="String" />
  <asp:Parameter Name="ID" Type="Int32" />
  </UpdateParameters>
  <InsertParameters>
  <asp:Parameter Name="name" Type="String" />
  <asp:Parameter Name="dj" Type="String" />
  <asp:Parameter Name="sl" Type="String" />
  <asp:Parameter Name="zj" Type="String" />
  </InsertParameters>
  </asp:SqlDataSource>
  <asp:Label ID="Label2" runat="server" ></asp:Label>
  </form>
</body>
</html> private void aa()
  {
  int sum = 0;
  for (int i = 0; i < this.GridView1.Rows.Count; i++)
  {
  try
  {
  sum += Convert.ToInt32(this.GridView1.Rows[i].Cells[4].Text.ToString().Trim());
  }
  catch
  {
  continue;
  }
  }
  Label2.Text = sum.ToString();
  }调试的时候,竖列得出的是 0 ,横列DJ乘以SL已经求出来了。因为我加了两咧ZJ字段,一列是BoundField,一列是TemplateField。为什么转换成TemplateField字段后计算不出来呢?如果sum += Convert.ToInt32(this.GridView1.Rows[i].Cells[4].Text.ToString().Trim());这句的[4] 换成[5] 就会出现错误:“输入字符串的格式不正确。”请帮我看看,谢谢了,因为我是自学的。对着本书学习,有些问题找不出来,只能上网论坛问老师们了。希望能给我代码。谢谢QQ:695496434

解决方案 »

  1.   

    本帖最后由 net_lover 于 2012-04-05 13:20:02 编辑
      

  2.   

    Cells[4]换成5就会出错,那一定是Cells[5]的内容不能够给转为int,你断点看看5的值是什么
      

  3.   

    http://blog.csdn.net/21aspnet/article/details/1540301 参考 17.GridView加入自动求和求平均值小计
      

  4.   

    4换成5不正确,肯定是Cells[5]中的内容不能转换为int类型
      

  5.   

    我给你一个竖列计算和:
    在RowDataBound事件中写
      //自动求和计算。
            public decimal item_num = 0;  //定义计算商品总数20
            public decimal item_volumn = 0; //定义商品体积21
            public decimal item_Wight = 0;  //定义商品重量22
            public int Rowcount = 0;
            protected void GridView_Order_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowIndex >= 0)
                {
                    item_num += Convert.ToDecimal(e.Row.Cells[20].Text);
                    item_volumn += Convert.ToDecimal(e.Row.Cells[21].Text);
                    item_Wight += Convert.ToDecimal(e.Row.Cells[22].Text);
                    Rowcount = e.Row.RowIndex + 1;
                }
                else if (e.Row.RowType == DataControlRowType.Footer)
                {
                    e.Row.Cells[1].Text = "总订单数:";
                    e.Row.Cells[2].Text = Rowcount.ToString();
                    e.Row.Cells[3].Text = "商品总件数:";
                    e.Row.Cells[4].Text = item_num.ToString();
                    e.Row.Cells[5].Text = "商品总体积:";
                    e.Row.Cells[6].Text = item_volumn.ToString();
                    e.Row.Cells[7].Text = "商品总重量:";
                    e.Row.Cells[8].Text = item_Wight.ToString();
                }
            }
      

  6.   

    private void aa()
      {
      int sum = 0;
      for (int i = 0; i < this.GridView1.Rows.Count; i++)
      {
      try
      {
       Label x = this.GridView1.Rows[i].FindControl("Label1") as Label;
       if(x!=null) sum += Convert.ToInt32( x.Text.ToString().Trim());
      }
      catch
      {
      continue;
      }
      }
      Label2.Text = sum.ToString();
      }
      

  7.   


    TextBox xxxx = this.GridView1.Rows[i].FindControl("TextBox1") as TextBox;
    int bbbbbbb=int.Parse( xxxx.Text.ToString().Trim()); 
    sum += bbbbbbb;