Ciao a tutti,

da cosa può dipendere questo errore dopo il delete di un record da un
datagrid

Index was out of range. Must be non-negative and less than the size of the
collection. Parameter name: index
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.ArgumentOutOfRangeException: Index was out of
range. Must be non-negative and less than the size of the collection.
Parameter name: index

Source Error:


Line 239: {
Line 240:
Line 241: int IDRiga =(int)dgCarrello.DataKeys[(int)E.Item.ItemIndex];
Line 242:
Line 243: OleDbCommand myCommand = new OleDbCommand("SP_Carrello_Delete",
myConnection);





<asp:datagrid id="dgCarrello" runat="server" HorizontalAlign="Center"
AutoGenerateColumns="False"
OnDeleteCommand="dgCarrello_Delete" OnSortCommand="Sort_Grid"
OnPageIndexChanged="dgCarrello_PageIndexChanged"
DataKeyField="ID" PagerStyle-Position="TopAndBottom"
PagerStyle-HorizontalAlign="Center" PagerStyle-Mode="NextPrev"
AllowSorting="True" CellSpacing="1" CellPadding="3" BackColor="White"
BorderColor="White"
BorderWidth="2px" GridLines="None" BorderStyle="Ridge" PageSize="1"
Width="600px">








protected void dgCarrello_Delete(Object sender, DataGridCommandEventArgs
E)
{
int IDRiga =(int)dgCarrello.DataKeys[(int)E.Item.ItemIndex];

OleDbCommand myCommand = new OleDbCommand("SP_Carrello_Delete",
myConnection);
myCommand.CommandType=CommandType.StoredProcedure;
myCommand.Parameters.Add(new OleDbParameter("@IDRiga", IDRiga));

try
{
myConnection.Open();
myCommand.ExecuteNonQuery();
myConnection.Close();

lblErrore.Text = "Elemento cancellato correttamente!";
dgCarrello.EditItemIndex = -1;
BindGrid();
}
.....






private void Page_Load(object sender, System.EventArgs e)
{
....


if(!IsPostBack)
{
BindGrid();
}
....

}



protected void BindGrid()
{

// creo il DataSet per il DataGrid
GetData();


// Imposto l'ordinamento presente nel ViewState
if (ViewState["Order"] != null)
{
DsDati.Tables["Carrello"].DefaultView.Sort =
ViewState["Order"].ToString();
}



// Imposto il filtro presente nel ViewState
if (ViewState["LastFilter"] != null)
{
DsDati.Tables["Carrello"].DefaultView.RowFilter =
ViewState["LastFilter"].ToString();
}


dgCarrello.DataSource = DsDati.Tables["Carrello"].DefaultView;
dgCarrello.DataBind();



NumeroRecords = DsDati.Tables["Carrello"].DefaultView.Count;
ElencoRecords.Text = NumeroRecords.ToString();

}







private void GetData()
{

try
{

DsDati = new DataSet();

OleDbCommand myCommand = new OleDbCommand("SP_Carrello", myConnection);
myCommand.Parameters.Add(new OleDbParameter("@CodiceUtente",
....
OleDbDataAdapter DataAdapt = new OleDbDataAdapter(myCommand);

DataAdapt.Fill(DsDati, "Carrello");

myConnection.Close();


}
}