private void DataGrid1_DeleteCommand(objectsource,System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string strDEL="Delete FROM worksort WHERE id=" + DataGrid1.DataKeys[(int)e.Item.ItemIndex];
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath("/TouchScreen/TouchScreen.mdb");

OleDbConnection conn = new OleDbConnection(strConn);
OleDbCommand myCommand = new OleDbCommand (strDEL,conn);  try
{
conn.Open();
myCommand.ExecuteNonQuery();
conn.Close();
}
catch(Exception ee)
{
throw ee;
}
Bind();
}
上面的代码到底时哪里错了,删除的时候老是报错!我连的是ACCESS!“索引超出范围。必须为非负值并小于集合大小。参数名: index ”
错误行:string strDEL="Delete FROM worksort WHERE id=" + DataGrid1.DataKeys[((int)e.Item.ItemIndex + 1)];

解决方案 »

  1.   

    DataGrid1.DataKeys[(int)e.Item.ItemIndex];

    DataGrid1.DataKeys[((int)e.Item.ItemIndex + 1)];怎么前后不一样?
      

  2.   

    TO lxcc:
    不好意思,这个是手误。前后是一样的,都是DataGrid1.DataKeys[(int)e.Item.ItemIndex];。
      

  3.   

    找到原因了,现在可以删除了,不过现在又出现了个问题,我用的是DataGrid自动分页,当我删除当前分页的最后一条记录的时候又报错了:无效的 CurrentPageIndex 值。它必须大于等于 0 且小于 PageCount。行 52:DataGrid1.DataBind();
    以下是数据绑定时用的方法:
    private void Bind()
    { string strConn = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath("/TouchScreen/TouchScreen.mdb");
    OleDbConnection conn = new OleDbConnection(strConn);
    string strSQL = "SELECT id,title,left(content,40) as content FROM meetguide";
    conn.Open();
    OleDbDataAdapter myAda = new OleDbDataAdapter(strSQL,conn);
    DataSet myDataSet = new DataSet();
    myAda.Fill(myDataSet); DataGrid1.VirtualItemCount = myDataSet.Tables[0].Rows.Count;
    DataGrid1.DataSource = myDataSet.Tables[0].DefaultView;
    DataGrid1.DataBind(); 《--报错行
    conn.Close();
    }不知哪里出了错?
      

  4.   

    这个我以前遇到~你在帮定前现把CurrentPageIndex属性赋值为1,最好先写个判断,看看CurrentPageIndex是否大于PageCount
      

  5.   


    DataGrid1.DataSource = myDataSet.Tables[0].DefaultView;DataGrid1.CurrentPageIndex=1;
    DataGrid1.DataBind(); 《--报错行
    不过在第一次绑定前DataGrid1.CurrentPageIndex=1;可能会出错
    你可以在删除执行后接着执行DataGrid1.CurrentPageIndex=1;