title: edit row in gridview
body: <p>I would like to help me with my code. I have 2 gridviews. In the first gridview the user can choose with a checkbox every row he wants. These rows are transfered in the second gridview. All these my code does them well.Now, I want to edit the quantity column in second gridview to change the value but i don't know what i must write in edit box.
  Here is my code:</p>
  
  <pre><code>using System;
  using System.Data;
  using System.Data.SqlClient;
  using System.Configuration;
  using System.Web;
  using System.Web.Security;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using System.Web.UI.WebControls.WebParts;
  using System.Web.UI.HtmlControls;
  using System.Collections;
  
  public partial class ShowLand : System.Web.UI.Page
  {
  
      protected void Page_Load(object sender, EventArgs e)
      {
  
  
          if (!IsPostBack)
          {
              BindPrimaryGrid();
              BindSecondaryGrid();
  
          }
      }
      private void BindPrimaryGrid()
      {
          string constr = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
          string query = "select * from Land";
          SqlConnection con = new SqlConnection(constr);
          SqlDataAdapter sda = new SqlDataAdapter(query, con);
          DataTable dt = new DataTable();
          sda.Fill(dt);
          gridview2.DataSource = dt;
          gridview2.DataBind();
      }
  
  
  
      private void GetData()
      {
          DataTable dt;
          if (ViewState["SelectedRecords1"] != null)
              dt = (DataTable)ViewState["SelectedRecords1"];
          else
              dt = CreateDataTable();
          CheckBox chkAll = (CheckBox)gridview2.HeaderRow
                              .Cells[0].FindControl("chkAll");
          for (int i = 0; i &lt; gridview2.Rows.Count; i++)
          {
              if (chkAll.Checked)
              {
                  dt = AddRow(gridview2.Rows[i], dt);
              }
              else
              {
                  CheckBox chk = (CheckBox)gridview2.Rows[i]
                                  .Cells[0].FindControl("chk");
                  if (chk.Checked)
                  {
                      dt = AddRow(gridview2.Rows[i], dt);
                  }
                  else
                  {
                      dt = RemoveRow(gridview2.Rows[i], dt);
                  }
              }
          }
          ViewState["SelectedRecords1"] = dt;
      }
      private void SetData()
      {
          CheckBox chkAll = (CheckBox)gridview2.HeaderRow.Cells[0].FindControl("chkAll");
          chkAll.Checked = true;
          if (ViewState["SelectedRecords1"] != null)
          {
              DataTable dt = (DataTable)ViewState["SelectedRecords1"];
              for (int i = 0; i &lt; gridview2.Rows.Count; i++)
              {
                  CheckBox chk = (CheckBox)gridview2.Rows[i].Cells[0].FindControl("chk");
                  if (chk != null)
                  {
                      DataRow[] dr = dt.Select("id = '" + gridview2.Rows[i].Cells[1].Text + "'");
                      chk.Checked = dr.Length &gt; 0;
                      if (!chk.Checked)
                      {
                          chkAll.Checked = false;
                      }
                  }
              }
          }
      }
              private DataTable CreateDataTable()
                  {
                       DataTable dt = new DataTable();
                       dt.Columns.Add("id");
                       dt.Columns.Add("name");
                       dt.Columns.Add("price");
                       dt.Columns.Add("quantity");
                       dt.Columns.Add("total");
                       dt.AcceptChanges();
                       return dt;
                  }
      private DataTable AddRow(GridViewRow gvRow, DataTable dt)
       {
      DataRow[] dr = dt.Select("id = '" + gvRow.Cells[1].Text + "'");
      if (dr.Length &lt;= 0)
      {
          dt.Rows.Add();
          dt.Rows[dt.Rows.Count - 1]["id"] = gvRow.Cells[1].Text;
          dt.Rows[dt.Rows.Count - 1]["name"] = gvRow.Cells[2].Text;
          dt.Rows[dt.Rows.Count - 1]["price"] = gvRow.Cells[3].Text;
          dt.Rows[dt.Rows.Count - 1]["quantity"] = gvRow.Cells[4].Text;
          dt.Rows[dt.Rows.Count - 1]["total"] = gvRow.Cells[5].Text;
          dt.AcceptChanges();
      }
      return dt;
      }
  
      private DataTable RemoveRow(GridViewRow gvRow, DataTable dt)
      {
      DataRow[] dr = dt.Select("id = '" + gvRow.Cells[1].Text + "'");
      if (dr.Length &gt; 0)
      {
          dt.Rows.Remove(dr[0]);
          dt.AcceptChanges();
      }
      return dt;
      }
  
      protected void CheckBox_CheckChanged(object sender, EventArgs e)
      {
      GetData();
      SetData();
      BindSecondaryGrid();
      }
  
      private void BindSecondaryGrid()
      {
      DataTable dt = (DataTable)ViewState["SelectedRecords1"];
      gridview3.DataSource = dt;
      gridview3.DataBind(); 
      }
    }
  </code></pre>
  
  <p>and the source code is</p>
  
  <pre><code>&lt;asp:GridView ID="gridview2" runat="server" AutoGenerateColumns="False" 
   DataKeyNames="id" DataSourceID="SqlDataSource5"&gt;
   &lt;Columns&gt;
    &lt;asp:TemplateField&gt;
     &lt;HeaderTemplate&gt;
      &lt;asp:CheckBox ID="chkAll" runat="server" onclick = "checkAll(this);"
                    AutoPostBack = "true"  OnCheckedChanged = "CheckBox_CheckChanged"/&gt;
     &lt;/HeaderTemplate&gt;
     &lt;ItemTemplate&gt;
         &lt;asp:CheckBox ID="chk" runat="server" onclick = "Check_Click(this)"
                 AutoPostBack = "true"  OnCheckedChanged = "CheckBox_CheckChanged" /&gt;
     &lt;/ItemTemplate&gt;
    &lt;/asp:TemplateField&gt;
  
    &lt;asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" 
             ReadOnly="True" SortExpression="id" /&gt;
  
    &lt;asp:BoundField DataField="name" HeaderText="name" 
             SortExpression="name" /&gt;
  
    &lt;asp:BoundField DataField="price" HeaderText="price" SortExpression="price" /&gt;
  
    &lt;asp:BoundField DataField="quantity" HeaderText="quantity" 
             SortExpression="quantity" /&gt;
  
    &lt;asp:BoundField DataField="total" HeaderText="total" SortExpression="total" /&gt;
   &lt;/Columns&gt;
   &lt;/asp:GridView&gt;
  
   &lt;asp:SqlDataSource ID="SqlDataSource5" runat="server" 
      ConnectionString="&lt;%$ ConnectionStrings:ConnectionString %&gt;" 
      SelectCommand="SELECT * FROM [Land]"&gt;&lt;/asp:SqlDataSource&gt;
    &lt;br /&gt;
    &lt;/div&gt;
  &lt;div&gt;
  
  &lt;asp:GridView ID="gridview3" runat="server"
    AutoGenerateColumns = "False" DataKeyNames="id" 
    EmptyDataText = "No Records Selected" &gt;
  &lt;Columns&gt;
    &lt;asp:BoundField DataField = "id" HeaderText = "id" /&gt;
    &lt;asp:BoundField DataField = "name" HeaderText = "name" ReadOnly="True" /&gt;
    &lt;asp:BoundField DataField = "price" HeaderText = "price" 
           DataFormatString="{0:c}" ReadOnly="True" /&gt;
    &lt;asp:TemplateField HeaderText="quantity"&gt;
    &lt;EditItemTemplate&gt;
       &lt;asp:TextBox ID="TextBox1" runat="server" Text='&lt;%# Bind("quantity")%&gt;'&lt;/asp:TextBox&gt;
    &lt;/EditItemTemplate&gt;
    &lt;ItemTemplate&gt;
       &lt;asp:Label ID="Label1" runat="server" Text='&lt;%# Bind("quantity") %&gt;'&gt;&lt;/asp:Label&gt;
    &lt;/ItemTemplate&gt;
    &lt;/asp:TemplateField&gt;
    &lt;asp:BoundField DataField = "total" HeaderText = "total" 
              DataFormatString="{0:c}" ReadOnly="True" /&gt;
              &lt;asp:CommandField ShowEditButton="True" /&gt;
  
   &lt;/Columns&gt;
  &lt;/asp:GridView&gt;
  
  &lt;asp:Label ID="totalLabel" runat="server"&gt;&lt;/asp:Label&gt;
  &lt;br /&gt;
  
       &lt;/div&gt;
      &lt;/form&gt;
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>