title: Formview cannot show data from gridview's selected index
body: <p>I am trying to implement a function:
   when you click on edit, you will load the item in the formview (which should then be put into edit mode.</p>
  
  <p>But the problem is that the formview always show the first row of the grid on edit mode.</p>
  
  <p>Can anyone tell me how to fix this? Thank you.</p>
  
  <pre><code>&lt;asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"&gt;
  &lt;div style="overflow-x:scroll; overflow-y:hidden; width:auto; height:auto"&gt;
  &lt;asp:GridView ID="GridView1" runat="server" DataKeyNames="CustomerID,ModifyBy" AllowPaging="True" AutoGenerateColumns="False" DataSourceID="SqlDataSource_gridView" CellPadding="4" ForeColor="#333333" GridLines="None" Height="325px" HorizontalAlign="Center" Width="809px" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"&gt;
      &lt;AlternatingRowStyle BackColor="White" /&gt;
      &lt;Columns&gt;
          &lt;asp:CommandField ShowSelectButton="True" /&gt;
          &lt;asp:BoundField DataField="CustomerID" HeaderText="CustomerID" SortExpression="CustomerID" InsertVisible="False" ReadOnly="True" /&gt;
          &lt;asp:BoundField DataField="CustomerName" HeaderText="CustomerName" SortExpression="CustomerName" /&gt;
          &lt;asp:BoundField DataField="CustomerTitle" HeaderText="CustomerTitle" SortExpression="CustomerTitle" /&gt;
          &lt;asp:BoundField DataField="Address1" HeaderText="Address1" SortExpression="Address1" /&gt;
          &lt;asp:BoundField DataField="City" HeaderText="City" SortExpression="City" /&gt;
          &lt;asp:BoundField DataField="StateID" HeaderText="StateID" SortExpression="StateID" /&gt;
          &lt;asp:BoundField DataField="CountryID" HeaderText="CountryID" SortExpression="CountryID" /&gt;
          &lt;asp:BoundField DataField="PostalCode" HeaderText="PostalCode" SortExpression="PostalCode" /&gt;
          &lt;asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" /&gt;
          &lt;asp:BoundField DataField="ModifyBy" HeaderText="ModifyBy" SortExpression="ModifyBy" /&gt;
          &lt;asp:CheckBoxField DataField="IsActive" HeaderText="IsActive" SortExpression="IsActive" ReadOnly="true" /&gt;
      &lt;/Columns&gt;
      &lt;FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" /&gt;
      &lt;HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" /&gt;
      &lt;PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" /&gt;
      &lt;RowStyle BackColor="#FFFBD6" ForeColor="#333333" /&gt;
      &lt;SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" /&gt;
      &lt;SortedAscendingCellStyle BackColor="#FDF5AC" /&gt;
      &lt;SortedAscendingHeaderStyle BackColor="#4D0000" /&gt;
      &lt;SortedDescendingCellStyle BackColor="#FCF6C0" /&gt;
      &lt;SortedDescendingHeaderStyle BackColor="#820000" /&gt;
  &lt;/asp:GridView&gt;
  &lt;/div&gt;
  &lt;asp:SqlDataSource ID="SqlDataSource_gridView" runat="server" ConnectionString="&lt;%$ ConnectionStrings:SummerBreezeConnectionString %&gt;" SelectCommand="SELECT CustomerName, CustomerTitle, Address1, City, StateID, CountryID, PostalCode, Email, CustomerID, ModifyBy, IsActive FROM Customer" UpdateCommand="adn_Customer_UPDATE" UpdateCommandType="StoredProcedure" DeleteCommand="adn_Customer_DELETE" DeleteCommandType="StoredProcedure" InsertCommand="adn_Customer_INSERT" InsertCommandType="StoredProcedure"&gt;
      &lt;DeleteParameters&gt;
          &lt;asp:Parameter Name="CustomerID" Type="Int32" /&gt;
          &lt;asp:Parameter Name="ModifyBy" Type="String" /&gt;
      &lt;/DeleteParameters&gt;
      &lt;InsertParameters&gt;
          &lt;asp:Parameter Name="CustomerName" Type="String" /&gt;
          &lt;asp:Parameter Name="CustomerTitle" Type="String" /&gt;
          &lt;asp:Parameter Name="Address1" Type="String" /&gt;
          &lt;asp:Parameter Name="City" Type="String" /&gt;
          &lt;asp:Parameter Name="StateID" Type="Int32" /&gt;
          &lt;asp:Parameter Name="PostalCode" Type="String" /&gt;
          &lt;asp:Parameter Name="CountryID" Type="Int32" /&gt;
          &lt;asp:Parameter Name="Email" Type="String" /&gt;
          &lt;asp:Parameter Name="CreateBy" Type="String" /&gt;
          &lt;asp:Parameter Direction="InputOutput" Name="CustomerID" Type="Int32" /&gt;
      &lt;/InsertParameters&gt;
      &lt;UpdateParameters&gt;
          &lt;asp:Parameter Name="CustomerID" Type="Int32" /&gt;
          &lt;asp:Parameter Name="CustomerName" Type="String" /&gt;
          &lt;asp:Parameter Name="CustomerTitle" Type="String" /&gt;
          &lt;asp:Parameter Name="Address1" Type="String" /&gt;
          &lt;asp:Parameter Name="City" Type="String" /&gt;
          &lt;asp:Parameter Name="StateID" Type="Int32" /&gt;
          &lt;asp:Parameter Name="PostalCode" Type="String" /&gt;
          &lt;asp:Parameter Name="CountryID" Type="Int32" /&gt;
          &lt;asp:Parameter Name="Email" Type="String" /&gt;
          &lt;asp:Parameter Name="ModifyBy" Type="String" /&gt;
      &lt;/UpdateParameters&gt;
  &lt;/asp:SqlDataSource&gt;
  &lt;asp:FormView ID="FormView1" runat="server" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4" DataKeyNames="CustomerID" DataSourceID="SqlDataSource_formView" ForeColor="Black" GridLines="Vertical" Height="237px" OnItemUpdated="FormView1_ItemUpdated" Width="342px"&gt;
      &lt;EditItemTemplate&gt;
          CustomerName:
          &lt;asp:TextBox ID="CustomerNameTextBox" runat="server" Text='&lt;%# Bind("CustomerName") %&gt;' /&gt;
          &lt;br /&gt;
          CustomerTitle:
          &lt;asp:TextBox ID="CustomerTitleTextBox" runat="server" Text='&lt;%# Bind("CustomerTitle") %&gt;' /&gt;
          &lt;br /&gt;
          Address1:
          &lt;asp:TextBox ID="Address1TextBox" runat="server" Text='&lt;%# Bind("Address1") %&gt;' /&gt;
          &lt;br /&gt;
          City:
          &lt;asp:TextBox ID="CityTextBox" runat="server" Text='&lt;%# Bind("City") %&gt;' /&gt;
          &lt;br /&gt;
          StateID:
          &lt;asp:TextBox ID="StateIDTextBox" runat="server" Text='&lt;%# Bind("StateID") %&gt;' /&gt;
          &lt;br /&gt;
          CountryID:
          &lt;asp:TextBox ID="CountryIDTextBox" runat="server" Text='&lt;%# Bind("CountryID") %&gt;' /&gt;
          &lt;br /&gt;
          PostalCode:
          &lt;asp:TextBox ID="PostalCodeTextBox" runat="server" Text='&lt;%# Bind("PostalCode") %&gt;' /&gt;
          &lt;br /&gt;
          Email:
          &lt;asp:TextBox ID="EmailTextBox" runat="server" Text='&lt;%# Bind("Email") %&gt;' /&gt;
          &lt;br /&gt;
          ModifyBy:
          &lt;asp:TextBox ID="ModifyByTextBox" runat="server" Text='&lt;%# Bind("ModifyBy") %&gt;' /&gt;
          &lt;br /&gt;
          CustomerID:
          &lt;asp:Label ID="CustomerIDLabel1" runat="server" Text='&lt;%# Eval("CustomerID") %&gt;' /&gt;
          &lt;br /&gt;
          &lt;asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="Update" /&gt;
          &amp;nbsp;&lt;asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" /&gt;
      &lt;/EditItemTemplate&gt;
      &lt;EditRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" /&gt;
      &lt;FooterStyle BackColor="#CCCC99" /&gt;
      &lt;HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" /&gt;
      &lt;PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" /&gt;
      &lt;RowStyle BackColor="#F7F7DE" /&gt;
  &lt;/asp:FormView&gt;
  &lt;asp:SqlDataSource ID="SqlDataSource_formView" runat="server" ConnectionString="&lt;%$ ConnectionStrings:SummerBreezeConnectionString %&gt;" DeleteCommand="adn_Customer_DELETE" DeleteCommandType="StoredProcedure" InsertCommand="adn_Customer_INSERT" InsertCommandType="StoredProcedure" SelectCommand="SELECT CustomerName, CustomerTitle, Address1, City, StateID, CountryID, PostalCode, Email, IsActive, ModifyBy, CustomerID FROM Customer" UpdateCommand="adn_Customer_UPDATE" UpdateCommandType="StoredProcedure"&gt;
      &lt;DeleteParameters&gt;
          &lt;asp:Parameter Name="CustomerID" Type="Int32" /&gt;
          &lt;asp:Parameter Name="ModifyBy" Type="String" /&gt;
      &lt;/DeleteParameters&gt;
      &lt;InsertParameters&gt;
          &lt;asp:Parameter Name="CustomerName" Type="String" /&gt;
          &lt;asp:Parameter Name="CustomerTitle" Type="String" /&gt;
          &lt;asp:Parameter Name="Address1" Type="String" /&gt;
          &lt;asp:Parameter Name="City" Type="String" /&gt;
          &lt;asp:Parameter Name="StateID" Type="Int32" /&gt;
          &lt;asp:Parameter Name="PostalCode" Type="String" /&gt;
          &lt;asp:Parameter Name="CountryID" Type="Int32" /&gt;
          &lt;asp:Parameter Name="Email" Type="String" /&gt;
          &lt;asp:Parameter Name="CreateBy" Type="String" /&gt;
          &lt;asp:Parameter Direction="InputOutput" Name="CustomerID" Type="Int32" /&gt;
      &lt;/InsertParameters&gt;
      &lt;UpdateParameters&gt;
          &lt;asp:Parameter Name="CustomerID" Type="Int32" /&gt;
          &lt;asp:Parameter Name="CustomerName" Type="String" /&gt;
          &lt;asp:Parameter Name="CustomerTitle" Type="String" /&gt;
          &lt;asp:Parameter Name="Address1" Type="String" /&gt;
          &lt;asp:Parameter Name="City" Type="String" /&gt;
          &lt;asp:Parameter Name="StateID" Type="Int32" /&gt;
          &lt;asp:Parameter Name="PostalCode" Type="String" /&gt;
          &lt;asp:Parameter Name="CountryID" Type="Int32" /&gt;
          &lt;asp:Parameter Name="Email" Type="String" /&gt;
          &lt;asp:Parameter Name="ModifyBy" Type="String" /&gt;
      &lt;/UpdateParameters&gt;
  &lt;/asp:SqlDataSource&gt;
  &lt;br /&gt;
  </code></pre>
  
  <p></p>
  
  <p>The code behind</p>
  
  <pre><code>protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
      {
          FormView1.ChangeMode(FormViewMode.Edit);
          FormView1.DataBind();
      }
  </code></pre>