datagridview,想实现编辑数据的功能,结果编辑后,一更新,就报“指定的参数已超出有效值的范围。
参数名: index;”不是cell[]不对,断点加在roomno,一直进入string unitprice才跳到catch,但实际上 string roomno、string roomtype、string roomstatu、string unitprice 都是空的。 后面的是删除,点着页面就跳一下,数据还是没变
- C# code
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { try { int index = e.RowIndex;//当前选择行索引值 string roomno = GridView1.DataKeys[index].Value.ToString();//获取主键房间号 string roomtype = ((TextBox)(GridView1.Rows[index].Cells[4].Controls[0])).Text.Trim(); string roomstatu = ((TextBox)(GridView1.Rows[index].Cells[5].Controls[0])).Text.Trim(); string unitprice = ((TextBox)(GridView1.Rows[index].Cells[6].Controls[0])).Text.Trim(); string sql = "update room set roomtype="+roomtype+",roomstatu="+roomstatu+",unitprice="+unitprice+"";//更新语句 SqlConnection conn = new SqlConnection(strCon); conn.Open(); SqlCommand comm = new SqlCommand(sql, conn); int ret = comm.ExecuteNonQuery(); if (ret>0) { //更新成功 } else { //更新失败 } GridView1.EditIndex = -1; DataBind(); } catch (Exception ex) { throw; } } protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { int index = e.RowIndex; string roomno = GridView1.DataKeys[index].Value.ToString(); //rxxx ro = new rxxx(); //ro.roomno = roomno; //ro.Del(); string sql = "delete from room where roomno="+roomno+"";//更新语句 SqlConnection conn = new SqlConnection(strCon); conn.Open(); SqlCommand comm = new SqlCommand(sql, conn); conn.Close(); DataBind(); }
------解决方案--------------------------------------------------------
那不就是Rows[index].Cells[6] 超了嘛
另外 这个是gv吧