Add New Row Of Grid View On Button Click

In my current project I have a requirement that the user should be able to add a new row on click of button in grid view.

So for that I have used asp grid view in that application page.

Below are the steps,
  1. Open visual studio -> create new empty project -> right click and click add new item select application page -> give name to that application page.
  2. In content place holder add below code
    1. <div id="gvAddrow">  
    2.     <asp:gridview ID="Gridview1" runat="server" ShowFooter="true" AutoGenerateColumns="false">  
    3.         <Columns>  
    4.             <asp:BoundField DataField="RowNumber" HeaderText="Row Number" />  
    5.             <asp:TemplateField HeaderText="Header 1">  
    6.                 <ItemTemplate>  
    7.                     <asp:TextBox ID="txt1" runat="server"></asp:TextBox>  
    8.                 </ItemTemplate>  
    9.             </asp:TemplateField>  
    10.             <asp:TemplateField HeaderText="Header 2">  
    11.                 <ItemTemplate>  
    12.                     <asp:TextBox ID="txt2" runat="server"></asp:TextBox>  
    13.                 </ItemTemplate>  
    14.             </asp:TemplateField>  
    15.             <asp:TemplateField HeaderText="Header 3">  
    16.                 <ItemTemplate>  
    17.                     <asp:TextBox ID="txt3" runat="server"></asp:TextBox>  
    18.                 </ItemTemplate>  
    19.                 <FooterStyle HorizontalAlign="Right" />  
    20.                 <FooterTemplate>  
    21.                     <asp:Button ID="ButtonAdd" runat="server" Text="Add Row" onclick="ButtonAdd_Click" /> </FooterTemplate>  
    22.             </asp:TemplateField>  
    23.         </Columns>  
    24.     </asp:gridview>  
    25. </div>  
  3. Open .cs file and add below code in button click event
    1. protected void ButtonAdd_Click(object sender, EventArgs e) {  
    2.     AddNewRowToGrid();  
    3. }  
    4. private void AddNewRowToGrid() {  
    5.     int rowIndex = 0;  
    6.     if (ViewState["CurrentTable"] != null) {  
    7.         DataTable dtCurrentTable = (DataTable) ViewState["CurrentTable"];  
    8.         DataRow drCurrentRow = null;  
    9.         if (dtCurrentTable.Rows.Count > 0) {  
    10.             for (int i = 1; i <= dtCurrentTable.Rows.Count; i++) {  
    11.                 //extract the TextBox values  
    12.                 TextBox box1 = (TextBox) Gridview1.Rows[rowIndex].Cells[1].FindControl("txt1");  
    13.                 TextBox box2 = (TextBox) Gridview1.Rows[rowIndex].Cells[2].FindControl("txt2");  
    14.                 TextBox box3 = (TextBox) Gridview1.Rows[rowIndex].Cells[3].FindControl("txt3");  
    15.                 drCurrentRow = dtCurrentTable.NewRow();  
    16.                 drCurrentRow["RowNumber"] = i + 1;  
    17.                 dtCurrentTable.Rows[i - 1]["Column1"] = box1.Text;  
    18.                 dtCurrentTable.Rows[i - 1]["Column2"] = box2.Text;  
    19.                 dtCurrentTable.Rows[i - 1]["Column3"] = box3.Text;  
    20.                 rowIndex++;  
    21.             }  
    22.             dtCurrentTable.Rows.Add(drCurrentRow);  
    23.             ViewState["CurrentTable"] = dtCurrentTable;  
    24.             Gridview1.DataSource = dtCurrentTable;  
    25.             Gridview1.DataBind();  
    26.         }  
    27.     } else {  
    28.         Response.Write("ViewState is null");  
    29.     }  
    30.     //Set Previous Data on Postbacks  
    31.     SetPreviousData();  
    32. }  
    33. private void SetPreviousData() {  
    34.     int rowIndex = 0;  
    35.     if (ViewState["CurrentTable"] != null) {  
    36.         DataTable dt = (DataTable) ViewState["CurrentTable"];  
    37.         if (dt.Rows.Count > 0) {  
    38.             for (int i = 0; i < dt.Rows.Count; i++) {  
    39.                 TextBox box1 = (TextBox) Gridview1.Rows[rowIndex].Cells[1].FindControl("txt1");  
    40.                 TextBox box2 = (TextBox) Gridview1.Rows[rowIndex].Cells[2].FindControl("txt2");  
    41.                 TextBox box3 = (TextBox) Gridview1.Rows[rowIndex].Cells[3].FindControl("txt3");  
    42.                 box1.Text = dt.Rows[i]["Column1"].ToString();  
    43.                 box2.Text = dt.Rows[i]["Column2"].ToString();  
    44.                 box3.Text = dt.Rows[i]["Column3"].ToString();  
    45.                 rowIndex++;  
    46.             }  
    47.         }  
    48.     }  
    49. }  
    50. On page load add below code so that it will bind initial rows of grid view  
    51. protected void Page_Load(object sender, EventArgs e) {  
    52.     if (!Page.IsPostBack) {  
    53.         SetInitialRow();  
    54.     }  
    55. }  
    56. private void SetInitialRow() {  
    57.     DataTable dt = new DataTable();  
    58.     DataRow dr = null;  
    59.     dt.Columns.Add(new DataColumn("RowNumber"typeof(string)));  
    60.     dt.Columns.Add(new DataColumn("Column1"typeof(string)));  
    61.     dt.Columns.Add(new DataColumn("Column2"typeof(string)));  
    62.     dt.Columns.Add(new DataColumn("Column3"typeof(string)));  
    63.     dr = dt.NewRow();  
    64.     dr["RowNumber"] = 1;  
    65.     dr["Column1"] = string.Empty;  
    66.     dr["Column2"] = string.Empty;  
    67.     dr["Column3"] = string.Empty;  
    68.     dt.Rows.Add(dr);  
    69.     //Store the DataTable in ViewState  
    70.     ViewState["CurrentTable"] = dt;  
    71.     Gridview1.DataSource = dt;  
    72.     Gridview1.DataBind();  
    73. }  
  4. Build and deploy above code.
  5. Open application page and grid view will appear on page
  6. On click of Add row button it will add one new row with three textboxes.
Thanks