gridview删除行时报错:索引超出范围。必须为非负值并小于集合大小。参数名: inde

来源:百度知道 编辑:UC知道 时间:2024/07/09 04:13:15
如题 一删除行时就出这个错误 别的页面就没事
代码如下 请各位大大帮忙解决下

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{

string strid = this.GridView1.DataKeys[e.RowIndex].Values.ToString();
int delPerson = new DBoperate().ExecuteUpdate("delete from FlowStepPerson where StepID=" + strid);
int result = new DBoperate().ExecuteUpdate("delete from FlowStep where ID=" + strid);
if (result > 0)
{
Response.Write("<script language=javascrip>alert('删除成功!')</script>");
}
else
{
Response.Write("<script language=javascrip>alert('删除失败!')</script>");
}
}

问题解决了可以加分 ^_^

string strid = this.GridView1.DataKeys[e.RowIndex].Values.ToString();
你必须在gridview的属性里设置 DataKeyNames 项,这样才有DateKey键,而且得带上DataKey的索引,比如:
this.GridView1.DataKeys[e.RowIndex].Values[0].ToString();

打断点看是否取到了ID值

rowindex是索引值吧 strid不一定是从0开始吧 可能删除了某行 但是strid不会重新排列 也就是说 rowindex不一定=strid