title: How to get each row data using button field in gridview to display in textbox?
body: <p>I want to display my gridview details in respective textbox by clicking Edit buttonfield appear in gridview. I am able get only first row(row[0]) data, either by default or by clicking all over the another rows. But I need each row by clicking each row edit button. Here is my design code. I am using VS 2013 and SQL Server 2012.</p>
  
  <pre><code>&lt;%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Admin.aspx.cs"     Inherits="RigoTest.Admin" %&gt;
  &lt;!DOCTYPE html&gt;
     &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
     &lt;head runat="server"&gt;
     &lt;title&gt;&lt;/title&gt;
     &lt;style type="text/css"&gt;
      .auto-style1 {
          width: 50%;
      }
    &lt;/style&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;form id="form1" runat="server"&gt;
  &lt;div&gt;
  &lt;center&gt;
      &lt;asp:GridView ID="gvdetails" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" 
          GridLines="None" Width="800px" HorizontalAlign="Center" OnRowEditing="gvdetails_RowEditing" OnRowCommand="gvdetails_RowCommand"&gt;
          &lt;AlternatingRowStyle BackColor="White" ForeColor="#284775" /&gt;
          &lt;Columns&gt;
              &lt;%--&lt;asp:TemplateField HeaderText="ID" Visible="false"&gt;
                  &lt;EditItemTemplate&gt;
                      &lt;asp:TextBox ID="txtID" runat="server" Text='&lt;%# Bind("ID") %&gt;'&gt;&lt;/asp:TextBox&gt;
                  &lt;/EditItemTemplate&gt;
                  &lt;ItemTemplate&gt;
                      &lt;asp:Label ID="lblID" runat="server" Text='&lt;%# Bind("ID") %&gt;'&gt;&lt;/asp:Label&gt;
                  &lt;/ItemTemplate&gt;
              &lt;/asp:TemplateField&gt;
              &lt;asp:TemplateField HeaderText="Question"&gt;
                  &lt;EditItemTemplate&gt;
                      &lt;asp:TextBox ID="txtQue" runat="server" Text='&lt;%# Bind("question") %&gt;'&gt;&lt;/asp:TextBox&gt;
                  &lt;/EditItemTemplate&gt;
                  &lt;ItemTemplate&gt;
                      &lt;asp:Label ID="lblQue" runat="server" Text='&lt;%# Bind("question") %&gt;'&gt;&lt;/asp:Label&gt;
                  &lt;/ItemTemplate&gt;
              &lt;/asp:TemplateField&gt;
  
  
              --%&gt;
  
              &lt;asp:BoundField DataField="ID" HeaderText="ID" Visible="false" /&gt;
              &lt;asp:BoundField DataField="question" HeaderText="Question" /&gt;
  
              &lt;%--&lt;asp:CommandField ShowEditButton="True" HeaderText="Edit2"  /&gt;--%&gt;
              &lt;asp:ButtonField HeaderText="Edit" CommandName="edit" Text="Edit" /&gt;
              &lt;%--&lt;asp:CommandField CausesValidation="False" HeaderText="Edit2" InsertVisible="False" ShowCancelButton="False" ShowEditButton="True" UpdateText="" /&gt;--%&gt;
          &lt;/Columns&gt;
          &lt;EditRowStyle BackColor="#999999" /&gt;
          &lt;FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /&gt;
          &lt;HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" HorizontalAlign="Left" /&gt;
          &lt;PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" /&gt;
          &lt;RowStyle BackColor="#F7F6F3" ForeColor="#333333" /&gt;
          &lt;SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" /&gt;
          &lt;SortedAscendingCellStyle BackColor="#E9E7E2" /&gt;
          &lt;SortedAscendingHeaderStyle BackColor="#506C8C" /&gt;
          &lt;SortedDescendingCellStyle BackColor="#FFFDF8" /&gt;
          &lt;SortedDescendingHeaderStyle BackColor="#6F8DAE" /&gt;
      &lt;/asp:GridView&gt;
  &lt;/center&gt;
  &lt;/div&gt;
  
  
  
      &lt;div style="height: 300px; margin-top: 250px"&gt;
          &lt;center&gt;
          &lt;table width="40%" class="auto-style1"&gt;
              &lt;tr&gt;
                  &lt;td&gt;
                      Add/Edit Question&lt;/td&gt;
                  &lt;td&gt;
                      &amp;nbsp;&lt;/td&gt;
                  &lt;td&gt;&amp;nbsp;&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;td&gt;
                      &amp;nbsp;&lt;/td&gt;
                  &lt;td&gt;
                      &amp;nbsp;&lt;/td&gt;
                  &lt;td&gt;&amp;nbsp;&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;td&gt;
                      &lt;asp:Label ID="Label5" runat="server" Text="Question :"&gt;&lt;/asp:Label&gt;
                  &lt;/td&gt;
                  &lt;td&gt;
                      &lt;asp:TextBox ID="txtquestion" runat="server" Width="295px"&gt;&lt;/asp:TextBox&gt;
                  &lt;/td&gt;
                  &lt;td&gt;&amp;nbsp;&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;td&gt;
                      &lt;asp:Label ID="Label3" runat="server" Text="Option :"&gt;&lt;/asp:Label&gt;
                  &lt;/td&gt;
                  &lt;td&gt;
                      &lt;asp:TextBox ID="txtoption" runat="server" Width="295px"&gt;&lt;/asp:TextBox&gt;
                  &lt;/td&gt;
                  &lt;td&gt;
                      &lt;asp:Button ID="btnadd" runat="server" Text="Add" Width="50px" OnClick="btnadd_Click" /&gt;
                  &lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;td&gt;&amp;nbsp;&lt;/td&gt;
                  &lt;td&gt;
                      &lt;asp:ListBox ID="ListBox1" runat="server" Width="300px"&gt;&lt;/asp:ListBox&gt;
                  &lt;/td&gt;
                  &lt;td&gt;
                      &lt;asp:Button ID="btndelete" runat="server" Text="Delete" Width="50px" OnClick="btndelete_Click" /&gt;
                      &lt;br /&gt;
                      &lt;br /&gt;
                      &lt;asp:Button ID="btnselect" runat="server" Text="Select" Width="50px" OnClick="btnselect_Click" /&gt;
                  &lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;td&gt;
                      &lt;asp:Label ID="Label4" runat="server" Text="Correct Option :"&gt;&lt;/asp:Label&gt;
                  &lt;/td&gt;
                  &lt;td&gt;
                      &lt;asp:TextBox ID="txtcorrectoption" runat="server" Width="295px"&gt;&lt;/asp:TextBox&gt;
                  &lt;/td&gt;
                  &lt;td&gt;&amp;nbsp;&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;td&gt;
                      &lt;br /&gt;
                      &lt;asp:Button ID="btnsave" runat="server" Text="Save" Width="100px" /&gt;
                  &lt;/td&gt;
                  &lt;td&gt;&amp;nbsp;&lt;/td&gt;
                  &lt;td&gt;&amp;nbsp;&lt;/td&gt;
              &lt;/tr&gt;
          &lt;/table&gt;
          &lt;/center&gt;
      &lt;/div&gt;
  &lt;/form&gt;
      &lt;/body&gt;
  </code></pre>
  
  <p></p>
  
  <p>HERE IS MY ASP.NET CODE</p>
  
  <pre><code>public partial class Admin : System.Web.UI.Page
  {
      SqlConnection con = new SqlConnection("server = yamma; uid = sa; pwd = /*-; database = rigo");
      SqlCommand cmd;
      SqlDataAdapter da = new SqlDataAdapter();
      DataTable dt = new DataTable();
      public static DataTable dtDefault = new DataTable(); 
      public Int64 n;
      protected void Page_Load(object sender, EventArgs e)
      {
          if(!Page.IsPostBack)
          {
              showdata();
              //string EditID = Request.QueryString["EditId"];
          }
  
      }
  
      public void showdata()
      {
          if (con.State == ConnectionState.Closed)
          {
              con.Open();
          }
          cmd = new SqlCommand("RigoSelect", con);
          cmd.CommandType = CommandType.StoredProcedure;
          da.SelectCommand = cmd;
          da.Fill(dt);
          da.Fill(dtDefault);
          gvdetails.DataSource = dt;
          gvdetails.DataBind();
  
      }
  
  
  
      protected void btnadd_Click(object sender, EventArgs e)
      {
          ListBox1.Items.Add(txtoption.Text);         //to select data from textbox to listbox
      }
  
      protected void btndelete_Click(object sender, EventArgs e)
      {
          string remove = ListBox1.SelectedItem.Text;
          ListBox1.Items.Remove(remove);
  
          ////clear all items in listbox
          //listBox1.Items.Clear();
      }
  
      protected void btnselect_Click(object sender, EventArgs e)
      {
          txtcorrectoption.Text = ListBox1.SelectedItem.Text;
      }
  
      protected void gvdetails_RowCommand(object sender, GridViewCommandEventArgs e)
      {
          //private Int64 n;
          //for(int i=0;i&lt;=n;i++)
          //{
          int id = int.Parse(gvdetails.Rows[0].Cells[0].Text);
  
          DataRow[] dr = null;
          dr = dtDefault.Select("ID = '" + id + "'");
          txtquestion.Text = dr[0]["question"].ToString();
          txtoption.Text = dr[0]["options"].ToString();
          //}
          showdata();
  
  
      }
  
      protected void gvdetails_RowEditing(object sender, GridViewEditEventArgs e)
      {
          // when boundfied is exist in gridview then id can get....thisway
          //int id = int.Parse(gvdetails.Rows[e.NewEditIndex].Cells[0].Text);
  
          ////Label lbl = (Label)gvdetails.Rows[e.NewEditIndex].Cells[0].FindControl("lblID");
          ////string id = lbl.Text.Trim();
          ////int EID = int.Parse(id);
          //// Response.Redirect("Admin.aspx?EditId=" + id + "");
  
          //DataRow[] dr = null;
          //dr = dtDefault.Select("ID = '" + id + "'");
          //txtquestion.Text = dr[0]["question"].ToString();
          //txtoption.Text = dr[0]["options"].ToString();
  
          //showdata();
      }
  
  
       }
   }
  
    My problem occurs here 
  
     protected void gvdetails_RowCommand(object sender, GridViewCommandEventArgs e)
       {
          //private Int64 n;
          //for(int i=0;i&lt;=n;i++)
          //{
          int id = int.Parse(gvdetails.Rows[0].Cells[0].Text);
  
          DataRow[] dr = null;
          dr = dtDefault.Select("ID = '" + id + "'");
          txtquestion.Text = dr[0]["question"].ToString();
          txtoption.Text = dr[0]["options"].ToString();
          //}
          showdata();
  
  
      }
  </code></pre>
  
  <p><strong>Here is my main problem occurs:</strong></p>
  
  <pre><code>  protected void gvdetails_RowCommand(object sender, GridViewCommandEventArgs e)
      {
          //private Int64 n;
          //for(int i=0;i&lt;=n;i++)
          //{
          int id = int.Parse(gvdetails.Rows[0].Cells[0].Text);
  
          DataRow[] dr = null;
          dr = dtDefault.Select("ID = '" + id + "'");
          txtquestion.Text = dr[0]["question"].ToString();
          txtoption.Text = dr[0]["options"].ToString();
          //}
          showdata();
  
  
      }
  </code></pre>
  
  <p>When I click on any row that display only first row data. I want to get each row data by each clicking on respective textbox of grid row.</p>
  
  <p>I have tried with commandfield but when try to add the data of option textbox (txtoption) in listbox with add button, it automatically display update, cancel option in gridview.</p>