In this article I will explain with an example, how to implement Cascading (Dependent) ComboBox in Windows Forms (WinForms) application using C# and VB.Net.
The three Cascading ComboBox i.e. for Country, State and City will be populated from SQL Server database in Windows Forms (WinForms) application using C# and VB.Net.
Database
Three tables Countries, States and Cities are created with the following schema.
Countries Table
States Table
Cities Table
Note: You can download the database table SQL by clicking the download link below.
Form Design
The Form consists of three ComboBox controls and a Button. The Country and the State ComboBoxes have been assigned SelectionChangeCommitted event handlers and the Button has been assigned Click event handler.
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
VB.Net
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
Generic function to populate data for the ComboBox
The following function will be used to populate all the three ComboBoxes i.e. for Country, State and City. It accepts SQL query as parameter which is executed and the results are populated into a DataTable.
Once the DataTable is populated from database, a new row is inserted in First position which will act as the Default (Blank) item for the ComboBox.
C#
private DataTable GetData(string sql)
{
string constr = @"Data Source=.\SQL2014;Initial Catalog=Cascading_ddl;Integrated Security=true";
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlDataAdapter sda = new SqlDataAdapter(sql, con))
{
DataTable dt = new DataTable();
sda.Fill(dt);
DataRow row = dt.NewRow();
row[0] = 0;
row[1] = "Please select";
dt.Rows.InsertAt(row, 0);
return dt;
}
}
}
VB.Net
Private Function GetData(ByVal sql As String) As DataTable
Dim constr As String = "Data Source=.\SQL2014;Initial Catalog=Cascading_ddl;Integrated Security=true"
Using con As SqlConnection = New SqlConnection(constr)
Using sda As SqlDataAdapter = New SqlDataAdapter(sql, con)
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
Dim row As DataRow = dt.NewRow()
row(0) = 0
row(1) = "Please select"
dt.Rows.InsertAt(row, 0)
Return dt
End Using
End Using
End Function
Populating the Country ComboBox
Inside the Form Load event handler, the Country ComboBox is populated from the Countries table and the State and City ComboBox are disabled.
C#
private void Form1_Load(object sender, EventArgs e)
{
cbCountries.DataSource = this.GetData("SELECT CountryId, CountryName FROM Countries");
cbCountries.DisplayMember = "CountryName";
cbCountries.ValueMember = "CountryId";
cbStates.Enabled = false;
cbCities.Enabled = false;
}
VB.Net
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
cbCountries.DataSource = Me.GetData("SELECT CountryId, CountryName FROM Countries")
cbCountries.DisplayMember = "CountryName"
cbCountries.ValueMember = "CountryId"
cbStates.Enabled = False
cbCities.Enabled = False
End Sub
Populating the State ComboBox
Inside the SelectionChangeCommitted event handler of the Country ComboBox, first the State and City ComboBoxes are cleared by assigning them NULL DataSource.
Then if the Country ComboBox has a valid selection then the State ComboBox is populated from the States table.
C#
private void cbCountries_SelectionChangeCommitted(object sender, EventArgs e)
{
cbStates.DataSource = null;
cbCities.DataSource = null;
cbStates.Enabled = false;
cbCities.Enabled = false;
if (cbCountries.SelectedValue.ToString() != "0")
{
string sql = string.Format("SELECT StateId, StateName FROM States WHERE CountryId = {0}", cbCountries.SelectedValue);
cbStates.DataSource = this.GetData(sql);
cbStates.DisplayMember = "StateName";
cbStates.ValueMember = "StateId";
cbStates.Enabled = true;
}
}
VB.Net
Private Sub cbCountries_SelectionChangeCommitted(ByVal sender As Object, ByVal e As EventArgs) Handles cbCountries.SelectionChangeCommitted
cbStates.DataSource = Nothing
cbCities.DataSource = Nothing
cbStates.Enabled = False
cbCities.Enabled = False
If cbCountries.SelectedValue.ToString() <> "0" Then
Dim sql As String = String.Format("SELECT StateId, StateName FROM States WHERE CountryId = {0}", cbCountries.SelectedValue)
cbStates.DataSource = Me.GetData(sql)
cbStates.DisplayMember = "StateName"
cbStates.ValueMember = "StateId"
cbStates.Enabled = True
End If
End Sub
Populating the City ComboBox
Inside the SelectionChangeCommitted event handler of the State ComboBox, first City ComboBox is cleared by assigning them NULL DataSource.
Then if the State ComboBox has a valid selection then the City ComboBox is populated from the Cities table.
C#
private void cbStates_SelectionChangeCommitted(object sender, EventArgs e)
{
cbCities.DataSource = null;
cbCities.Enabled = false;
if (cbStates.SelectedValue.ToString() != "0")
{
string sql = string.Format("SELECT CityId, CityName FROM Cities WHERE StateId = {0}", cbStates.SelectedValue);
cbCities.DataSource = this.GetData(sql);
cbCities.DisplayMember = "CityName";
cbCities.ValueMember = "CityId";
cbCities.Enabled = true;
}
}
VB.Net
Private Sub cbStates_SelectionChangeCommitted(ByVal sender As Object, ByVal e As EventArgs) Handles cbStates.SelectionChangeCommitted
cbCities.DataSource = Nothing
cbCities.Enabled = False
If cbStates.SelectedValue.ToString() <> "0" Then
Dim sql As String = String.Format("SELECT CityId, CityName FROM Cities WHERE StateId = {0}", cbStates.SelectedValue)
cbCities.DataSource = Me.GetData(sql)
cbCities.DisplayMember = "CityName"
cbCities.ValueMember = "CityId"
cbCities.Enabled = True
End If
End Sub
Fetching the selected value of Country, State and City ComboBoxes
When the Button is clicked, the Selected Text values of Country, State and City ComboBoxes are fetched and are displayed using MessageBox.
C#
private void btnSubmit_Click(object sender, EventArgs e)
{
string message = "Country: " + cbCountries.SelectedText;
message += Environment.NewLine;
message += "State: " + cbStates.SelectedText;
message += Environment.NewLine;
message += "City: " + cbCities.SelectedText;
MessageBox.Show(message);
}
VB.Net
Private Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSubmit.Click
Dim message As String = "Country: " & cbCountries.SelectedText
message &= Environment.NewLine
message &= "State: " & cbStates.SelectedText
message &= Environment.NewLine
message &= "City: " & cbCities.SelectedText
MessageBox.Show(message)
End Sub
Screenshot
Downloads