Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
.NET
.NET Core
.NET MAUI
.NET Standard
Active Directory
ADO.NET
Agile Development
AI
AJAX
AlbertAGPT
Alchemy
Alexa Skills
Algorand
Algorithms in C#
Android
Angular
ArcObject
ASP.NET
ASP.NET Core
Augmented Reality
Avalanche
AWS
Azure
Backbonejs
Big Data
BizTalk Server
Blazor
Blockchain
Bootstrap
Bot Framework
Business
Business Intelligence(BI)
C#
C# Corner
C# Strings
C, C++, MFC
Career Advice
Careers and Jobs
Chapters
ChatGPT
Cloud
Coding Best Practices
Cognitive Services
COM Interop
Compact Framework
Copilot
Cortana Development
Cosmos DB
Cryptocurrency
Cryptography
Crystal Reports
CSS
Current Affairs
Custom Controls
Cyber Security
Data Mining
Data Science
Databases & DBA
Databricks
Design Patterns & Practices
DevExpress
DevOps
DirectX
Dynamics CRM
Enterprise Development
Entity Framework
Error Zone
Exception Handling
F#
Files, Directory, IO
Flutter
Games Programming
GDI+
General
Generative AI
GO
Google Cloud
Google Development
Graphics Design
Graphite Studio
Hardware
Hiring and Recruitment
HoloLens
How do I
HTML 5
Infragistics
Internet & Web
Internet of Things
Ionic
Java
Java and .NET
JavaScript
JQuery
JSON
JSP
Knockout
Kotlin
Langchain
Leadership
Learn .NET
Learn iOS Programming
LINQ
Machine Learning
Metaverse
Microsoft 365
Microsoft Fabric
Microsoft Office
Microsoft Phone
Microsoft Teams
Mobile Development
MongoDB
MuleSoft
MySQL
NEAR
NetBeans
Networking
NFT
NoCode LowCode
Node.js
Office Development
OOP/OOD
Open Source
Operating Systems
Oracle
Outsourcing
Philosophy
PHP
Polygon
PostgreSQL
Power Apps
Power Automate
Power BI
Power Pages
Printing in C#
Products
Progress
Progressive Web Apps
Project Management
Public Speaking
Python
Q#
QlikView
Quantum Computing
R
React
React Native
Reports using C#
Robotics & Hardware
RPA
Ruby on Rails
RUST
Salesforce
Security
Servers
ServiceNow
SharePoint
Sharp Economy
SignalR
Smart Devices
Snowflake
Software Architecture/Engineering
Software Testing
Solana
Solidity
Sports
SQL
SQL Server
Startups
Stratis Blockchain
Swift
SyncFusion
Threading
Tools
TypeScript
Unity
UWP
Visual Basic .NET
Visual Studio
Vue.js
WCF
Wearables
Web API
Web Design
Web Development
Web3
Windows
Windows Controls
Windows Forms
Windows PowerShell
Windows Services
Workflow Foundation
WPF
Xamarin
XAML
XML
XNA
XSharp
Register
Login
1
Answer
how to add dropdown list with gridview and update when sele
indradeo kumar
5y
1.1k
1
Reply
Dear Team,
kindly find my code details and help to complate same still i unable to complate database structur.
.aspx==
<
%@ Page
Language
=
"C#"
AutoEventWireup
=
"true"
Codefile
=
"DeptCotractDetai;s.aspx.cs"
Inherits
=
"Demoexsql.DeptCotractDetai_s"
%
>
<!DOCTYPE html
>
<
html
xmlns
=
"http://www.w3.org/1999/xhtml"
>
<
head
runat
=
"server"
>
<
title
>
</
title
>
</
head
>
<
body
>
<
form
id
=
"form1"
runat
=
"server"
>
<
div
>
<
asp:GridView
ID
=
"GridView1"
runat
=
"server"
AutoGenerateColumns
=
"false"
EnableViewState
=
"true"
OnRowDataBound
=
"GridView1_RowDataBound"
>
<
Columns
>
<
asp:BoundField
HeaderText
=
"Id"
DataField
=
"CountryId"
/>
<
asp:TemplateField
HeaderText
=
"Country"
>
<
ItemTemplate
>
<
asp:DropDownList
ID
=
"ddlCountries"
runat
=
"server"
AutoPostBack
=
"true"
OnSelectedIndexChanged
=
"drpcountry_SelectedIndexChanged"
>
</
asp:DropDownList
>
</
ItemTemplate
>
</
asp:TemplateField
>
<
asp:TemplateField
HeaderText
=
"State"
>
<
ItemTemplate
>
<
asp:DropDownList
ID
=
"ddlState"
runat
=
"server"
AutoPostBack
=
"true"
OnSelectedIndexChanged
=
"drpstate_SelectedIndexChanged"
>
</
asp:DropDownList
>
</
ItemTemplate
>
</
asp:TemplateField
>
<
asp:TemplateField
HeaderText
=
"City/Town"
>
<
ItemTemplate
>
<
asp:DropDownList
runat
=
"server"
ID
=
"ddlCity"
ClientIDMode
=
"Static"
Width
=
"100px"
>
</
asp:DropDownList
>
</
ItemTemplate
>
</
asp:TemplateField
>
</
Columns
>
</
asp:GridView
>
<
br
/>
<
asp:Button
ID
=
"Button1"
Text
=
"Save"
runat
=
"server"
OnClick
=
"Save"
/>
<
br
/>
<
asp:GridView
ID
=
"Gridview2"
runat
=
"server"
AutoGenerateColumns
=
"false"
>
<
Columns
>
<
asp:BoundField
HeaderText
=
"Country"
DataField
=
"Country"
/>
<
asp:BoundField
HeaderText
=
"State"
DataField
=
"State"
/>
<
asp:BoundField
HeaderText
=
"City"
DataField
=
"City"
/>
</
Columns
>
</
asp:GridView
>
</
div
>
</
form
>
</
body
>
</
html
>
.aspx.cs==
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Data;
using
System.Data.SqlClient;
using
System.Configuration;
namespace
Demoexsql
{
public
partial
class
DeptCotractDetai_s : System.Web.UI.Page
{
protected
void
Page_Load(
object
sender, EventArgs e)
{
if
(!IsPostBack)
{
GridView1.DataSource = GetData(
"SELECT * FROM Countries"
);
GridView1.DataBind();
}
DataTable dt =
new
DataTable();
dt.Columns.AddRange(
new
DataColumn[] {
new
DataColumn(
"Country"
,
typeof
(
string
)),
new
DataColumn(
"State"
,
typeof
(
string
)),
new
DataColumn(
"City"
,
typeof
(
string
)) });
DropDownList ddlCountries =
null
;
DropDownList ddlStates =
null
;
DropDownList ddlCities =
null
;
foreach
(GridViewRow row
in
GridView1.Rows)
{
if
(row.RowType == DataControlRowType.DataRow)
{
ddlCountries = (row.FindControl(
"ddlCountries"
)
as
DropDownList);
ddlStates = (row.FindControl(
"ddlState"
)
as
DropDownList);
ddlCities = (row.FindControl(
"ddlCity"
)
as
DropDownList);
string
country = (ddlCountries.SelectedItem.Value ==
"0"
?
string
.Empty : ddlCountries.SelectedItem.Value);
string
state = (ddlStates.SelectedItem.Value ==
"0"
?
string
.Empty : ddlStates.SelectedItem.Value);
string
city = (ddlCities.SelectedItem.Value ==
"0"
?
string
.Empty : ddlCities.SelectedItem.Value);
if
(country !=
""
|| state !=
""
|| city !=
""
)
{
dt.Rows.Add(ddlCountries.SelectedItem.Text ==
"Select Country"
?
"NULL"
: ddlCountries.SelectedItem.Text, ddlStates.SelectedItem.Text ==
"Select State"
?
"NULL"
: ddlStates.SelectedItem.Text, ddlCities.SelectedItem.Text ==
"Select City"
?
"NULL"
: ddlCities.SelectedItem.Text);
}
else
{
dt.Rows.Add(country ==
""
?
"NULL"
: ddlCountries.SelectedItem.Value, country ==
""
?
"NULL"
: ddlStates.SelectedItem.Value, country ==
""
?
"NULL"
: ddlCities.SelectedItem.Value);
}
}
Gridview2.DataSource = dt;
Gridview2.DataBind();
}
}
private
DataSet GetData(
string
query)
{
string
conString = ConfigurationManager.ConnectionStrings[
"constr"
].ConnectionString;
SqlCommand cmd =
new
SqlCommand(query);
using
(SqlConnection con =
new
SqlConnection(conString))
{
using
(SqlDataAdapter sda =
new
SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using
(DataSet ds =
new
DataSet())
{
sda.Fill(ds);
return
ds;
}
}
}
}
private
void
BindDropDownList(DropDownList ddl,
string
query,
string
text,
string
value,
string
defaultText)
{
string
conString = ConfigurationManager.ConnectionStrings[
"constr"
].ConnectionString;
SqlCommand cmd =
new
SqlCommand(query);
using
(SqlConnection con =
new
SqlConnection(conString))
{
using
(SqlDataAdapter sda =
new
SqlDataAdapter())
{
cmd.Connection = con;
con.Open();
ddl.DataSource = cmd.ExecuteReader();
ddl.DataTextField = text;
ddl.DataValueField = value;
ddl.DataBind();
con.Close();
}
}
ddl.Items.Insert(0,
new
ListItem(defaultText,
"0"
));
}
protected
void
GridView1_RowDataBound(
object
sender, GridViewRowEventArgs e)
{
DropDownList ddlCountries =
null
;
DropDownList ddlStates =
null
;
DropDownList ddlCities =
null
;
if
(e.Row.RowType == DataControlRowType.DataRow)
{
ddlCountries = (e.Row.FindControl(
"ddlCountries"
)
as
DropDownList);
ddlStates = (e.Row.FindControl(
"ddlState"
)
as
DropDownList);
ddlCities = (e.Row.FindControl(
"ddlCity"
)
as
DropDownList);
BindDropDownList(ddlCountries,
"SELECT DISTINCT CountryName,CountryId FROM Countries"
,
"CountryName"
,
"CountryId"
,
"Select Country"
);
BindDropDownList(ddlStates,
"SELECT DISTINCT StateName,StateId FROM States"
,
"StateName"
,
"StateId"
,
"Select Country"
);
BindDropDownList(ddlCities,
"SELECT DISTINCT CityName,CityId FROM Cities"
,
"CityName"
,
"CityId"
,
"Select Country"
);
ddlStates.Enabled =
false
;
ddlCities.Enabled =
false
;
ddlStates.Items.Insert(0,
new
ListItem(
"Select State"
,
"0"
));
ddlCities.Items.Insert(0,
new
ListItem(
"Select City"
,
"0"
));
}
}
protected
void
drpcountry_SelectedIndexChanged(
object
sender, EventArgs e)
{
DropDownList ddlCountries = (DropDownList)sender;
GridViewRow currentRow = (GridViewRow)ddlCountries.NamingContainer;
DropDownList ddlStates = (DropDownList)currentRow.FindControl(
"ddlState"
);
DropDownList ddlCities = (DropDownList)currentRow.FindControl(
"ddlCity"
);
ddlStates.Enabled =
false
;
ddlCities.Enabled =
false
;
ddlStates.Items.Clear();
ddlCities.Items.Clear();
ddlStates.Items.Insert(0,
new
ListItem(
"Select State"
,
"0"
));
ddlCities.Items.Insert(0,
new
ListItem(
"Select City"
,
"0"
));
int
countryId =
int
.Parse(ddlCountries.SelectedItem.Value);
if
(countryId > 0)
{
string
query =
string
.Format(
"select StateId, StateName from States where CountryId = {0}"
, countryId);
BindDropDownList(ddlStates, query,
"StateName"
,
"StateId"
,
"Select State"
);
ddlStates.Enabled =
true
;
}
}
protected
void
drpstate_SelectedIndexChanged(
object
sender, EventArgs e)
{
DropDownList ddlCountries = (DropDownList)sender;
GridViewRow currentRow = (GridViewRow)ddlCountries.NamingContainer;
DropDownList ddlStates = (DropDownList)currentRow.FindControl(
"ddlState"
);
DropDownList ddlCities = (DropDownList)currentRow.FindControl(
"ddlCity"
);
ddlCities.Enabled =
false
;
ddlCities.Items.Clear();
ddlCities.Items.Insert(0,
new
ListItem(
"Select City"
,
"0"
));
int
stateId =
int
.Parse(ddlStates.SelectedItem.Value);
if
(stateId > 0)
{
string
query =
string
.Format(
"select CityId, CityName from Cities where StateId = {0}"
, stateId);
BindDropDownList(ddlCities, query,
"CityName"
,
"CityId"
,
"Select City"
);
ddlCities.Enabled =
true
;
}
}
protected
void
Save(
object
sender, EventArgs e)
{
}
protected
void
Save(
object
sender, EventArgs e)
{
}
}
}
Regards,
Indradeo
Post
Reset
Cancel
Answers (
1
)
Next Recommended Forum
fetch value of two dropdown and then concate the value
what is factory and action page in entity framework ?