protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string Name=((TextBox)(this.GridView1.Rows[e.RowIndex].Cells[0].Controls[0])).Text.ToString();
        string Address=((TextBox)(this.GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString();
        string s = "select *  from ts1";
        SqlDataAdapter da = new SqlDataAdapter(s, conn);
        SqlCommandBuilder qc = new SqlCommandBuilder(da);
        DataSet ds = new DataSet();
        da.Fill(ds, "a");
        DataRow r=ds.Tables["a"].NewRow();
        r["name"] = Name;
        r["address"] = Address;
        ds.Tables["a"].Rows.Add(r);
        
        da.Update(ds,"a");
        this.GridView1.EditIndex = -1;
       this.show();
}
为什么不能更新  而是添加
谁能帮我该该

解决方案 »

  1.   

     DataRow r=ds.Tables["a"].NewRow(); 
            r["name"] = Name; 
            r["address"] = Address; 
            ds.Tables["a"].Rows.Add(r); 
             
            da.Update(ds,"a"); 上面全删了,改为:
     DataRow r=ds.Tables["a"].Rows[index];
     r["name"] = Name; 
     r["address"] = Address; 至于为什么是添加,因为你写了让他加啊DataRow r=ds.Tables["a"].NewRow(); ...  ds.Tables["a"].Rows.Add(r); 
    这代码是别人写的吧?
      

  2.   

    这都已经是 ds.Tables["a"].NewRow(); 
    当然是添加
      

  3.   

    index==this.GridView1.DataKeys[e.RowIndex].Value.ToString()吗?
      

  4.   

    按照你的意思应该是GridView当前编辑行的index.
      

  5.   

              string Name=((TextBox)(this.GridView1.Rows[e.RowIndex].Cells[0].Controls[0])).Text.ToString();
            string Address=((TextBox)(this.GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString();
            string s = "select *  from ts1";
            SqlDataAdapter da = new SqlDataAdapter(s, conn);
            SqlCommandBuilder qc = new SqlCommandBuilder(da);
            DataSet ds = new DataSet();
            da.Fill(ds);
            DataRow r = ds.Tables["a"].Rows[Convert.ToInt32(this.GridView1.DataKeys[e.RowIndex].Value)];
            r["name"] = Name;
            r["address"] = Address; 
            //da.Update(ds);
            this.GridView1.EditIndex = -1;
           this.show();
    还是不对啊 
      

  6.   

    DataRow r = ds.Tables["a"].Rows[第几行]
    这里是整数.你要更新第几行就填几.比如说要更新第一行1就写1.
    另外你要把更新完的DataTable绑到GridView1上才能在页面上看到更新效果.
      

  7.   

      string Name=((TextBox)(this.GridView1.Rows[e.RowIndex].Cells[0].Controls[0])).Text.ToString(); 
            string Address=((TextBox)(this.GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString(); 
            string s = "select *  from ts1"; 
            SqlDataAdapter da = new SqlDataAdapter(s, conn); 
            SqlCommandBuilder qc = new SqlCommandBuilder(da); 
            DataSet ds = new DataSet(); 
            da.Fill(ds); 
            DataRow r = ds.Tables["a"].Rows[Convert.ToInt32(this.GridView1.DataKeys[e.RowIndex].Value)]; 
            r["name"] = Name; 
            r["address"] = Address;  
            //da.Update(ds); 
            this.GridView1.EditIndex = -1; 
           this.show(); 
    更新的是下一行啊   this.GridView1.DataKeys[e.RowIndex].Value不是获得当前操作行的索引吗  更新哪行几是哪行吗
      

  8.   

    谢谢改为
    DataRow r = ds.Tables["a"].Rows[e.RowIndex];就对了