I have a gridview that has and auto- generated delete button. The grid has one datakeyname and for some reason it I am getting this error : Message: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
I have looked at many tutorials and examples. This code should work right?
protected void grdBins_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int rec_id = int.Parse(grdBins.DataKeys[e.RowIndex].Value.ToString());
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "delete from t_run_schedule_lots " +
"where rec_id = @id";
cmd.Parameters.Add("@id", SqlDbType.Int).Value = rec_id ;
cmd.CommandType = CommandType.Text;
cmd.Connection = this.sqlConnection1;
this.sqlConnection1.Open();
//execute insert statement
cmd.ExecuteNonQuery();
this.sqlConnection1.Close();
//re-populate grid */
fill_grid();
grdBins.EditIndex = -1;
grdBins.DataBind();
// this bit was just to see if I was capturing the ID field properly.
lblBins.Visible = true;
lblBins.Text = rec_id.ToString();
}
If anyone knows a good example in C# that would make this work it would be greatly appreciated.
Here is what I did to get it working:
Although I don’t know why the RowIndex option would not work. Anyway it is working now.