Here i take one dropdownlist which will bind dynamically, one add button and another remove. Below i show .cs page code.
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.Data.SqlClient;
using System.Text;
public partial class Booking : System.Web.UI.Page
{
ClsCommon cls = new ClsCommon();
private void SetInitialRow()
{
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("Column1", typeof(string)));
dr = dt.NewRow();
dr["Column1"] = string.Empty;
dt.Rows.Add(dr);
//Store the DataTable in ViewState
ViewState["CurrentTable"] = dt;
Gridview1.DataSource = dt;
Gridview1.DataBind();
}
private void AddNewRowToGrid()
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
//extract the TextBox values
DropDownList box2 = (DropDownList)Gridview1.Rows[rowIndex].Cells[1].FindControl("ddl1");
drCurrentRow = dtCurrentTable.NewRow();
dtCurrentTable.Rows[i - 1]["Column2"] = box2.Text;
rowIndex++;
}
dtCurrentTable.Rows.Add(drCurrentRow);
ViewState["CurrentTable"] = dtCurrentTable;
Gridview1.DataSource = dtCurrentTable;
Gridview1.DataBind();
}
}
else
{
Response.Write("ViewState is null");
}
//Set Previous Data on Postbacks
SetPreviousData();
}
private 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 box2 = (DropDownList)Gridview1.Rows[rowIndex].Cells[2].FindControl("ddl1");
box2.Text = dt.Rows[i]["Column2"].ToString();
rowIndex++;
}
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
SetInitialRow();
}
}
protected void ButtonAdd_Click(object sender, EventArgs e)
{
AddNewRowToGrid();
}
protected void ButtonRemove_Click(object sender, EventArgs e)
{
//Button lb = (Button)sender;
ImageButton ib = (ImageButton)sender;
GridViewRow gvRow = (GridViewRow)ib.NamingContainer;
int rowID = gvRow.RowIndex;
if (ViewState["CurrentTable"] != null)
{
DataTable dt = (DataTable)ViewState["CurrentTable"];
if (dt.Rows.Count > 1)
{
if (gvRow.RowIndex < dt.Rows.Count - 1)
{
//Remove the Selected Row data
dt.Rows.Remove(dt.Rows[rowID]);
}
}
//Store the current data in ViewState for future reference
ViewState["CurrentTable"] = dt;
//Re bind the GridView for the updated data
Gridview1.DataSource = dt;
Gridview1.DataBind();
}
//Set Previous Data on Postbacks
SetPreviousData();
}
protected void Gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//(your connection string) SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[""].ConnectionString);
DropDownList ddlDropDownList = (DropDownList)e.Row.FindControl("ddl1");
if (ddlDropDownList != null)
{
SqlDataAdapter da = new SqlDataAdapter("select distinct(source) from Table_Name", con);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
ddlDropDownList.DataSource = dt;
ddlDropDownList.DataTextField = "source";
ddlDropDownList.DataValueField = "source";
ddlDropDownList.DataBind();
ddlDropDownList.Items.Insert(0, "--Select--");
}
else
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Source Not Available');", true);
}
}
}
}
protected void Gridview2_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//(your connection string) SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[""].ConnectionString);
DropDownList ddlDropDownList = (DropDownList)e.Row.FindControl("ddl4");
if (ddlDropDownList != null)
{
SqlDataAdapter da = new SqlDataAdapter("select distinct(sector) from table_name", con);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
ddlDropDownList.DataSource = dt;
ddlDropDownList.DataTextField = "sector";
ddlDropDownList.DataValueField = "sector";
ddlDropDownList.DataBind();
ddlDropDownList.Items.Insert(0, "--Select--");
}
else
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Package Not Available');", true);
}
}
}
}
protected void Gridview1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataTable dt = (DataTable)ViewState["CurrentTable"];
ImageButton ib = (ImageButton)e.Row.FindControl("ButtonRemove");
ImageButton ib1 = (ImageButton)e.Row.FindControl("ButtonAdd");
if (ib != null)
{
if (dt.Rows.Count > 1)
{
if (e.Row.RowIndex == dt.Rows.Count - 1)
{
ib.Visible = false;
}
}
else
{
ib.Visible = false;
}
}
if (ib1 != null)
{
if (dt.Rows.Count > 1)
{
if (e.Row.RowIndex == dt.Rows.Count - 1)
{
ib1.Visible = true;
}
else
{
ib1.Visible = false;
}
}
else
{
ib1.Visible = true;
}
}
}
}
protected void Button2_Click(object sender, EventArgs e)
{
if (ViewState["CurrentTable"] != null)
{
// SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[""].ConnectionString);
SqlDataAdapter da = new SqlDataAdapter("select max(user_id)+1 from table_name", con);
DataTable dt = new DataTable();
da.Fill(dt);
int usr_id;
if (dt.Rows[0][0].ToString() != "")
{
usr_id = Convert.ToInt32(dt.Rows[0][0].ToString());
}
else
{
usr_id = 100;
}
string pen = "pending";
SqlCommand cmd1 = new SqlCommand("insert into table_name values('" + usr_id + "','" + TextBox2.Text.ToString() + "','" + TextBox3.Text.ToString() + "','" + TextBox4.Text.ToString() + "','" + TextBox5.Text.ToString() + "','" + pen + "')", con);
Session["u_id"] = Convert.ToString(usr_id);
con.Open();
cmd1.ExecuteNonQuery();
con.Close();
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 0; i <= dtCurrentTable.Rows.Count - 1; i++)
{
//extract the TextBox values
TextBox box1 = (TextBox)Gridview1.Rows[i].Cells[0].FindControl("TextBox1");
DropDownList box2 = (DropDownList)Gridview1.Rows[i].Cells[1].FindControl("ddl1");
DropDownList box3 = (DropDownList)Gridview1.Rows[i].Cells[2].FindControl("ddl2");
DropDownList box4 = (DropDownList)Gridview1.Rows[i].Cells[3].FindControl("ddl3");
Label box5 = (Label)Gridview1.Rows[i].Cells[4].FindControl("label11");
Label box6 = (Label)Gridview1.Rows[i].Cells[5].FindControl("label22");
//string xyz = box2.SelectedItem.ToString();
DateTime date_arrival;
date_arrival = Convert.ToDateTime(box1.Text.ToString());
SqlCommand cmd = new SqlCommand("insert into booking(source,destination,vehicle,no_of_vehicle,amount,date_of_travel,user_id) values(@source,@destination,@vehicle,@no_of_vehicle,@amount,@date_of_travel,@user_id)", con);
SqlParameter param0 = new SqlParameter("@source", SqlDbType.NVarChar);
param0.Value = box2.SelectedItem.ToString();
cmd.Parameters.Add(param0);
SqlParameter param1 = new SqlParameter("@destination", SqlDbType.NVarChar);
param1.Value = box3.SelectedItem.ToString();
cmd.Parameters.Add(param1);
SqlParameter param2 = new SqlParameter("@vehicle", SqlDbType.VarChar);
param2.Value = box4.SelectedItem.ToString();
cmd.Parameters.Add(param2);
SqlParameter param3 = new SqlParameter("@no_of_vehicle", SqlDbType.Int);
param3.Value = Convert.ToInt32(box5.Text.ToString());
cmd.Parameters.Add(param3);
SqlParameter param4 = new SqlParameter("@amount", SqlDbType.Float);
param4.Value = Convert.ToDouble(box6.Text.ToString());
cmd.Parameters.Add(param4);
SqlParameter param5 = new SqlParameter("@date_of_travel", SqlDbType.Date);
param5.Value = date_arrival;
cmd.Parameters.Add(param5);
SqlParameter param6 = new SqlParameter("@user_id", SqlDbType.Int);
param6.Value = usr_id;
cmd.Parameters.Add(param6);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
else
{
Response.Write("ViewState is null");
}
DataTable dtt = (DataTable)ViewState["CurrentTable"];
ViewState["CurrentTable"] = null;
Response.Redirect("Default.aspx");
}
}
if you have any problem please mail me.
Thank you.....Happy codding....