This example explains How To Create Cascading ComboBox Dependent On One Another In WinForms Windows Forms Applications Using C# And VB.Net.
I have used Country, State, City tables from database to populate respective cascading combobox based on selection of country and state.
Drag 3 combobox controls from toolbar on the windows form, write following code to populate comboboxes.
Table schemas are shown below.
Write connection string in app.config file
Bind Country ComboBox when Form loads
Populate State and City combobox in SelectedIndexChanged event of country,State combobox based on selected IDs
VB.NET CODE
Build and run the application.
I have used Country, State, City tables from database to populate respective cascading combobox based on selection of country and state.
Drag 3 combobox controls from toolbar on the windows form, write following code to populate comboboxes.
Table schemas are shown below.
Write connection string in app.config file
<configuration>
<connectionStrings>
<add name="connectionString"
connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Cascading.mdf;Integrated Security=True;User Instance=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
Bind Country ComboBox when Form loads
01
using
System;
02
using
System.Data;
03
using
System.Windows.Forms;
04
using
System.Configuration;
05
using
System.Data.SqlClient;
06
07
namespace
CascadingComboBox
08
{
09
public
partial
class
Form1 : Form
10
{
11
string
strConn = ConfigurationManager.ConnectionStrings[
"connectionString"
].ConnectionString;
12
public
Form1()
13
{
14
InitializeComponent();
15
}
16
17
private
void
Form1_Load(
object
sender, EventArgs e)
18
{
19
FillCountry();
20
}
21
private
void
FillCountry()
22
{
23
SqlConnection con =
new
SqlConnection(strConn);
24
SqlCommand cmd =
new
SqlCommand();
25
cmd.Connection = con;
26
cmd.CommandType = CommandType.Text;
27
cmd.CommandText =
"SELECT CountryID, CountryName FROM Country"
;
28
DataSet objDs =
new
DataSet();
29
SqlDataAdapter dAdapter =
new
SqlDataAdapter();
30
dAdapter.SelectCommand = cmd;
31
con.Open();
32
dAdapter.Fill(objDs);
33
con.Close();
34
cmbCountry.ValueMember =
"CountryID"
;
35
cmbCountry.DisplayMember =
"CountryName"
;
36
cmbCountry.DataSource = objDs.Tables[0];
37
}
38
}
39
}
Populate State and City combobox in SelectedIndexChanged event of country,State combobox based on selected IDs
01
private
void
cmbCountry_SelectedIndexChanged(
object
sender, EventArgs e)
02
{
03
if
(cmbCountry.SelectedValue.ToString() !=
""
)
04
{
05
int
CountryID = Convert.ToInt32(cmbCountry.SelectedValue.ToString());
06
FillStates(CountryID);
07
cmbCity.SelectedIndex = 0;
08
}
09
}
10
11
private
void
FillStates(
int
countryID)
12
{
13
SqlConnection con =
new
SqlConnection(strConn);
14
SqlCommand cmd =
new
SqlCommand();
15
cmd.Connection = con;
16
cmd.CommandType = CommandType.Text;
17
cmd.CommandText =
"SELECT StateID, StateName FROM State WHERE CountryID =@CountryID"
;
18
cmd.Parameters.AddWithValue(
"@CountryID"
, countryID);
19
DataSet objDs =
new
DataSet();
20
SqlDataAdapter dAdapter =
new
SqlDataAdapter();
21
dAdapter.SelectCommand = cmd;
22
con.Open();
23
dAdapter.Fill(objDs);
24
con.Close();
25
if
(objDs.Tables[0].Rows.Count > 0)
26
{
27
cmbState.ValueMember =
"StateID"
;
28
cmbState.DisplayMember =
"StateName"
;
29
cmbState.DataSource = objDs.Tables[0];
30
}
31
32
}
33
34
private
void
cmbState_SelectedIndexChanged(
object
sender, EventArgs e)
35
{
36
int
StateID = Convert.ToInt32(cmbState.SelectedValue.ToString());
37
FillCities(StateID);
38
}
39
40
private
void
FillCities(
int
stateID)
41
{
42
SqlConnection con =
new
SqlConnection(strConn);
43
SqlCommand cmd =
new
SqlCommand();
44
cmd.Connection = con;
45
cmd.CommandType = CommandType.Text;
46
cmd.CommandText =
"SELECT CityID, CityName FROM City WHERE StateID =@StateID"
;
47
cmd.Parameters.AddWithValue(
"@StateID"
, stateID);
48
DataSet objDs =
new
DataSet();
49
SqlDataAdapter dAdapter =
new
SqlDataAdapter();
50
dAdapter.SelectCommand = cmd;
51
con.Open();
52
dAdapter.Fill(objDs);
53
con.Close();
54
if
(objDs.Tables[0].Rows.Count > 0)
55
{
56
cmbCity.DataSource = objDs.Tables[0];
57
cmbCity.DisplayMember =
"CityName"
;
58
cmbCity.ValueMember =
"CItyID"
;
59
60
}
61
62
}
VB.NET CODE
01
Private
Sub
Form1_Load(sender
As
Object
, e
As
EventArgs)
02
FillCountry()
03
End
Sub
04
Private
Sub
FillCountry()
05
Dim
con
As
New
SqlConnection(strConn)
06
Dim
cmd
As
New
SqlCommand()
07
cmd.Connection = con
08
cmd.CommandType = CommandType.Text
09
cmd.CommandText =
"SELECT CountryID, CountryName FROM Country"
10
Dim
objDs
As
New
DataSet()
11
Dim
dAdapter
As
New
SqlDataAdapter()
12
dAdapter.SelectCommand = cmd
13
con.Open()
14
dAdapter.Fill(objDs)
15
con.Close()
16
cmbCountry.ValueMember =
"CountryID"
17
cmbCountry.DisplayMember =
"CountryName"
18
cmbCountry.DataSource = objDs.Tables(0)
19
End
Sub
20
21
Private
Sub
cmbCountry_SelectedIndexChanged(sender
As
Object
, e
As
EventArgs)
22
If
cmbCountry.SelectedValue.ToString() <>
""
Then
23
Dim
CountryID
As
Integer
= Convert.ToInt32(cmbCountry.SelectedValue.ToString())
24
FillStates(CountryID)
25
cmbCity.SelectedIndex = 0
26
End
If
27
End
Sub
28
29
Private
Sub
FillStates(countryID
As
Integer
)
30
Dim
con
As
New
SqlConnection(strConn)
31
Dim
cmd
As
New
SqlCommand()
32
cmd.Connection = con
33
cmd.CommandType = CommandType.Text
34
cmd.CommandText =
"SELECT StateID, StateName FROM State WHERE CountryID =@CountryID"
35
cmd.Parameters.AddWithValue(
"@CountryID"
, countryID)
36
Dim
objDs
As
New
DataSet()
37
Dim
dAdapter
As
New
SqlDataAdapter()
38
dAdapter.SelectCommand = cmd
39
con.Open()
40
dAdapter.Fill(objDs)
41
con.Close()
42
If
objDs.Tables(0).Rows.Count > 0
Then
43
cmbState.ValueMember =
"StateID"
44
cmbState.DisplayMember =
"StateName"
45
cmbState.DataSource = objDs.Tables(0)
46
End
If
47
48
End
Sub
49
50
Private
Sub
cmbState_SelectedIndexChanged(sender
As
Object
, e
As
EventArgs)
51
Dim
StateID
As
Integer
= Convert.ToInt32(cmbState.SelectedValue.ToString())
52
FillCities(StateID)
53
End
Sub
54
55
Private
Sub
FillCities(stateID
As
Integer
)
56
Dim
con
As
New
SqlConnection(strConn)
57
Dim
cmd
As
New
SqlCommand()
58
cmd.Connection = con
59
cmd.CommandType = CommandType.Text
60
cmd.CommandText =
"SELECT CityID, CityName FROM City WHERE StateID =@StateID"
61
cmd.Parameters.AddWithValue(
"@StateID"
, stateID)
62
Dim
objDs
As
New
DataSet()
63
Dim
dAdapter
As
New
SqlDataAdapter()
64
dAdapter.SelectCommand = cmd
65
con.Open()
66
dAdapter.Fill(objDs)
67
con.Close()
68
If
objDs.Tables(0).Rows.Count > 0
Then
69
cmbCity.DataSource = objDs.Tables(0)
70
cmbCity.DisplayMember =
"CityName"
71
72
cmbCity.ValueMember =
"CItyID"
73
End
If
74
75
End
Sub
Build and run the application.
0 comments:
Post a Comment