Step 1:- (Default.aspx) code
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Demo.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Dynamically
add and remove rows in gridview</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="Gridview1" runat="server" ShowFooter="true" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="RowNumber" HeaderText="Row
Number" />
<asp:TemplateField HeaderText="Header
1">
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Column1") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Header
2">
<ItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Column2") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Header
3">
<ItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Column3") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete">
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" Text="Delete" RowNumber='<%#Container.DataItemIndex
%>' OnClick="LinkButton1_Click" />
</ItemTemplate>
<FooterStyle HorizontalAlign="Right" />
<FooterTemplate>
<asp:Button ID="btnAddNewRow" runat="server" Text="Add
New Row" OnClick="ButtonAdd_Click" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
Step 2:- (Default.aspx.cs) code
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Demo
{
public partial class Default :
System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
SetInitialRow();
}
}
private void SetInitialRow()
{
DataTable dataTable
= new DataTable();
DataRow dataRow
= null;
dataTable.Columns.Add(new DataColumn("RowNumber", typeof(string)));
dataTable.Columns.Add(new DataColumn("Column1", typeof(string)));
dataTable.Columns.Add(new DataColumn("Column2", typeof(string)));
dataTable.Columns.Add(new DataColumn("Column3", typeof(string)));
dataRow = dataTable.NewRow();
dataRow["RowNumber"] = 1;
dataRow["Column1"] = string.Empty;
dataRow["Column2"] = string.Empty;
dataRow["Column3"] = string.Empty;
dataTable.Rows.Add(dataRow);
ViewState["ViewStateTable"] = dataTable;
Gridview1.DataSource = dataTable;
Gridview1.DataBind();
}
private void AddNewRowToGrid()
{
int rowIndex
= 0;
if (ViewState["ViewStateTable"] != null)
{
DataTable dataTable
= (DataTable)ViewState["ViewStateTable"];
DataRow dataRow
= null;
if (dataTable.Rows.Count
> 0)
{
for (int i
= 1; i <= dataTable.Rows.Count; i++)
{
TextBox box1
= (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
TextBox box2
= (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
TextBox box3
= (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3");
dataRow = dataTable.NewRow();
dataRow["RowNumber"] = i + 1;
dataTable.Rows[i - 1]["Column1"] =
box1.Text;
dataTable.Rows[i -
1]["Column2"] = box2.Text;
dataTable.Rows[i - 1]["Column3"] =
box3.Text;
rowIndex++;
}
dataTable.Rows.Add(dataRow);
ViewState["ViewStateTable"] =
dataTable;
Gridview1.DataSource = dataTable;
Gridview1.DataBind();
}
}
else
{
Response.Write("ViewState is null");
}
}
protected void ButtonAdd_Click(object sender, EventArgs e)
{
AddNewRowToGrid();
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
int rowIndex
= 0;
int index
= Convert.ToInt32(((LinkButton)sender).Attributes["RowNumber"]);
if (ViewState["ViewStateTable"] != null)
{
DataTable dataTable
= (DataTable)ViewState["ViewStateTable"];
if (dataTable.Rows.Count
== 1)
{
LinkButton lnk
= (LinkButton)Gridview1.Rows[0].FindControl("LinkButton1");
lnk.Enabled = false;
}
else
{
DataRow dataRow;
for (int i
= 1; i <= dataTable.Rows.Count; i++)
{
TextBox box1
= (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
TextBox box2
= (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
TextBox box3
= (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3");
dataRow = dataTable.NewRow();
dataTable.Rows[i - 1]["Column1"] =
box1.Text;
dataTable.Rows[i - 1]["Column2"] =
box2.Text;
dataTable.Rows[i - 1]["Column3"] =
box3.Text;
rowIndex++;
}
dataTable.Rows.RemoveAt(index);
ViewState["ViewStateTable"] = dataTable;
Gridview1.DataSource = dataTable;
Gridview1.DataBind();
}
}
}
}
}
Output:-