AspdotnetCodes.com
Search
Articles
Books
Resources
Asp.Net News
Downloads
Free Tech Magazines
Asp.Net Web Hosting
Archives
Softwares
Newsletter
Suggest Us
Link to Us
Feeds Subscription
Articles
Questions & Answers
Tips & Tricks
 Multiple dropdown inside the gridview
Posted by Rahul mehta on  5/8/2013 1:39:21 AM Category: Asp.Net 2.0
Add to my favorites
Email to friend
I want 3 dropdown inside the gridview such as country, state & city for insert ,update ,delete in asp.net c#.

Thanks In Advance
Rahul Mehta

Post Your Reply Post New Question

 Viewer's Reply
Multiple dropdown inside the gridview
Answered By Rahul mehta on 5/31/2013 2:30:51 AM
---create below table in sql first----------------

CREATE TABLE [dbo].[org_main](
[org_id] [numeric](18, 0) IDENTITY(1,1) NOT NULL,
[name] [varchar](200) NULL,--org name

)



CREATE TABLE [dbo].[cat_main](
[cat_id] [numeric](18, 0) IDENTITY(1,1) NOT NULL,
[org_id] [numeric](18, 0) NOT NULL,
[name] [varchar](80) NULL,--cat name
)



CREATE TABLE [dbo].[dept_main](
[dept_id] [numeric](18, 0) IDENTITY(1,1) NOT NULL,
[cat_id] [numeric](18, 0) NOT NULL,
[org_id] [numeric](18, 0) NOT NULL,
[name] [varchar](80) NULL,---dept name

) ON [PRIMARY]


CREATE TABLE [dbo].[tour_main](
[tour_no] [numeric](18, 0) IDENTITY(1,1) NOT NULL,
[tdate] [varchar](15) NULL,
[exp_req] [numeric](9, 2) NULL,
[purpose] [varchar](100) NULL,
[state] [varchar](30) NOT NULL,
[active] [char](1) NULL,
CONSTRAINT [PK_tour_main] PRIMARY KEY CLUSTERED
(
[tour_no] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

CREATE TABLE [dbo].[tour_sub](
[tour_no] [numeric](18, 0) NOT NULL,
[feedback] [varchar](500) NULL,
[org] [varchar](100) NULL,
[cat] [varchar](100) NULL,
[org_id] [int] NULL,
[dept] [varchar](150) NULL,
[cat_id] [int] NULL,
[vdate] [varchar](20) NULL,
[visit] [char](2) NULL,
[toursubid] [int] IDENTITY(1,1) NOT NULL
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[tour_sub] WITH CHECK ADD CONSTRAINT [FK_tour_sub_tour_main] FOREIGN KEY([tour_no])
REFERENCES [dbo].[tour_main] ([tour_no])
GO
ALTER TABLE [dbo].[tour_sub] CHECK CONSTRAINT [FK_tour_sub_tour_main]


-------------------------------------------------------

------------------design-------------------

please note; i have used ajax toolkit for calendar


<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>

<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:DemoConnectionString %>"
SelectCommand="SELECT name, org_id FROM org_main ORDER BY name"></asp:SqlDataSource>
<style>
h3
{
color: MidnightBlue;
}
</style>
<style type="text/css">
.style2
{
width: 105px;
}
</style>
<table width="100%">
<tr>
<td>
<table>
<tr>
<td>
<asp:Button ID="cmdNew" runat="server" Text="New" OnClick="cmdNew_Click" />
</td>
<td>
<asp:Button ID="cmdModify" runat="server" Text="Modify" OnClick="cmdModify_Click" />
</td>
<td>
<asp:Button ID="cmdUpdate" runat="server" Text="Update" OnClick="cmdUpdate_Click"
ValidationGroup="G1" />
</td>
<td>
<asp:Button ID="cmdDelete" runat="server" Text="Delete" OnClick="cmdDelete_Click" />
</td>
<td>
<asp:Button ID="CmdSave" runat="server" Text="Save" OnClick="CmdSave_Click" ValidationGroup="G1" />
</td>
<td>
<asp:Button ID="cmdReset" runat="server" Text="Reset" OnClick="cmdReset_Click" />
</td>
<td>
<asp:Button ID="cmdExit" runat="server" Text="Exit" OnClick="cmdExit_Click" Style="height: 26px" />
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<table>
<tr>
<td class="style2">
Date
</td>
<td>
<asp:TextBox ID="tbStartDate" runat="server" autocomplete="off" Text="Click On Calendar" />
<asp:Button ID="cal1" runat="server" Text="Calendar" Visible="true" />
<ajaxToolkit:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="tbStartDate"
Enabled="True" PopupButtonID="cal1" Format="dd/MM/yyyy">
</ajaxToolkit:CalendarExtender>
</td>
</tr>
<tr>
<td class="style2">
State
</td>
<td>
<asp:DropDownList ID="ddstate" runat="server" Height="20px" Width="
 
Multiple dropdown inside the gridview
Answered By Rahul mehta on 5/31/2013 2:38:45 AM
--programming code ----

using System;
using System.Collections;
using System.Configuration;
using System.Data;
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.Data.SqlClient;
using System.Drawing;

public partial class Account_TourPlan : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["tiConnectionString"].ConnectionString);
SqlCommand cmd;
DataSet ds = new DataSet();
DataTable dt = new DataTable();
string str;
char value = 'Y';

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindEmployeeDetails();
GridView1.Enabled = false;
BindStatedropdown();
CmdSave.Enabled = false;
cmdUpdate.Enabled = false;
cmdModify.Enabled = true;
tbStartDate.ReadOnly = true;
txtpurpose.ReadOnly = true;
txtexp.ReadOnly = true;
ddstate.Enabled = false;
txtstate.Visible = false;
HiddenField6.Value = "";
}
}

protected void BindEmployeeDetails()
{
DataSet dsgv = new DataSet();
if (cmdNew.Enabled == true && HiddenField3.Value != "")
{
con.Open();
if (HiddenField3.Value != "")
{
SqlCommand cmd = new SqlCommand("Select tour_no,feedback,org,cat,org_id,dept,cat_id,vdate,visit,toursubid from tour_sub where tour_no = '" + HiddenField3.Value + "'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dsgv);
}

con.Close();

if (dsgv.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = dsgv;
GridView1.DataBind();
}
else
{
dsgv.Tables[0].Rows.Add(dsgv.Tables[0].NewRow());
GridView1.DataSource = dsgv;
GridView1.DataBind();
int columncount = GridView1.Rows[0].Cells.Count;
GridView1.Rows[0].Cells.Clear();
GridView1.Rows[0].Cells.Add(new TableCell());
GridView1.Rows[0].Cells[0].ColumnSpan = columncount;
GridView1.Rows[0].Cells[0].Text = "No Records Found";
}
}
else if (cmdNew.Enabled == false && HiddenField3.Value != "")
{
SqlCommand cmd = new SqlCommand("Select tour_no,feedback,org,cat,org_id,dept,cat_id,vdate,visit,toursubid from tour_sub where tour_no = '" + HiddenField3.Value + "'", con);
con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dsgv);
con.Close();

if (dsgv.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = dsgv;
GridView1.DataBind();
}
else
{
dsgv.Tables[0].Rows.Add(dsgv.Tables[0].NewRow());
GridView1.DataSource = dsgv;
GridView1.DataBind();
int columncount = GridView1.Rows[0].Cells.Count;
GridView1.Rows[0].Cells.Clear();
GridView1.Rows[0].Cells.Add(new TableCell());
GridView1.Rows[0].Cells[0].ColumnSpan = columncount;
GridView1.Rows[0].Cells[0].Text = "No Records Found";
}

}

else
{

SqlCommand cmd = new SqlCommand("Select tour_no,feedback,org,cat,org_id,dept,cat_id,vdate,visit,toursubid from tour_sub where toursubid = '" + 0 + "'", con);
con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dsgv);
con.Close();

if (dsgv.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = dsgv;
GridView1.DataBind();
}
else
{
dsgv.Tables[0].Rows.Add(dsgv.Tables[0].NewRow());
GridView1.DataSource = dsgv;
GridView1.DataBind();
int columncount = GridView1.Rows[0].Cells.Count;
GridView1.Rows[0].Cells.Clear();
GridView1.Rows[0].Cells.Add(new TableCell());
GridView1.Rows[0].Cells[0].ColumnSpan = columncount;
GridView1.Rows[0].Cells[0].Text = "No Records Found";
}
}
}

protected void BindStatedropdown()
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from State", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
ddstate.DataSource = ds;
ddstate.DataT
 
Multiple dropdown inside the gridview
Answered By Rahul mehta on 5/31/2013 2:39:39 AM
protected void BindStatedropdown()
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from State", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
ddstate.DataSource = ds;
ddstate.DataTextField = "statename";
ddstate.DataValueField = "stateid";
ddstate.DataBind();
ddstate.Items.Insert(0, new ListItem("Select", "0"));

}

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{

decimal EID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
if (EID != 0)
{
con.Open();
cmd = new SqlCommand("DELETE FROM TOUR_SUB WHERE toursubid=@EID", con);
cmd.Parameters.AddWithValue("@EID", EID);
cmd.ExecuteNonQuery();
con.Close();
HiddenField6.Value = "";
}
BindEmployeeDetails();
}

protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (ListBox1.Text != "" && cmdNew.Enabled == true)
{
cmdModify.Enabled = true;
cmdUpdate.Enabled = false;
tbStartDate.ReadOnly = true;
txtexp.ReadOnly = true;
txtpurpose.ReadOnly = true;
txtstate.ReadOnly = false;
ddstate.Enabled = false;

txtstate.Visible = true;
txtstate.ReadOnly = true;
con.Open();
SqlCommand cmd = new SqlCommand("select * from tour_main where tour_no = '" + ListBox1.SelectedValue + "'", con);
HiddenField3.Value = ListBox1.SelectedValue;
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
tbStartDate.Text = dr[1].ToString();
txtexp.Text = dr[2].ToString();
txtpurpose.Text = dr[3].ToString();
txtstate.Text = dr[4].ToString();
}
dr.Close();
con.Close();
GridView1.Enabled = false;
ddstate.Enabled = false;

}
BindEmployeeDetails();
}

protected void cmdNew_Click(object sender, EventArgs e)
{
cal1.Enabled = true;
txtstate.Visible = false;
txtexp.Text = "";
txtpurpose.Text = "";
tbStartDate.Text = "Click On Calendar";
ListBox1.Items.Clear();
cmdNew.Enabled = false;
CmdSave.Enabled = true;
cmdUpdate.Enabled = false;
cmdModify.Enabled = false;
cmdReset.Enabled = true;
cmdDelete.Enabled = false;
tbStartDate.ReadOnly = false;
txtpurpose.ReadOnly = false;
txtexp.ReadOnly = false;
ddstate.Enabled = true;
BindStatedropdown();
HiddenField3.Value = "";
BindEmployeeDetails();
GridView1.Enabled = false;
GridView1.Visible = true;
cmdReset.Text = "Back";

}

protected void CmdSave_Click(object sender, EventArgs e)
{
if (tbStartDate.Text != "" && ddstate.SelectedIndex != 0 && txtpurpose.Text != "" && txtexp.Text != "")
{
str = tbStartDate.Text;
con.Open();
cmd = new SqlCommand("Insert into tour_main(tdate,exp_req,purpose,state,active) Values('" + str + "','" + txtexp.Text + "','" + txtpurpose.Text + "','" + ddstate.SelectedItem.Text + "','" + value + "');select @@IDENTITY", con);
HiddenField3.Value = cmd.ExecuteScalar().ToString();
con.Close();
CmdSave.Enabled = true;
Page.RegisterStartupScript("msg", "<script language='javascript'>alert('U Can Now Fill Tour Plan Details')</script>");
if (HiddenField3.Value != "")
{
cmdReset.Text = "Back";
GridView1.Enabled = true;
txtexp.ReadOnly = true;
txtpurpose.ReadOnly = true;
tbStartDate.ReadOnly = true;
ddstate.Enabled = false;
CmdSave.Enabled = false;
cal1.Enabled = false;
BindEmployeeDetails();
}


}
else
{
Page.RegisterStartupScript("msg", "<script language='javascript'>alert('Please Fill All Details')</script>");
CmdSave.Enabled = true;
BindEmployeeDetails();
}

}

protected void cmdModify_Click(object sender, EventArgs e)
{
if (txtstate.Text != "")
{
cal1.Enabled = true;
cmdModify.Enabled = false;
cmdUpdate.Enabled = true;
tbStartDate.ReadOnly = false;
txtexp.ReadOnly = false;
txtpurpose.ReadOnly = false;
txtstate.ReadOnly = true;
ddstate.Enabled = true;
GridView1.En
 
Multiple dropdown inside the gridview
Answered By Rahul mehta on 5/31/2013 2:40:53 AM
protected void cmdModify_Click(object sender, EventArgs e)
{
if (txtstate.Text != "")
{
cal1.Enabled = true;
cmdModify.Enabled = false;
cmdUpdate.Enabled = true;
tbStartDate.ReadOnly = false;
txtexp.ReadOnly = false;
txtpurpose.ReadOnly = false;
txtstate.ReadOnly = true;
ddstate.Enabled = true;
GridView1.Enabled = true;
BindEmployeeDetails();
}
else
{
Page.RegisterStartupScript("msg", "<script language='javascript'>alert('Please Select Atleast 1 Item')</script>");
BindEmployeeDetails();
}
}

protected void cmdUpdate_Click(object sender, EventArgs e)
{
if (tbStartDate.Text != "" && txtstate.Text != "")
{
str = tbStartDate.Text;
con.Open();
cmd = new SqlCommand("update Tour_Main set tdate = '" + str + "',exp_req = '" + txtexp.Text + "',Purpose = '" + txtpurpose.Text + "',state = @state,Active = '" + value + "' where tour_no = '" + HiddenField3.Value + "'", con);

if (ddstate.SelectedIndex == 0)
{
cmd.Parameters.AddWithValue("@state", txtstate.Text);
}
else
{
cmd.Parameters.AddWithValue("@state", ddstate.SelectedItem.Text);
}
cmd.ExecuteNonQuery();
Page.RegisterStartupScript("msg", "<script language='javascript'>alert('Data Updated Successfully')</script>");
con.Close();
cmdReset_Click(sender, e);
}
else
{
Page.RegisterStartupScript("msg", "<script language='javascript'>alert('Please Fill All The Details')</script>");
BindEmployeeDetails();
}
}

protected void cmdDelete_Click(object sender, EventArgs e)
{
if (tbStartDate.Text != "" && txtstate.Text != "")
{
SqlCommand cmd1 = new SqlCommand("Delete from tour_sub where tour_no = '" + HiddenField3.Value + "'", con);
SqlCommand cmd = new SqlCommand("Delete from tour_main where tour_no = '" + HiddenField3.Value + "'", con);
con.Open();
cmd1.ExecuteNonQuery();
cmd.ExecuteNonQuery();
con.Close();
Page.RegisterStartupScript("msg", "<script language='javascript'>alert('Data Deleted Successfully')</script>");
cmdReset_Click(sender, e);
BindEmployeeDetails();
}
else
{
Page.RegisterStartupScript("msg", "<script language='javascript'>alert('Please Select Atleast 1 Item')</script>");
BindEmployeeDetails();
}
}

protected void cmdReset_Click(object sender, EventArgs e)
{

#region Again Filling Listbox

ListBox1.Items.Clear();
con.Open();
SqlCommand sqlCmd = new SqlCommand("SELECT * FROM Tour_Main order by Tour_no", con);
SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
con.Close();
sqlDa.Fill(dt);
if (dt.Rows.Count > 0)
{
ListBox1.DataBind();
}

#endregion

cmdReset.Text = "Reset";
cal1.Enabled = false;
txtstate.Text = "";
txtstate.Visible = false;
tbStartDate.ReadOnly = true;
txtpurpose.ReadOnly = true;
txtexp.ReadOnly = true;
ddstate.Enabled = false;
cmdModify.Enabled = true;
CmdSave.Enabled = false;
cmdDelete.Enabled = true;
CmdSave.Enabled = false;
tbStartDate.Text = "Click On Calendar";
txtexp.Text = "";
txtpurpose.Text = "";
BindStatedropdown();
cmdNew.Enabled = true;
HiddenField3.Value = "";
BindEmployeeDetails();
GridView1.Enabled = false;
}

protected void cmdExit_Click(object sender, EventArgs e)
{

}

protected void ddstate_SelectedIndexChanged(object sender, EventArgs e)
{
BindEmployeeDetails();
}

public DataTable load_department(string id)
{

DataTable dt = new DataTable();
if (id != "")
{
con.Open();
SqlCommand cmd = new SqlCommand("select dept_id,name from dept_main where cat_id ='" + id + "'", con);
con.Close();
SqlDataAdapter sd = new SqlDataAdapter(cmd);
sd.Fill(dt);
}
return dt;


}
public DataTable load_organisation()
{
DataTable dt = new DataTable();
con.Open();
SqlCommand cmd = new SqlCommand("select org_id,name from org_main", con);
con.Close();
SqlDataAdapter sd = new SqlDataAdapter(cmd);
sd.Fill(dt);
return dt;

}
 
Multiple dropdown inside the gridview
Answered By Rahul mehta on 5/31/2013 2:41:15 AM
public DataTable load_cat(string dc)
{

DataTable dt = new DataTable();
if (dc != "")
{
con.Open();
SqlCommand cmd = new SqlCommand("select distinct(cat_id),name from cat_main where org_id ='" + dc + "' ", con);
con.Close();
SqlDataAdapter sd = new SqlDataAdapter(cmd);
sd.Fill(dt);
}
return dt;

}

protected void binddropcat(string dc)
{
if (dc != "")
{

SqlCommand cmd1 = new SqlCommand("SELECT distinct(cat_id),name FROM CAT_MAIN where org_id ='" + dc + "' ", con);
SqlDataAdapter da = new SqlDataAdapter(cmd1);
SqlCommandBuilder builder1 = new SqlCommandBuilder(da);
DataSet ds1 = new DataSet();
da.Fill(ds1);
con.Close();

DropDownList retaorgg = (DropDownList)GridView1.FooterRow.FindControl("DropDownList1");
retaorgg.SelectedValue = HiddenField4.Value;
DropDownList ddocat = (DropDownList)GridView1.FooterRow.FindControl("DropDownList2");
ddocat.AppendDataBoundItems = false;
ddocat.DataSource = ds1;
ddocat.DataValueField = "cat_id";
ddocat.DataTextField = "name";
ddocat.DataBind();

}

}

protected void binddropdept(string dc)
{
if (dc != "")
{

SqlCommand cmd1 = new SqlCommand("SELECT dept_id,name FROM DEPT_MAIN where cat_id ='" + dc + "' ", con);
SqlDataAdapter da = new SqlDataAdapter(cmd1);
SqlCommandBuilder builder1 = new SqlCommandBuilder(da);
DataSet ds1 = new DataSet();
da.Fill(ds1);
con.Close();
DropDownList ogcat = (DropDownList)GridView1.FooterRow.FindControl("DropDownList1");
ogcat.SelectedValue = HiddenField4.Value;
DropDownList cat = (DropDownList)GridView1.FooterRow.FindControl("DropDownList2");
cat.SelectedValue = HiddenField5.Value;
DropDownList dept = (DropDownList)GridView1.FooterRow.FindControl("DropDownList3");
if (HiddenField7.Value != "")
{
dept.SelectedValue = HiddenField7.Value;
}
dept.DataSource = ds1;
dept.DataValueField = "dept_id";
dept.DataTextField = "name";
dept.DataBind();
HiddenField7.Value = "";
}
}

protected void dropdowncat(string dc)
{
if (dc != "")
{
SqlCommand cmd1 = new SqlCommand("SELECT distinct(cat_id),name FROM CAT_MAIN where org_id ='" + dc + "' ", con);
SqlDataAdapter da = new SqlDataAdapter(cmd1);
SqlCommandBuilder builder1 = new SqlCommandBuilder(da);
DataSet ds1 = new DataSet();
da.Fill(ds1);
con.Close();
DropDownList d2 = new DropDownList();

foreach (GridViewRow row in GridView1.Rows)
{
d2 = (DropDownList)row.FindControl("DropDownList2");
if (dc != null && d2 != null)
{
d2.AppendDataBoundItems = false;
d2.DataSource = ds1;
d2.DataValueField = "cat_id";
d2.DataTextField = "name";
d2.DataBind();
}
}
}
}

protected void dropdowndept(string dc)
{
if (dc != "")
{
SqlCommand cmd1 = new SqlCommand("SELECT dept_id,name FROM DEPT_MAIN where cat_id ='" + dc + "' ", con);
SqlDataAdapter da = new SqlDataAdapter(cmd1);
SqlCommandBuilder builder1 = new SqlCommandBuilder(da);
DataSet ds1 = new DataSet();
da.Fill(ds1);
con.Close();
DropDownList d3 = new DropDownList();
foreach (GridViewRow row in GridView1.Rows)
{
d3 = (DropDownList)row.FindControl("DropDownList3");
if (dc != null && d3 != null)
{
d3.DataSource = ds1;
d3.DataValueField = "dept_id";
d3.DataTextField = "name";
d3.DataBind();
}
}
}
}
 
Multiple dropdown inside the gridview
Answered By Rahul mehta on 5/31/2013 2:41:38 AM
protected void ddorg_SelectedIndexChanged(object sender, EventArgs e)
{
Decimal str;
str = Decimal.Parse(((DropDownList)(GridView1.FooterRow.FindControl("DropDownList1"))).SelectedValue);
HiddenField4.Value = str.ToString();
if (HiddenField6.Value == "")
{
BindEmployeeDetails();
string strorg = str.ToString();
binddropcat(strorg);
}
DropDownList d1 = new DropDownList();
foreach (GridViewRow row in GridView1.Rows)
{
d1 = (DropDownList)row.FindControl("DropDownList1");
if (d1 != null)
{

string str1 = d1.SelectedValue;
string strorg1 = str1.ToString();
dropdowncat(strorg1);

}
}
}

protected void ddcat_SelectedIndexChanged(object sender, EventArgs e)
{
string strcatid = "";
DropDownList d2 = new DropDownList();
HiddenField1.Value = "";
foreach (GridViewRow row in GridView1.Rows)
{
DropDownList d1 = new DropDownList();
d1 = (DropDownList)row.FindControl("DropDownList1");
d2 = (DropDownList)row.FindControl("DropDownList2");
if (d2 != null || d1 != null)
{
HiddenField1.Value = d1.SelectedValue;
str = d2.SelectedValue;

if (HiddenField1.Value != "")
{
cmd = new SqlCommand("select distinct(cat_id),name from cat_main where org_id ='" + HiddenField1.Value + "' and name ='" + str + "'", con);
con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
strcatid = dr[0].ToString();
HiddenField2.Value = strcatid;
}
if (strcatid == "")
{
con.Close();
}

if (strcatid != "")
{
dr.Close();
con.Close();
dropdowndept(strcatid);

}
if (strcatid == "")
{
string catid = "";
SqlCommand cmd1 = new SqlCommand("select distinct(cat_id),name from cat_main where org_id ='" + HiddenField1.Value + "' and cat_id ='" + str + "'", con);
con.Open();
SqlDataAdapter da1 = new SqlDataAdapter(cmd1);
DataSet ds1 = new DataSet();
da1.Fill(ds1);
dr = cmd1.ExecuteReader();
while (dr.Read())
{
catid = dr[0].ToString();
HiddenField2.Value = catid;
}
dr.Close();
con.Close();
dropdowndept(catid);
ddorg_SelectedIndexChanged(sender, e);
d2.SelectedValue = HiddenField2.Value;
}
}
}
}

if (HiddenField1.Value == "")
{
int catt;
catt = int.Parse(((DropDownList)(GridView1.FooterRow.FindControl("DropDownList2"))).SelectedValue);
DropDownList stcatt = (DropDownList)GridView1.FooterRow.FindControl("DropDownList2");
string cat = catt.ToString();
HiddenField5.Value = cat;
if (d2 == null)
{
ddorg_SelectedIndexChanged(sender, e);
}
HiddenField6.Value = "";
stcatt.SelectedValue = HiddenField5.Value;
binddropdept(cat);
}
}

protected void dddept_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList rdept = (DropDownList)GridView1.FooterRow.FindControl("DropDownList3");
HiddenField7.Value = rdept.SelectedValue;
ddorg_SelectedIndexChanged(sender, e);
binddropdept(HiddenField5.Value);

}
 
Multiple dropdown inside the gridview
Answered By Rahul mehta on 5/31/2013 2:41:58 AM
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
if (GridView1.EditIndex == -1)
{
HiddenField6.Value = "";
}
BindEmployeeDetails();
}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{

DataRowView drv = e.Row.DataItem as DataRowView;
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
DropDownList dorg = (DropDownList)e.Row.FindControl("DropDownList11");
DropDownList dp = (DropDownList)e.Row.FindControl("DropDownList1");
DropDownList cat = (DropDownList)e.Row.FindControl("DropDownList2");
DropDownList dept = (DropDownList)e.Row.FindControl("DropDownList3");

DataTable dt = load_organisation();

for (int i = 0; i < dt.Rows.Count; i++)
{
ListItem lt = new ListItem();
lt.Text = dt.Rows[i][1].ToString();
dorg.Items.Add(lt.Text);
}

str = drv[4].ToString();
dp.SelectedValue = drv[4].ToString();

DataTable dt1 = load_cat(str);
for (int i = 0; i < dt1.Rows.Count; i++)
{
ListItem ct = new ListItem();
ct.Text = dt1.Rows[i][1].ToString();
cat.Items.Add(ct);
}
cat.SelectedValue = drv[3].ToString();

string strdept;
strdept = drv[6].ToString();

DataTable dt2 = load_department(strdept);
for (int i = 0; i < dt2.Rows.Count; i++)
{
ListItem dpt = new ListItem();
dpt.Text = dt2.Rows[i][1].ToString();
dept.Items.Add(dpt);
}
dept.SelectedValue = drv[5].ToString();

}
}

else
{
con.Open();
cmd = new SqlCommand("select org_id,name from org_main", con);
SqlDataAdapter oda = new SqlDataAdapter(cmd);
SqlCommandBuilder builder = new SqlCommandBuilder(oda);
DataSet dt = new DataSet();
oda.Fill(dt);
con.Close();

//organisation

DropDownList ddl = (DropDownList)e.Row.FindControl("DropDownList11");
if (e.Row.RowType == DataControlRowType.Footer)
{
DropDownList ddors = (DropDownList)e.Row.FindControl("DropDownList11");
ddors.DataSource = dt;
ddors.DataValueField = "org_id";
ddors.DataTextField = "org_id";
ddors.DataBind();

}
}
}

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
if (GridView1.EditIndex == e.NewEditIndex)
{
HiddenField6.Value = "1";
}
BindEmployeeDetails();
}
 
Multiple dropdown inside the gridview
Answered By Rahul mehta on 5/31/2013 2:42:17 AM
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{

Label lb = (Label)GridView1.Rows[e.RowIndex].FindControl("lbleid");
DropDownList ddl = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("DropDownList1");
DropDownList dd2 = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("DropDownList2");
DropDownList dd3 = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("DropDownList3");
TextBox tx1 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1");
string date = ((TextBox)(GridView1.Rows[e.RowIndex].FindControl("txtdate"))).Text;
string visit = ((TextBox)(GridView1.Rows[e.RowIndex].FindControl("txtyn"))).Text;
string fb = ((TextBox)(GridView1.Rows[e.RowIndex].FindControl("txtfb"))).Text;
con.Open();
SqlCommand cmd = new SqlCommand("update tour_sub set org='" + ddl.SelectedItem.ToString() + "',cat='" + dd2.SelectedItem.ToString() + "',org_id ='" + ddl.SelectedValue + "',dept='" + dd3.SelectedItem + "',cat_id ='" + HiddenField2.Value + "', vdate='" + date + "',visit='" + visit + "',feedback='" + fb + "' where toursubid='" + lb.Text + "'", con);
cmd.ExecuteNonQuery();
con.Close();
GridView1.EditIndex = -1;
HiddenField6.Value = "";
BindEmployeeDetails();
}

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("Insert"))
{
DropDownList ddl = (DropDownList)GridView1.FooterRow.FindControl("DropDownList1");
DropDownList dd2 = (DropDownList)GridView1.FooterRow.FindControl("DropDownList2");
DropDownList dd3 = (DropDownList)GridView1.FooterRow.FindControl("DropDownList3");
TextBox date = (TextBox)GridView1.FooterRow.FindControl("txtdate");
TextBox visit = (TextBox)GridView1.FooterRow.FindControl("txtyesn");
TextBox fb = (TextBox)GridView1.FooterRow.FindControl("txtdfb");
if (ddl != null && dd2 != null && dd3 != null)
{
con.Open();
cmd = new SqlCommand("INSERT INTO tour_sub(tour_no,feedback,org,cat,org_id,dept,cat_id,vdate,visit) VALUES ('" + HiddenField3.Value + "','" + fb.Text + "','" + ddl.SelectedItem + "','" + dd2.SelectedItem + "','" + ddl.SelectedValue + "','" + dd3.SelectedItem + "','" + dd2.SelectedValue + "','" + date.Text + "','" + visit.Text + "')", con);
cmd.ExecuteNonQuery();
con.Close();
BindEmployeeDetails();
HiddenField6.Value = "";
}
else
{
Page.RegisterStartupScript("msg", "<script language='javascript'>alert('Please Fill All The Details ')</script>");
BindEmployeeDetails();
}
}
}

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
BindEmployeeDetails();
}

protected void GridView1_PageIndexChanged(object sender, EventArgs e)
{

}
 
Multiple dropdown inside the gridview
Answered By Rahul mehta on 5/31/2013 2:45:23 AM
--design part -- i have not taken inside html code of asp.net design page so please u all who will be using write this code inside html syntax

<tr>
<td class="style2">
State
</td>
<td>
<asp:DropDownList ID="ddstate" runat="server" Height="20px" Width="144px" AutoPostBack="True"
OnSelectedIndexChanged="ddstate_SelectedIndexChanged">
</asp:DropDownList>
<asp:TextBox ID="txtstate" runat="server" Visible="false"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
Expenses Req
</td>
<td>
<asp:TextBox ID="txtexp" runat="server" AutoComplete="off" MaxLength="12"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ControlToValidate="txtexp"
ErrorMessage="Please Enter Only Numbers" ValidationExpression="^(\d{0,13}\.\d{0,5}|\d{0,13})$"
ValidationGroup="G1"></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td class="style2">
Purpose
</td>
<td>
<asp:TextBox ID="txtpurpose" runat="server" AutoComplete="off" MaxLength="100"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Enter alphabets"
ControlToValidate="txtpurpose" ValidationGroup="G1" ValidationExpression="[a-zA-Z\s]+"></asp:RegularExpressionValidator>
</td>
</tr>
</table>
<asp:HiddenField ID="HiddenField1" runat="server" />
<asp:HiddenField ID="HiddenField2" runat="server" />
<asp:HiddenField ID="HiddenField3" runat="server" />
<asp:HiddenField ID="HiddenField4" runat="server" />
<asp:HiddenField ID="HiddenField5" runat="server" />
<asp:HiddenField ID="HiddenField6" runat="server" />
<asp:HiddenField ID="HiddenField7" runat="server" />
<td valign="top">
<asp:ListBox ID="ListBox1" runat="server" Height="117px" Width="250px" AutoPostBack="True"
OnSelectedIndexChanged="ListBox1_SelectedIndexChanged" DataSourceID="SqlDataSource1"
DataTextField="tour_no" DataValueField="tour_no"></asp:ListBox>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:demoConnectionString48 %>"
SelectCommand="SELECT [tour_no] FROM [tour_main]"></asp:SqlDataSource>
</td>
</td>
</tr>
</table>
<h3>
Tour Plan Details
</h3>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound"
OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" DataKeyNames="toursubid"
ShowFooter="True" OnRowCommand="GridView1_RowCommand" OnRowUpdating="GridView1_RowUpdating"
BackColor="White" BorderColor="White" BorderStyle="Ridge" BorderWidth="1px" CellPadding="2"
GridLines="None" CellSpacing="1" OnRowCancelingEdit="GridView1_RowCancelingEdit"
AllowPaging="true" PageSize="6" OnPageIndexChanged="GridView1_PageIndexChanged"
OnPageIndexChanging="GridView1_PageIndexChanging">
<PagerSettings Mode="NumericFirstLast" PageButtonCount="4" FirstPageText="First"
LastPageText="Last" />
<Columns>
<asp:TemplateField HeaderText="Action" ControlStyle-Width="35">
<EditItemTemplate>
<asp:Label ID="lbleid" runat="server" Text='<%#Eval("toursubid") %>' Visible="false"></asp:Label>
</EditItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="lnkinsert" runat="server" Text="Insert" CommandName="Insert"></asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Organisation" ControlStyle-Width="130px">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("org") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" ControlStyle-Width="130px" runat="server" AutoPostBack="true"
 
Multiple dropdown inside the gridview
Answered By Rahul mehta on 5/31/2013 2:46:39 AM
<asp:TemplateField HeaderText="Organisation" ControlStyle-Width="130px">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("org") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" ControlStyle-Width="130px" runat="server" AutoPostBack="true"
OnSelectedIndexChanged="ddorg_SelectedIndexChanged" DataSourceID="SqlDataSource2"
DataTextField="name" DataValueField="org_id">
</asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="DropDownList1" ControlStyle-Width="130px" runat="server" AutoPostBack="true"
OnSelectedIndexChanged="ddorg_SelectedIndexChanged" DataSourceID="SqlDataSource2"
DataTextField="name" DataValueField="org_id">
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="org_id" Visible="false">
<ItemTemplate>
<asp:Label ID="Label11" runat="server" Visible="false" Text='<%# Eval("org_id") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DropDownList11" runat="server" Visible="false" AutoPostBack="true">
</asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="DropDownList11" runat="server" Visible="false" AutoPostBack="true">
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Category" ControlStyle-Width="130px">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("cat") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DropDownList2" ControlStyle-Width="130px" runat="server" AutoPostBack="true"
OnSelectedIndexChanged="ddcat_SelectedIndexChanged" AppendDataBoundItems="true">
</asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="DropDownList2" ControlStyle-Width="130px" runat="server" AutoPostBack="true"
OnSelectedIndexChanged="ddcat_SelectedIndexChanged" AppendDataBoundItems="true">
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Department" ControlStyle-Width="130px">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("dept") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DropDownList3" OnSelectedIndexChanged="dddept_SelectedIndexChanged"
ControlStyle-Width="130px" runat="server" AutoPostBack="true">
</asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="DropDownList3" OnSelectedIndexChanged="dddept_SelectedIndexChanged"
ControlStyle-Width="130px" runat="server" AutoPostBack="true">
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date">
<ItemTemplate>
<%#Eval("vdate") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtdate" runat="server" AutoComplete="OFF" Text='<%#Eval("vdate") %>'
Width="75px"></asp:TextBox>
<asp:Button ID="calfd" runat="server" Text="Calendar" Visible="true" Width="67px" />
<ajaxToolkit:CalendarExtender ID="CalendarExtender2" runat="server" TargetControlID="txtdate"
Format="dd/MM/yyyy" PopupButtonID="calfd">
</ajaxToolkit:CalendarExtender>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtdate" runat="server" AutoComplete="OFF" Width="75px"></asp:TextBox>
<asp:Button ID="calfd" runat="server" Text="Calendar" Visible="true" Width="67px" />
<ajaxToolkit:CalendarExtender ID="CalendarExtender2" runat="server" Format="dd/MM/yyyy"
PopupButtonID="calfd" TargetControlID="txtdate">
</ajaxToolkit:CalendarExtender>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField
 
Multiple dropdown inside the gridview
Answered By Rahul mehta on 5/31/2013 2:47:30 AM
<asp:TemplateField HeaderText="Y/N">
<ItemTemplate>
<%#Eval("visit")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtyn" runat="server" Text='<%#Eval("visit") %>' Width="25px" MaxLength="1"
AutoComplete="OFF"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtyesn" runat="server" Width="25px" MaxLength="1" AutoComplete="OFF"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="FeedBack" ControlStyle-Width="125px">
<ItemTemplate>
<%#Eval("feedback")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtfb" runat="server" ControlStyle-Width="125px" Text='<%#Eval("feedback") %>'
Width="70px" AutoComplete="OFF"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtdfb" runat="server" ControlStyle-Width="125px" AutoComplete="OFF"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="true" ControlStyle-ForeColor="Blue ">
<ControlStyle ForeColor="Blue"></ControlStyle>
</asp:CommandField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton CommandName="Delete" CommandArgument='<%#Eval("toursubid") %>' runat="server"
ID="lnkdelete" Text="Delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />
<PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
<RowStyle BackColor="#DEDFDE" ForeColor="Black" />
<SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#594B9C" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#33276A" />
</asp:GridView>
<br />
</asp:Content>
 
Post Your Reply If you still have problem, post it again under this reply for better response.

 
Sponsored by
Introduction to Software Development
Integrating Silverlight 4 with SharePoint 2010 - Free 42 Page Sample Chapter
No Nonsense XML Web Development With PHP - Free 146 Page Preview!