Please refer this code
HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript">
window.onload = function () {
var table = document.getElementById("<%=Gridview1.ClientID %>");
var dropdownlist = table.getElementsByTagName('select');
for (var i = 0; i < dropdownlist.length; i++) {
if (dropdownlist[i].value == "Yes") {
var parent = dropdownlist[i].parentNode.parentNode;
var control = parent.getElementsByTagName('input');
for (var j = 0; j < control.length; j++) {
if (control[j].type == "text") {
control[j].disabled = "disabled";
}
}
}
else {
}
}
}
</script>
<script type="text/javascript">
function EnableDisableTextBox(ddl) {
var parent = ddl.parentNode.parentNode;
var control = parent.getElementsByTagName('input');
for (var i = 0; i < control.length; i++) {
if (control[i].type == "text" && ddl.value == "Yes") {
control[i].disabled = "disabled";
}
if (control[i].type == "text" && ddl.value != "Yes") {
control[i].disabled = "";
}
}
}
</script>
<style type="text/css">
body
{
font-family: Arial;
font-size: 10pt;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="Gridview1" runat="server" ShowFooter="true" AutoGenerateColumns="false"
OnRowCreated="Gridview1_RowCreated">
<Columns>
<asp:TemplateField HeaderText="RowNumber">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Header 1">
<ItemTemplate>
<%--<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>--%>
<asp:DropDownList ID="DropDownList1" onchange="EnableDisableTextBox(this);" runat="server">
<asp:ListItem Text="Please Select" Value="Please Select" />
<asp:ListItem Text="Yes" Value="Yes" />
<asp:ListItem Text="No" Value="No" />
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Header 2">
<ItemTemplate>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Header 3">
<ItemTemplate>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</ItemTemplate>
<FooterStyle HorizontalAlign="Right" />
<FooterTemplate>
<asp:Button ID="ButtonAdd" runat="server" Text="Add New Row" OnClick="ButtonAdd_Click" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">Remove</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
Namespace
using System.Data;
C#
private void SetInitialRow()
{
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("Column1", typeof(string)));
dt.Columns.Add(new DataColumn("Column2", typeof(string)));
dt.Columns.Add(new DataColumn("Column3", typeof(string)));
dr = dt.NewRow();
dr["Column1"] = string.Empty;
dr["Column2"] = string.Empty;
dr["Column3"] = string.Empty;
dt.Rows.Add(dr);
//dr = dt.NewRow();
//Store the DataTable in ViewState
ViewState["CurrentTable"] = dt;
Gridview1.DataSource = dt;
Gridview1.DataBind();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
SetInitialRow();
}
}
protected void ButtonAdd_Click(object sender, EventArgs e)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
dtCurrentTable.Rows.Clear();
foreach (GridViewRow row in Gridview1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
DropDownList box1 = (DropDownList)row.FindControl("DropDownList1");
TextBox box2 = (TextBox)row.FindControl("TextBox2");
TextBox box3 = (TextBox)row.FindControl("TextBox3");
dtCurrentTable.Rows.Add(box1.SelectedItem.Value, box2.Text, box3.Text);
}
}
dtCurrentTable.Rows.Add("", "", "");
ViewState["CurrentTable"] = dtCurrentTable;
this.Gridview1.DataSource = dtCurrentTable;
this.Gridview1.DataBind();
for (int i = 0; i < dtCurrentTable.Rows.Count; i++)
{
if (this.Gridview1.Rows[i].RowType == DataControlRowType.DataRow)
{
DropDownList box1 = (DropDownList)this.Gridview1.Rows[i].FindControl("DropDownList1");
TextBox box2 = (TextBox)this.Gridview1.Rows[i].FindControl("TextBox2");
TextBox box3 = (TextBox)this.Gridview1.Rows[i].FindControl("TextBox3");
box1.Text = dtCurrentTable.Rows[i]["Column1"].ToString();
box2.Text = dtCurrentTable.Rows[i]["Column2"].ToString();
box3.Text = dtCurrentTable.Rows[i]["Column3"].ToString();
}
}
}
protected void Gridview1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataTable dt = (DataTable)ViewState["CurrentTable"];
LinkButton lb = (LinkButton)e.Row.FindControl("LinkButton1");
if (lb != null)
{
if (dt.Rows.Count > 1)
{
if (e.Row.RowIndex == dt.Rows.Count - 1)
{
lb.Visible = false;
}
}
else
{
lb.Visible = false;
}
}
}
}
//Deleting the Row
protected void LinkButton1_Click(object sender, EventArgs e)
{
LinkButton lb = (LinkButton)sender;
GridViewRow gvRow = (GridViewRow)lb.NamingContainer;
DataTable dt = (DataTable)ViewState["CurrentTable"];
dt.Rows.RemoveAt(gvRow.RowIndex);
this.Gridview1.DataSource = dt;
this.Gridview1.DataBind();
ViewState["CurrentTable"] = dt;
for (int i = 0; i < dt.Rows.Count; i++)
{
if (this.Gridview1.Rows[i].RowType == DataControlRowType.DataRow)
{
DropDownList box1 = (DropDownList)this.Gridview1.Rows[i].FindControl("DropDownList1");
TextBox box2 = (TextBox)this.Gridview1.Rows[i].FindControl("TextBox2");
TextBox box3 = (TextBox)this.Gridview1.Rows[i].FindControl("TextBox3");
box1.Text = dt.Rows[i]["Column1"].ToString();
box2.Text = dt.Rows[i]["Column2"].ToString();
box3.Text = dt.Rows[i]["Column3"].ToString();
}
}
}
Screenshot