In. when you use the GridView control to edit data online, the "Index was out of range. must be non-negative and less than the size of the collection. parameter name: index "I have checked a lot of questions about the incorrect data index value on the Internet. I don't feel like there are any articles that direct point out the solutions to the problem. Let's summarize them first.
In fact, this problem requires great attention during operations. It is not in the RowEditing or RowUpdating method of the GridView control, but needs to specify the primary key of the GridView control in the class for obtaining data, the background code is as follows:
Page_Load( (! str_conn = ConfigurationManager.AppSettings[ SqlConnection myConn = SqlConnection myConn = sql_Str = SqlDataAdapter myda = DataSet myDs = GridView1.DataSource = GridView1_RowEditing( GridView1.EditIndex = GridView1_RowUpdating( stuNum = newName = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[].Controls[ SqlConnection myConn = sql_str = SqlCommand myCmd = myCmd.Parameters.Add(, SqlDbType.NVarChar).Value = myCmd.Parameters.Add(, SqlDbType.VarChar).Value = (myCmd.ExecuteNonQuery() > Response.Write( Response.Write( textbox1.Text = + stuNum + + newName + + sql_str + + GridView1.EditIndex = - GridView1_RowsCancelingEdit( GridView1.EditIndex = - }
135 }
136 }
As for the front-end page, it is nothing more than binding related events. No code will be posted here.