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
using System; using System.Data; using System.Windows.Forms; using System.Configuration; using System.Data.SqlClient; namespace CascadingComboBox { public partial class Form1 : Form { string strConn = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { FillCountry(); } private void FillCountry() { SqlConnection con = new SqlConnection(strConn); SqlCommand cmd = new SqlCommand(); cmd.Connection = con; cmd.CommandType = CommandType.Text; cmd.CommandText = "SELECT CountryID, CountryName FROM Country"; DataSet objDs = new DataSet(); SqlDataAdapter dAdapter = new SqlDataAdapter(); dAdapter.SelectCommand = cmd; con.Open(); dAdapter.Fill(objDs); con.Close(); cmbCountry.ValueMember = "CountryID"; cmbCountry.DisplayMember = "CountryName"; cmbCountry.DataSource = objDs.Tables[0]; } } }
Populate State and City combobox in SelectedIndexChanged event of country,State combobox based on selected IDs
private void cmbCountry_SelectedIndexChanged(object sender, EventArgs e) { if (cmbCountry.SelectedValue.ToString() != "") { int CountryID = Convert.ToInt32(cmbCountry.SelectedValue.ToString()); FillStates(CountryID); cmbCity.SelectedIndex = 0; } } private void FillStates(int countryID) { SqlConnection con = new SqlConnection(strConn); SqlCommand cmd = new SqlCommand(); cmd.Connection = con; cmd.CommandType = CommandType.Text; cmd.CommandText = "SELECT StateID, StateName FROM State WHERE CountryID =@CountryID"; cmd.Parameters.AddWithValue("@CountryID", countryID); DataSet objDs = new DataSet(); SqlDataAdapter dAdapter = new SqlDataAdapter(); dAdapter.SelectCommand = cmd; con.Open(); dAdapter.Fill(objDs); con.Close(); if (objDs.Tables[0].Rows.Count > 0) { cmbState.ValueMember = "StateID"; cmbState.DisplayMember = "StateName"; cmbState.DataSource = objDs.Tables[0]; } } private void cmbState_SelectedIndexChanged(object sender, EventArgs e) { int StateID = Convert.ToInt32(cmbState.SelectedValue.ToString()); FillCities(StateID); } private void FillCities(int stateID) { SqlConnection con = new SqlConnection(strConn); SqlCommand cmd = new SqlCommand(); cmd.Connection = con; cmd.CommandType = CommandType.Text; cmd.CommandText = "SELECT CityID, CityName FROM City WHERE StateID =@StateID"; cmd.Parameters.AddWithValue("@StateID", stateID); DataSet objDs = new DataSet(); SqlDataAdapter dAdapter = new SqlDataAdapter(); dAdapter.SelectCommand = cmd; con.Open(); dAdapter.Fill(objDs); con.Close(); if (objDs.Tables[0].Rows.Count > 0) { cmbCity.DataSource = objDs.Tables[0]; cmbCity.DisplayMember = "CityName"; cmbCity.ValueMember = "CItyID"; } }
VB.NET CODE
Private Sub Form1_Load(sender As Object, e As EventArgs) FillCountry() End Sub Private Sub FillCountry() Dim con As New SqlConnection(strConn) Dim cmd As New SqlCommand() cmd.Connection = con cmd.CommandType = CommandType.Text cmd.CommandText = "SELECT CountryID, CountryName FROM Country" Dim objDs As New DataSet() Dim dAdapter As New SqlDataAdapter() dAdapter.SelectCommand = cmd con.Open() dAdapter.Fill(objDs) con.Close() cmbCountry.ValueMember = "CountryID" cmbCountry.DisplayMember = "CountryName" cmbCountry.DataSource = objDs.Tables(0) End Sub Private Sub cmbCountry_SelectedIndexChanged(sender As Object, e As EventArgs) If cmbCountry.SelectedValue.ToString() <> "" Then Dim CountryID As Integer = Convert.ToInt32(cmbCountry.SelectedValue.ToString()) FillStates(CountryID) cmbCity.SelectedIndex = 0 End If End Sub Private Sub FillStates(countryID As Integer) Dim con As New SqlConnection(strConn) Dim cmd As New SqlCommand() cmd.Connection = con cmd.CommandType = CommandType.Text cmd.CommandText = "SELECT StateID, StateName FROM State WHERE CountryID =@CountryID" cmd.Parameters.AddWithValue("@CountryID", countryID) Dim objDs As New DataSet() Dim dAdapter As New SqlDataAdapter() dAdapter.SelectCommand = cmd con.Open() dAdapter.Fill(objDs) con.Close() If objDs.Tables(0).Rows.Count > 0 Then cmbState.ValueMember = "StateID" cmbState.DisplayMember = "StateName" cmbState.DataSource = objDs.Tables(0) End If End Sub Private Sub cmbState_SelectedIndexChanged(sender As Object, e As EventArgs) Dim StateID As Integer = Convert.ToInt32(cmbState.SelectedValue.ToString()) FillCities(StateID) End Sub Private Sub FillCities(stateID As Integer) Dim con As New SqlConnection(strConn) Dim cmd As New SqlCommand() cmd.Connection = con cmd.CommandType = CommandType.Text cmd.CommandText = "SELECT CityID, CityName FROM City WHERE StateID =@StateID" cmd.Parameters.AddWithValue("@StateID", stateID) Dim objDs As New DataSet() Dim dAdapter As New SqlDataAdapter() dAdapter.SelectCommand = cmd con.Open() dAdapter.Fill(objDs) con.Close() If objDs.Tables(0).Rows.Count > 0 Then cmbCity.DataSource = objDs.Tables(0) cmbCity.DisplayMember = "CityName" cmbCity.ValueMember = "CItyID" End If End Sub
Build and run the application.
If you like this post than join us or share
2 comments:
This comment has been removed by a blog administrator.
strConn error (VB.net)
Post a Comment