GridView删除所选的时候报错
GridView删除所选的时候报错。详细描述:我在Gridview中添加复选框,然后便利他,如果选中则删除,
代码:
foreach (GridViewRow gv in SmartGridView1.Rows)
{
CheckBox CbDel = (CheckBox)gv.FindControl("CbChoose");
while (CbDel.Checked == true)
{
SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["JXPG"].ConnectionString);
cn.Open();
string sql = "DELETE FROM ZB_FIRST WHERE ID=@ID";
SqlCommand cm = new SqlCommand(sql, cn);
cm.Parameters.Add("@ID", SqlDbType.Int);
cm.Parameters["@ID"].Value = Convert.ToInt32(SmartGridView1.DataKeys[gv.RowIndex].Value);
cm.ExecuteNonQuery();
BindSgvView();
cn.Close();
执行后报错,但是实际上数据库已经被删除
报错代码:
“/web”应用程序中的服务器错误。
--------------------------------------------------------------------------------
索引超出范围。必须为非负值并小于集合大小。
参数名: index
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.ArgumentOutOfRangeException: 索引超出范围。必须为非负值并小于集合大小。
参数名: index
源错误:
行 145: SqlCommand cm = new SqlCommand(sql, cn);
行 146: cm.Parameters.Add("@ID", SqlDbType.Int);
行 147: cm.Parameters["@ID"].Value = Convert.ToInt32(SmartGridView1.DataKeys[gv.RowIndex].Value);
行 148: cm.ExecuteNonQuery();
行 149: BindSgvView();
--------------------编程问答-------------------- gv.RowIndex
应该是索引超出范围了。断点看啊 --------------------编程问答--------------------
SmartGridView1.DataKeys[gv.RowIndex]
这一句。。。。
补充:.NET技术 , ASP.NET