I have a GridView within a LoginView that I am running into this issue when I click the edit button. I know that I need to use FindControl to get to the objects within the LoginView so I have done that. But now I am having this IndexOutOfRangeException error. I posted the index and the DataKeys.Count to an error label and I don't see the issue. But it comes up in the xx.Rows[e.RowIndex] only. If I only use e.RowIndex (for example: int index = e.RowIndex.ToString();) I won't get the same error. So I have it narrowed down to that but cannot figure it out from here. If you need for me to post any other parts of the code please let me know.
The line giving me fits is the GridViewRow row = gv.Rows[e.RowIndex]; line.
protected void OnRowDeleting(object sender, GridViewDeleteEventArgs e)
{
GridView gv = ReviewLoginView.FindControl("gvReview") as GridView;
if (gv.DataKeys.Count > e.RowIndex)
{
GridViewRow row = gv.Rows[e.RowIndex];
string Id = (row.FindControl("lblID") as Label).Text;
string constr = System.Configuration.ConfigurationManager.AppSettings["DefaultConnection"];
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("cardReview"))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@id", Id);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
gv.EditIndex = -1;
this.BindGrid();
}
}