using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
//using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Specialized;
namespace autorowgenaration
{
public partial class WebForm1 : System.Web.UI.Page
{
private ArrayList ddlist()
{
ArrayList arl = new ArrayList();
arl.Add(new ListItem("feel", "1"));
arl.Add(new ListItem("feel2", "2"));
return arl;
}
private void fildrop(DropDownList ddl)
{
ArrayList arl = ddlist();
foreach (ListItem item in arl)
{
ddl.Items.Add(item);
}
}
protected void setfirstrow()
{
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("rowno", typeof(string)));
dt.Columns.Add(new DataColumn("col1", typeof(string)));
dt.Columns.Add(new DataColumn("col2", typeof(string)));
dt.Columns.Add(new DataColumn("col3", typeof(string)));
dt.Columns.Add(new DataColumn("col4",typeof(string)));
dr = dt.NewRow();
dr["rowno"] = 1;
dt.Rows.Add(dr);
ViewState["currenttable"] = dt;
GridView1.DataSource = dt;
GridView1.DataBind();
DropDownList ddl1 = (DropDownList)GridView1.Rows[0].Cells[1].FindControl("ddlfeelings");
DropDownList ddl2 = (DropDownList)GridView1.Rows[0].Cells[2].FindControl("ddlno");
DropDownList ddl3 = (DropDownList)GridView1.Rows[0].Cells[3].FindControl("ddlday");
DropDownList ddl4 = (DropDownList)GridView1.Rows[0].Cells[4].FindControl("ddlspecifications");
fildrop(ddl1);
fildrop(ddl2);
fildrop(ddl3);
fildrop(ddl4);
}
protected void addnewrow()
{
if (ViewState["currenttable"] != null)
{
DataTable dtcurrenttable = (DataTable)ViewState["currenttable"];
DataRow drCurrentRow = null;
if (dtcurrenttable.Rows.Count > 0)
{
drCurrentRow = dtcurrenttable.NewRow();
drCurrentRow["rowno"] = dtcurrenttable.Rows.Count + 1;
dtcurrenttable.Rows.Add(drCurrentRow);
ViewState["CurrentTable"] = dtcurrenttable;
for (int i = 0; i < dtcurrenttable.Rows.Count - 1; i++)
{
DropDownList ddl1 = (DropDownList)GridView1.Rows[i].Cells[1].FindControl("ddlfeelings");
DropDownList ddl2 = (DropDownList)GridView1.Rows[i].Cells[2].FindControl("ddlno");
DropDownList ddl3 = (DropDownList)GridView1.Rows[i].Cells[3].FindControl("ddlday");
DropDownList ddl4 = (DropDownList)GridView1.Rows[i].Cells[4].FindControl("ddlspecifications");
dtcurrenttable.Rows[i]["col1"] = ddl1.SelectedItem.Text;
dtcurrenttable.Rows[i]["col2"] = ddl2.SelectedItem.Text;
dtcurrenttable.Rows[i]["col3"] = ddl3.SelectedItem.Text;
dtcurrenttable.Rows[i]["col4"] = ddl4.SelectedItem.Text;
}
GridView1.DataSource = dtcurrenttable;
GridView1.DataBind();
}
}
else
{
Response.Write("ViewState is null");
}
setpreviousdata();
}
protected void setpreviousdata()
{
int rowIndex = 0;
if (ViewState["currenttable"] != null)
{
DataTable dt = (DataTable)ViewState["currenttable"];
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
DropDownList ddl1 = (DropDownList)GridView1.Rows[rowIndex].Cells[1].FindControl("ddlfeelings");
DropDownList ddl2 = (DropDownList)GridView1.Rows[rowIndex].Cells[2].FindControl("ddlno");
DropDownList ddl3 = (DropDownList)GridView1.Rows[rowIndex].Cells[3].FindControl("ddlday");
DropDownList ddl4 = (DropDownList)GridView1.Rows[rowIndex].Cells[3].FindControl("ddlspecifications");
fildrop(ddl1);
fildrop(ddl2);
fildrop(ddl3);
fildrop(ddl4);
if (i < dt.Rows.Count - 1)
{
ddl1.ClearSelection();
ddl1.Items.FindByText(dt.Rows[i]["col1"].ToString()).Selected = true;
ddl2.ClearSelection();
ddl2.Items.FindByText(dt.Rows[i]["col2"].ToString()).Selected = true;
ddl3.ClearSelection();
ddl3.Items.FindByText(dt.Rows[i]["col3"].ToString()).Selected = true;
ddl4.ClearSelection();
ddl4.Items.FindByText(dt.Rows[i]["col4"].ToString()).Selected = true;
}
rowIndex++;
}
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
setfirstrow();
}
}
protected void btnadd_Click(object sender, EventArgs e)
{
addnewrow();
}
}
}
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
//using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Specialized;
namespace autorowgenaration
{
public partial class WebForm1 : System.Web.UI.Page
{
private ArrayList ddlist()
{
ArrayList arl = new ArrayList();
arl.Add(new ListItem("feel", "1"));
arl.Add(new ListItem("feel2", "2"));
return arl;
}
private void fildrop(DropDownList ddl)
{
ArrayList arl = ddlist();
foreach (ListItem item in arl)
{
ddl.Items.Add(item);
}
}
protected void setfirstrow()
{
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("rowno", typeof(string)));
dt.Columns.Add(new DataColumn("col1", typeof(string)));
dt.Columns.Add(new DataColumn("col2", typeof(string)));
dt.Columns.Add(new DataColumn("col3", typeof(string)));
dt.Columns.Add(new DataColumn("col4",typeof(string)));
dr = dt.NewRow();
dr["rowno"] = 1;
dt.Rows.Add(dr);
ViewState["currenttable"] = dt;
GridView1.DataSource = dt;
GridView1.DataBind();
DropDownList ddl1 = (DropDownList)GridView1.Rows[0].Cells[1].FindControl("ddlfeelings");
DropDownList ddl2 = (DropDownList)GridView1.Rows[0].Cells[2].FindControl("ddlno");
DropDownList ddl3 = (DropDownList)GridView1.Rows[0].Cells[3].FindControl("ddlday");
DropDownList ddl4 = (DropDownList)GridView1.Rows[0].Cells[4].FindControl("ddlspecifications");
fildrop(ddl1);
fildrop(ddl2);
fildrop(ddl3);
fildrop(ddl4);
}
protected void addnewrow()
{
if (ViewState["currenttable"] != null)
{
DataTable dtcurrenttable = (DataTable)ViewState["currenttable"];
DataRow drCurrentRow = null;
if (dtcurrenttable.Rows.Count > 0)
{
drCurrentRow = dtcurrenttable.NewRow();
drCurrentRow["rowno"] = dtcurrenttable.Rows.Count + 1;
dtcurrenttable.Rows.Add(drCurrentRow);
ViewState["CurrentTable"] = dtcurrenttable;
for (int i = 0; i < dtcurrenttable.Rows.Count - 1; i++)
{
DropDownList ddl1 = (DropDownList)GridView1.Rows[i].Cells[1].FindControl("ddlfeelings");
DropDownList ddl2 = (DropDownList)GridView1.Rows[i].Cells[2].FindControl("ddlno");
DropDownList ddl3 = (DropDownList)GridView1.Rows[i].Cells[3].FindControl("ddlday");
DropDownList ddl4 = (DropDownList)GridView1.Rows[i].Cells[4].FindControl("ddlspecifications");
dtcurrenttable.Rows[i]["col1"] = ddl1.SelectedItem.Text;
dtcurrenttable.Rows[i]["col2"] = ddl2.SelectedItem.Text;
dtcurrenttable.Rows[i]["col3"] = ddl3.SelectedItem.Text;
dtcurrenttable.Rows[i]["col4"] = ddl4.SelectedItem.Text;
}
GridView1.DataSource = dtcurrenttable;
GridView1.DataBind();
}
}
else
{
Response.Write("ViewState is null");
}
setpreviousdata();
}
protected void setpreviousdata()
{
int rowIndex = 0;
if (ViewState["currenttable"] != null)
{
DataTable dt = (DataTable)ViewState["currenttable"];
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
DropDownList ddl1 = (DropDownList)GridView1.Rows[rowIndex].Cells[1].FindControl("ddlfeelings");
DropDownList ddl2 = (DropDownList)GridView1.Rows[rowIndex].Cells[2].FindControl("ddlno");
DropDownList ddl3 = (DropDownList)GridView1.Rows[rowIndex].Cells[3].FindControl("ddlday");
DropDownList ddl4 = (DropDownList)GridView1.Rows[rowIndex].Cells[3].FindControl("ddlspecifications");
fildrop(ddl1);
fildrop(ddl2);
fildrop(ddl3);
fildrop(ddl4);
if (i < dt.Rows.Count - 1)
{
ddl1.ClearSelection();
ddl1.Items.FindByText(dt.Rows[i]["col1"].ToString()).Selected = true;
ddl2.ClearSelection();
ddl2.Items.FindByText(dt.Rows[i]["col2"].ToString()).Selected = true;
ddl3.ClearSelection();
ddl3.Items.FindByText(dt.Rows[i]["col3"].ToString()).Selected = true;
ddl4.ClearSelection();
ddl4.Items.FindByText(dt.Rows[i]["col4"].ToString()).Selected = true;
}
rowIndex++;
}
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
setfirstrow();
}
}
protected void btnadd_Click(object sender, EventArgs e)
{
addnewrow();
}
}
}
--------------aspx--------------------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="autorowgenaration.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.style1
{
width: 100%;
}
.style2
{
width: 182px;
}
.style3
{
width: 130px;
}
.style4
{
width: 143px;
}
.style5
{
width: 174px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<table id="tblfeel" class="style1">
<tr>
<td class="style2">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="rowno" HeaderText="sno" />
<asp:TemplateField HeaderText="feelings">
<ItemTemplate>
<asp:DropDownList ID="ddlfeelings" runat="server">
<asp:ListItem>i am feeling</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="no">
<ItemTemplate>
<asp:DropDownList ID="ddlno" runat="server">
<asp:ListItem>--no--</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="day">
<ItemTemplate>
<asp:DropDownList ID="ddlday" runat="server">
<asp:ListItem>Day</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="specifications">
<ItemTemplate>
<asp:DropDownList ID="ddlspecifications" runat="server">
<asp:ListItem>Specification</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="btnadd" runat="server" Text="Add new row" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</td>
<td class="style3">
</td>
<td class="style4">
</td>
<td class="style5">
</td>
<td>
</td>
</tr>
</table>
<asp:Button ID="btnadd" runat="server" onclick="btnadd_Click" Text="ADD" />
</form>
</body>
</html>