title: How to add new grid rows without clearing existing data
body: <p>My Code successfully adds new rows to the gridview on the button click, but clears all entered values in existing rows. I understand that, the existing data should be stored into a ViewState, but I do not know where/how to achieve this.</p>
  
  <p>ASPX:</p>
  
  <pre><code>&lt;asp:GridView ID="gvCommissions" runat="server" AutoGenerateColumns="False"
       CellPadding="4" ForeColor="#333333" AutoGenerateDeleteButton="True"&gt;                
    &lt;Columns&gt;
      &lt;asp:TemplateField HeaderText="S.NO" ItemStyle-Width="5%"&gt;
       &lt;ItemTemplate&gt;
        &lt;asp:Label ID="lblSno" runat="server" Width="98%"&gt;&lt;/asp:Label&gt;
       &lt;/ItemTemplate&gt;
      &lt;/asp:TemplateField&gt;
      &lt;asp:TemplateField HeaderText="NAME" ItemStyle-Width="30%"&gt;
       &lt;ItemTemplate&gt;
        &lt;asp:TextBox ID="txtName" runat="server" Width="98%"&gt;&lt;/asp:TextBox&gt;
       &lt;/ItemTemplate&gt;
    &lt;/Columns&gt;
  &lt;/asp:GridView&gt;
  </code></pre>
  
  <p>CODE BEHIND:</p>
  
  <pre><code>DataTable dt;
  
      protected void Page_Load(object sender, EventArgs e)
      {
         if(ViewState["gvCommissions"] != null) 
           dt = (DataTable)ViewState["gvCommissions"];
          if (!Page.IsPostBack)
          {
              GridViewStructure();
              AddNewRow();
          }
          else
          {
              dt = (DataTable)ViewState["gvCommissions"];
          }
  
          ViewState["gvCommissions"] = dt;  
      }
  
      private void GridViewStructure()
      {
          dt = new DataTable();
          dt.Columns.Add("sno");
          dt.Columns.Add("name");
      }            
  
      private void AddNewRow()
      {            
          dt = new DataTable();
          dt.Columns.Add("sno");
          dt.Columns.Add("name");
  
          foreach (GridViewRow gvRow in gvCommissions.Rows)
          {
              DataRow drCurrentRow = dt.NewRow();
              drCurrentRow["sno"] = ((Label)gvRow.FindControl("lblSno")).Text;
              drCurrentRow["name"] = ((TextBox)gvRow.FindControl("txtName")).Text;
              dt.Rows.Add(drCurrentRow);
          }
  
          DataRow dr = dt.NewRow();
          dr["sno"] = "";
          dr["name"] = "";
  
          dt.Rows.Add(dr);
  
          gvCommissions.DataSource = dt;
          gvCommissions.DataBind();
  
      }
  
      protected void btnAddRow_Click(object sender, EventArgs e)
      {
          AddNewRow();            
      }
  </code></pre>