When we use RowUpdating of the GridView to modify data today, we always start to prompt that the Index is out of the range. We checked the DataKeyNames settings repeatedly and found that it only sets one field, this error is returned because it has never been too explicit about the use of DataKeyNames. Originally, I only set the "UserID" field for DataKeyNames, but the code below is as follows:
string UserID=GridView1.DataKeys[e.RowIndex].Values[0].ToString();string Username=GridView1.DataKeys[e.RowIndex].Values[1].ToString();string Department=GridView1.DataKeys[e.RowIndex].Values[2].ToString();string Company=GridView1.DataKeys[e.RowIndex].Values[3].ToString();
In this way, because it only sets the UserID field, it will naturally go wrong in the second row, that is, although there are four columns in the GridView, UserID, Username, Department, Company, however, there is only one UserID column in The DataKeys set.
So I changed my mind. How should I get the value of other columns that are not the primary key column or are not in DataKeyNames so as to put it into the update statement? For beginners like me, this is actually a problem when some properties and methods of the GridView are not very familiar.
As a result, Baidu and Google have published many previous articles on the Internet, and there are endless answers. Many of them are about to display and convert the corresponding column into a Label.) GridView1 ....., dataKeys [e. rowIndex], Rows [e. rowIndex], DataKeys obviously cannot be used now, so I think we should use Rows. But how can we use the Rows set to represent the values of other columns in the row? After some searches, coupled with the habit of using Response. Write to try to output values using ClassicASP), the final result is in the following format:
string Username=((TextBox)this.GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;string Department=((TextBox)this.GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;string Company=((TextBox)this.GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
Because I didn't use any templates, I only use the simplest and default BoundField, so I take it for granted, do I have to verify it later?) I want to convert it to TextBox. this is finally a success.
In the whole process, I thought it was the first line.StringUserID = GridView1.DataKeys [e. rowIndex]. values [0]. toString ();) problem, made for a long time, and later through Response. write to output the value of the first column. It turns out that the first row can correctly output the current UserID value, but none of the following three columns will work. So I found that some old habits are sometimes very helpful.
The front-end GridView code is as follows:
<asp:GridView ID="GridView1" runat="server" Width="70%" CellPadding="4" ForeColor="#333333"AutoGenerateColumns="False" AllowPaging="True" PageSize="20"OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating"DataKeyNames="UserID" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowDataBound="GridView1_RowDataBound"GridLines="None"><Columns><asp:BoundField HeaderText="User ID" DataField="UserID" /><asp:BoundField HeaderText="User Name" DataField="Username" /><asp:BoundField HeaderText="Department" DataField="Department" /><asp:BoundField HeaderText="Division" DataField="Division" /><asp:CommandField HeaderText="OPT" ShowDeleteButton="True" ShowEditButton="True" /></Columns>
The RowDating code is as follows:
Protected void GridView1_RowUpdating (object sender, GridViewUpdateEventArgs e) {// Response. write (GridView1.DataKeys [e. rowIndex]. values [0]. toString () + "<br/>"); // The problem is detected only through these two lines. // Response. write (TextBox) this. gridView1.Rows [e. rowIndex]. cells [1]. controls [0]). text); string UserID = GridView1.DataKeys [e. rowIndex]. values [0]. toString (); string Username = (TextBox) this. gridView1.Rows [e. rowIndex]. cells [1]. controls [0]). text; string Department = (TextBox) this. gridView1.Rows [e. rowIndex]. cells [2]. controls [0]). text; string Division = (TextBox) this. gridView1.Rows [e. rowIndex]. cells [3]. controls [0]). text; string connstr = "Server = Server; UID = username; PWD = password; Database = test ;"; string sqlstr = "UPDATE UserInfo SET Username = '" + Username + "', Department = '" + Department + "', company = '"+ Company +" 'where UserID =' "+ UserID +" '"; try {SqlConnection conn = new SqlConnection (connstr); if (conn. state. toString () = "Closed") conn. open (); SqlCommand comm = new SqlCommand (sqlstr, conn); comm. executeNonQuery (); comm. dispose (); if (conn. state. toString () = "Open") conn. close (); GridView1.EditIndex =-1; GridViewBind ();} catch (Exception ex) {Response. write ("Database Error, Reason:" + ex. message );}}
Please note it down for your reference.