REW.C
2019-06-12 10:01GridView控件RowUpdating问题。
为什么控件编辑状态下点击更新就会出现索引超出范围的错误?
后台代码如下:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string strID = GridView1.DataKeys[e.RowIndex].Value.ToString();
string strNAME = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
string strCS = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
string strMATH = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
string strENG = ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text;
string strSQL = "UPDATE SCORES SET NAME='" + strNAME + "',SCORE_CS='" + strCS + "',SCORE_MATH='" + strMATH + "',SCORES_ENG='" + strENG + "' WHERE ID=" + strID;
conn = new SqlConnection(WebConfigurationManager.AppSettings["conStr"]);
conn.Open();
cmd = new SqlCommand(strSQL, conn);
cmd.ExecuteNonQuery();
cmd.Dispose();
conn.Close();
GridView1.EditIndex = -1;
GridViewBind();
}
protected void GridViewBind()
{
string str_con = WebConfigurationManager.AppSettings["conStr"];
conn = new SqlConnection(str_con);
conn.Open();
sda = new SqlDataAdapter("SELECT * FROM SCORES", conn);
sda.Fill(ds, "ds_SCORES");
conn.Close();
GridView1.DataSource = ds.Tables["ds_SCORES"];
GridView1.DataBind();
}
补充:GridView的主键DataKeyNames=也是设置了的。
另外,试了下在RowUpdating方法中输出GridView1.DataKeys.Count和GridView1.Rows.Count,发现都是0。
- 点赞
- 回答
- 收藏
- 复制链接分享
0条回答
为你推荐
- gridview 放在ContentPlaceHolder下,不能更新
- gridview
- contentplaceholder
- 2个回答
- 其他信息: 未将对象引用设置到对象的实例。
- gridview
- asp.net
- c#
- 5个回答