我照这里的一片文章写了用checkbox删除datagrid中的记录的一段程序,确老是报错,那位高手帮忙解决一下?
七七&小鱼 2004-05-06 09:04:54 private DataTable dt=new DataTable();
private DataSet ds = new DataSet();
private void Button1_Click(object sender, System.EventArgs e)
{int i=0;int ii;
CheckBox cb;
foreach(DataGridItem dgi in this.DataGrid1.Items)
{
cb = (CheckBox) dgi.Cells[0].Controls[1];
if(cb.Checked==true)
{
ii=(int)DataGrid1.DataKeys[i];
ds.Tables["clientt"].Rows.Remove(this.ds.Tables["clientt"].Rows.Find(ii));
}
i++;
}
string con=ConfigurationSettings.AppSettings["DSN"];
SqlConnection myConnection = new SqlConnection(con);
SqlDataAdapter myCommand = new SqlDataAdapter("select * from clientt", myConnection);
myCommand.Update(ds, "clientt");
myCommand.Fill(ds, "clientt");
DataGrid1.DataBind();
}
就是点一下button删除选中的记录,不知道这样对不对?
报错的是
索引超出范围。必须为非负值并小于集合大小。参数名: index
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.ArgumentOutOfRangeException: 索引超出范围。必须为非负值并小于集合大小。参数名: index
源错误:
行 103: if(cb.Checked==true)
行 104: {
行 105: ii=(int)DataGrid1.DataKeys[i];
行 106: ds.Tables["clientt"].Rows.Remove(this.ds.Tables["clientt"].Rows.Find(ii));
行 107: }