title: while editing gridview i get null datakeys as well as null new values
body: <p>I have aspx code of grid view as follows..</p>
  
  <p><strong>ASPX</strong>:        </p>
  
  <pre><code>&lt;asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                      AutoGenerateEditButton="True" BackColor="White" BorderColor="#CC9966" 
                      BorderStyle="None" BorderWidth="1px" CellPadding="4" 
                      DataKeyNames="Machine no." EnableModelValidation="True" 
                      onrowcancelingedit="GridView1_RowCancelingEdit" 
                      onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating"&gt;
                      &lt;Columns&gt;
                          &lt;asp:BoundField DataField="Machine no." HeaderText="Machine no." 
                              ReadOnly="True" SortExpression="Machine no." /&gt;
                          &lt;asp:BoundField DataField="Machine Name" HeaderText="Machine Name" 
                              SortExpression="Machine Name" /&gt;
                          &lt;asp:TemplateField HeaderText="Is active?" SortExpression="Is active?"&gt;
                              &lt;ItemTemplate&gt;
                                  &lt;asp:Label ID="Label1" runat="server" Text='&lt;%# Bind("[Is active?]") %&gt;'&gt;&lt;/asp:Label&gt;
                              &lt;/ItemTemplate&gt;
                              &lt;EditItemTemplate&gt;
                                  &lt;asp:DropDownList ID="DropDownList1" runat="server" 
                                      SelectedValue='&lt;%# Bind("[Is active?]") %&gt;'&gt;
                                      &lt;asp:ListItem Value="0"&gt;Inactive&lt;/asp:ListItem&gt;
                                      &lt;asp:ListItem Value="1"&gt;Active&lt;/asp:ListItem&gt;
                                  &lt;/asp:DropDownList&gt;
                              &lt;/EditItemTemplate&gt;
                          &lt;/asp:TemplateField&gt;
                      &lt;/Columns&gt;
                      &lt;FooterStyle BackColor="#FFFFCC" ForeColor="#330099" /&gt;
                      &lt;HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" /&gt;
                      &lt;PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" /&gt;
                      &lt;RowStyle BackColor="White" ForeColor="#330099" /&gt;
                      &lt;SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" /&gt;
                  &lt;/asp:GridView&gt;
  </code></pre>
  
  <p>I have defined the logic in code behind as follows..</p>
  
  <p><strong>CodeBehind</strong>:</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
  {
       if (!Page.IsPostBack)
       {
            bindgrid();
       }
  
  }
  void bindgrid()
  {
       string q = "SELECT machineno AS 'Machine no.',machinename AS 'Machine Name',active AS 'Is active?' FROM machinedetails";
       DataTable dt = dtu.table(q, out error);
       GridView1.DataSource = dt;
       GridView1.DataBind();
  }
  protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
  {
       GridView1.EditIndex = e.NewEditIndex;
       bindgrid();
  }
  protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
  {
       GridView1.EditIndex = -1;
       bindgrid();
  }
  protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
  {
       string mno = e.Keys[0].ToString();
       string mname = e.NewValues[0].ToString();
       string active = e.NewValues[1].ToString();
       string q = "update machinedetails set machinename = '"+mname+"',active='"+active+"' where machineno =" + mno;
       Response.Write(q);
       e.Cancel=true;
       GridView1.EditIndex = -1;
       bindgrid();
  }
  </code></pre>
  
  <p>But it throws the error:-</p>
  
  <pre><code>   Index was out of range. Must be non-negative and less than the size of the collection.
     Parameter name: index
  </code></pre>
  
  <p>can any one help me with this??</p>
  
  <p>Steve --> as per your question i changed my logic as below</p>
  
  <pre><code>protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
  {
       string mno = GridView1.DataKeys[e.RowIndex][0].ToString();
       GridView gv = (GridView)sender;
       for (int i = 0; i &lt; gv.Columns.Count; i++)
       {
            DataControlFieldCell cell = gv.Rows[e.RowIndex].Cells[i] as DataControlFieldCell;
            gv.Columns[i].ExtractValuesFromCell(e.NewValues, cell, DataControlRowState.Edit, true);
       }
       string mname = e.NewValues[0].ToString();
       string active = e.NewValues[1].ToString();
       string q = "update machinedetails set machinename = '" + mname + "',active='" + active + "' where machineno =" + mno;
       Response.Write(q);
       e.Cancel=true;
       GridView1.EditIndex = -1;
       bindgrid();
  }
  </code></pre>
  
  <p>Now I dont get any error but mname is coming same as datakey
  my response.write code produse the following output -->update machinedetails set machinename = '2',active='0' where machineno =2 </p>
  
  <p>machinename = '2' should be the value that i have entered in the textbox. Dropdownlist value is comming correct.</p>