title: Add new row in gridview while button onclick
body: <p>This is my program that page load the gridview and retrieve the database record to textbox and i put a add new button wish to add new row exactly like that row but the textbox is empty.</p>
  <p><a href="https://i.stack.imgur.com/mbNN5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/mbNN5.png" alt="enter image description here" /></a></p>
  <p>I wanted to create a new row in <code>GridView</code> while click add new button while my <code>TextBox</code> is retrieve data from database. I try to run the code but it is nothing happen when i click the button. Hope someone may help. Thanks.</p>
  <p>My front end code</p>
  <pre><code>&lt;Columns&gt;
      &lt;asp:TemplateField HeaderStyle-CssClass=&quot;display_none&quot;&gt;
          &lt;ItemTemplate&gt;
              &lt;asp:Label ID=&quot;Label3&quot; runat=&quot;server&quot; Font-Bold=&quot;true&quot; Font-Size=&quot;Medium&quot;&gt;Name&lt;/asp:Label&gt;
          &lt;/ItemTemplate&gt;
      &lt;/asp:TemplateField&gt;
  
      &lt;asp:TemplateField HeaderStyle-CssClass=&quot;display_none&quot;&gt;
  
          &lt;ItemTemplate&gt;
              &lt;asp:TextBox ID=&quot;TextBox1&quot; runat=&quot;server&quot; CssClass=&quot;NormalInputTextField&quot; Text='&lt;%#Eval(&quot;SLMN&quot;) %&gt;'&gt;&lt;/asp:TextBox&gt;
          &lt;/ItemTemplate&gt;
      &lt;/asp:TemplateField&gt;
      &lt;asp:TemplateField HeaderStyle-CssClass=&quot;display_none&quot;&gt;
      &lt;ItemTemplate&gt;
           &lt;asp:Button ID=&quot;BtnDelete&quot; runat=&quot;server&quot; Text=&quot;Delete&quot; CssClass=&quot;btn btn-primary&quot; CommandName=&quot;remove&quot; CommandArgument='&lt;%# Container.DataItemIndex %&gt;'/&gt;
      &lt;/ItemTemplate&gt;
      &lt;/asp:TemplateField&gt;
      &lt;asp:TemplateField FooterStyle-BorderStyle=&quot;None&quot; HeaderStyle-CssClass=&quot;display_none&quot;&gt;
      &lt;ItemTemplate&gt;
           &lt;asp:Button ID=&quot;BtnSearch2&quot; runat=&quot;server&quot; Text=&quot;Search&quot; CssClass=&quot;btn btn-primary&quot;  CommandName=&quot;ViewComments&quot; OnClientClick=&quot;javascript:saveData(this,'grid')&quot;/&gt;
      &lt;/ItemTemplate&gt;
      &lt;FooterStyle HorizontalAlign=&quot;Right&quot; BorderStyle=&quot;None&quot;/&gt;
      &lt;FooterTemplate&gt;
           &lt;asp:Button ID=&quot;BtnAdd&quot; runat=&quot;server&quot; Text=&quot;Add New Row&quot; CssClass=&quot;btn btn-primary&quot; OnClick=&quot;ButtonAdd_Click&quot;/&gt;
      &lt;/FooterTemplate&gt;
      &lt;/asp:TemplateField&gt;
  &lt;/Columns&gt;
  </code></pre>
  <p>My back end code</p>
  <pre><code>private void AddNewRowToGrid()
  {
       DataTable dt = new DataTable();
       DataRow dr = null;
       dt.Columns.Add(new DataColumn(&quot;Column1&quot;, typeof(string)));
       dt.Columns.Add(new DataColumn(&quot;Column2&quot;, typeof(string)));
       dr = dt.NewRow();
       dr[&quot;Column1&quot;] = string.Empty;
       dr[&quot;Column2&quot;] = string.Empty;
       dt.Rows.Add(dr);
  }
  </code></pre>
  <p>My onclick event to call add new function</p>
  <pre><code>protected void ButtonAdd_Click(object sender, EventArgs e)
  {
      AddNewRowToGrid();
  }
  </code></pre>
  <p><strong>My gridview tag</strong></p>
  <pre><code>&lt;grd:MultiSelectGridView ID=&quot;grid2&quot; runat=&quot;server&quot; Width=&quot;500px&quot; 
                          CssClass=&quot;paging_gridview&quot; AllowPaging=&quot;True&quot; 
                          AutoGenerateColumns =&quot;false&quot; PageSize=&quot;10&quot; PagerType=&quot;Custom&quot;  
                          ShowFooter=&quot;true&quot; OnRowDeleting=&quot;grid2_RowDeleting&quot; 
                          MultiSelectDataKeyName=&quot;Urid,Name&quot; ShowHeaderWhenEmpty=&quot;true&quot;
                          MultiSelectColumnIndex=&quot;0&quot; EnableMultiSelect=&quot;false&quot; GridLines=&quot;None&quot; BorderStyle=&quot;None&quot; OnRowCommand=&quot;grid2_RowCommand&quot;
                          &gt;
  </code></pre>
  <p>i bind my record to gridview by page load</p>
  <pre><code>protected void Page_Load(object sender, EventArgs e)
  {
      if (!Page.IsPostBack)
      {
          Loadgrid();
      }
  }
  
  private void Loadgrid()
  {
      string branch = txtBranch.Text.Trim();
      string name = txtName.Text.Trim();
  
      string sql = &quot;SELECT * FROM fcs_cotmdl WHERE TMDL= '&quot; + name + &quot;'AND CONO='&quot; + branch + &quot;' &quot;;
  
      SqlDataSource2.ConnectionString = ConfigurationManager.ConnectionStrings[&quot;ConnStr_epsi&quot;].ConnectionString;
      SqlDataSource2.ConnectionString = SqlDataSource2.ConnectionString.Replace(&quot;Provider=OraOLEDB.Oracle.1;&quot;, &quot;&quot;);
      SqlDataSource con = new SqlDataSource();
  
      SqlDataSource2.SelectCommand = sql;
      grid2.DataSourceID = &quot;SqlDataSource2&quot;;
      grid2.DataBind();
  }
  </code></pre>