GridView 编辑功能 中的 "更新" 问题 请高手帮助!! 在线等!!@
这是c#中 更新的代码: (不知道哪错了)
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
String sqlstr, id, GridView1,rs1,rs2,rs3;
int i;
TextBox obj1, obj2, obj3;
i = GridView1.DataKeys[e.Item.ItemIndex];
obj1 = (TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0];
obj2 = (TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0];
obj3 = (TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0];
rs1 = obj1.Text;
rs2 = obj2.Text;
rs3 = obj3.Text;
id = GridView1.DataKeys[i].ToString();
myCon = new SqlConnection(strCon);
string sqlstr = "update data set Name= ' "+ rs3 + " ',Title= ' "+ rs2 + " ',Content= ' "+ rs1 + " ' where id= "+ id ;
SqlCommand rs = new SqlCommand(sqlstr, myCon);
myCon.Open();
rs.ExecuteNonQuery();
myCon.Close();
GridView1.EditItemIndex = -1;
bind();
}
------解决方案--------------------错了饿..是 e.Keys[ "字段名 "].ToString();
------解决方案--------------------id取值不用那么麻烦的,直接就能取到了,用下面的代码试试看
int id = e.RowIndex + 1;
string rs3 = ((TextBox)this.GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
string rs2 = ((TextBox)this.GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
string rs1 = ((TextBox)this.GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
myCon = new SqlConnection(strCon);
myCon.Open();
SqlCommand cmd = new SqlCommand( "update data set name= ' " + rs3 + " ',Title ' " + rs2 + " ',Content= ' " + rs1 + " ' where id= ' " + Convert.ToString(id) + " ' ", con);
cmd.ExecuteNonQuery();
Response.Write( "ok ");
------解决方案--------------------Jiaowei85() ,您的回言:int id = e.RowIndex