sqlcon = new SqlConnection(strCon);
        string sqlstr = "update User set UserID='"
        + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[0].Controls[0])).Text.ToString().Trim() + "',Uname='"
        + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[1])).Text.ToString().Trim() + "',PassWord='"
        + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[2])).Text.ToString().Trim() + "' where UserID='"
        + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
        sqlcom = new SqlCommand(sqlstr, sqlcon);
        sqlcon.Open();
        sqlcom.ExecuteNonQuery();
        sqlcon.Close();
        GridView1.EditIndex = -1;
        bind();
帮我看看啊!谢谢了!新手也不容易!!

解决方案 »

  1.   

    绑定GridView的时候是否指明了主键比如下面代码,用你的主键替代id
    GridView1.DataSource = ...;
    GridView1.DataKeys = new string[]{"id"};
    GridView1.DataBind();
      

  2.   

    GridView1.EditIndex = -1;
    是什么
      

  3.   

    我估计下面三个红色的都应该是0
    sqlcon = new SqlConnection(strCon);
      string sqlstr = "update User set UserID='"
      + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[0].Controls[0])).Text.ToString().Trim() + "',Uname='"
      + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[1])).Text.ToString().Trim() + "',PassWord='"
      + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[2])).Text.ToString().Trim() + "' where UserID='"
      + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
      sqlcom = new SqlCommand(sqlstr, sqlcon);
      sqlcon.Open();
      sqlcom.ExecuteNonQuery();
      sqlcon.Close();
      GridView1.EditIndex = -1;
      bind();
      

  4.   

    不是!这个我知道啊!就是Cells的遍历是从0开始啊!
      

  5.   

    Controls[0] Controls[1] Controls[2] 全改成 0