title: When using a GridView, SelectedIndex is always 0 when using custom select button
body: <p>When clicking the select button in gvSquid2, x gets a valid and correct value.  When clicking the select button in gvSquid, I get an ArgumentOutOfRange Exception because gv.<code>SelectedIndex</code>=0.  Additionally, gv.<code>SelectedDataKey</code> is null.  How do I get the <code>DataKey</code> information?  Putting it in a column is not acceptable, as it must be hidden from the user.  Hiding the column is useless, because during <code>DataBind()</code>, the value is discarded.  I ripped this example from Microsoft's website.  What am I missing?</p>
  
  <pre><code>&lt;asp:gridview ID="gvSquid2" DataSourceID="dsComments" AutoGenerateColumns="false" AutoGenerateSelectButton="true" DataKeyNames="ID" OnSelectedIndexChanged="gvSquid_SelectedIndexChanged" runat="server" EnableViewState="false"&gt;
      &lt;Columns&gt;
          &lt;asp:BoundField DataField="Date" ReadOnly="true" HeaderText="Date" SortExpression="Date" /&gt;
          &lt;asp:BoundField DataField="Comment" ReadOnly="true" HeaderText="Comment" SortExpression="Comment" /&gt;
          &lt;asp:BoundField DataField="Username" ReadOnly="true" HeaderText="User" SortExpression="Username" /&gt;
      &lt;/Columns&gt;
  &lt;/asp:gridview&gt; 
  
  &lt;asp:gridview ID="gvSquid" DataSourceID="dsComments" AutoGenerateColumns="false" DataKeyNames="ID" OnSelectedIndexChanged="gvSquid_SelectedIndexChanged" runat="server" EnableViewState="false"&gt;
    &lt;Columns&gt;
      &lt;asp:CommandField ButtonType="Image" HeaderText="Select" ShowSelectButton="true" SelectImageUrl="~/includes/RedX.jpg" /&gt;
      &lt;asp:BoundField DataField="Date" ReadOnly="true" HeaderText="Date" SortExpression="Date" /&gt;
      &lt;asp:BoundField DataField="Comment" ReadOnly="true" HeaderText="Comment" SortExpression="Comment" /&gt;
      &lt;asp:BoundField DataField="Username" ReadOnly="true" HeaderText="User" SortExpression="Username" /&gt;
    &lt;/Columns&gt;
  &lt;/asp:gridview&gt; 
  
  protected void gvSquid_SelectedIndexChanged(object sender, EventArgs e) {
      string x;
      x = gvSquid.DataKeys[gvSquid.SelectedIndex].Value.ToString();
  } 
  </code></pre>