Cascading ComboBox In Windows Application Using C#

In this article, I will show you how to use cascading combo boxes in windows application using C#.

We take two combo box, bind and cascade it.

Initial chamber

Step 1: Open Visual Studio 2010, Go to File, New, Projects, then under Visual C# select Windows.

window form application

You can change the name of the project and browse your project to different location too. And then press OK.

Step 2: In Solution Explorer you get your Project, Add Service Based Database.  By going to your project right click and Add New Item - Service Based Database.

Service based database

Database chamber

Step 3: Get to your Database [Database.mdf], we will create two table - tbl_country and tbl_state. Go to the database.mdf, Table and Add New table. Design your table like the following:

Tbl_country:

table design

Tbl_state:

table

Design chamber

Step 4: Now open your Form1.cs [Design] file, where we create our design for Cascading ComboBox.

We will drag two ComboBox from the tool box to Form1.cs [Design], you will see your Form look like this.

Form1.cs [Design]:

design

Code chamber

Right click on the blank part of Form1.cs, View Code. You will see you are entered in the code part of the form. Write the following code and then Press F5 to run the project.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9.   
  10. using System.Data.SqlClient;  
  11.   
  12. namespace cascadingdropdownlist  
  13. {  
  14.     public partial class Form1 : Form  
  15.     {  
  16.         SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True");  
  17.         DataRow dr;  
  18.         public Form1()  
  19.         {  
  20.             InitializeComponent();  
  21.             refreshdata();  
  22.         }  
  23.   
  24.         public void refreshdata()  
  25.         {  
  26.             
  27.   
  28.              
  29.             con.Open();  
  30.             SqlCommand cmd = new SqlCommand("select * from tbl_country", con);  
  31.             SqlDataAdapter sda = new SqlDataAdapter(cmd);  
  32.             DataTable dt = new DataTable();  
  33.             sda.Fill(dt);  
  34.             con.Close();  
  35.             dr = dt.NewRow();  
  36.             dr.ItemArray = new object[] { 0, "--Select Country--" };  
  37.             dt.Rows.InsertAt(dr, 0);  
  38.             comboBox1.ValueMember = "countryid";  
  39.             comboBox1.DisplayMember = "countryname";  
  40.             comboBox1.DataSource = dt;      
  41.      
  42.         }  
  43.   
  44.         private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)  
  45.         {  
  46.             if (comboBox1.SelectedValue.ToString()!= null)  
  47.             {  
  48.                 int countryid = Convert.ToInt32(comboBox1.SelectedValue.ToString());  
  49.                 refreshstate(countryid);  
  50.   
  51.                   
  52.             }  
  53.         }  
  54.   
  55.             public void refreshstate(int countryid)  
  56.             {  
  57.               
  58.             con.Open();  
  59.             SqlCommand cmd = new SqlCommand("select * from tbl_state where countryid= @countryid", con);  
  60.             cmd.Parameters.AddWithValue("countryid", countryid);  
  61.             SqlDataAdapter sda = new SqlDataAdapter(cmd);  
  62.             DataTable dt = new DataTable();  
  63.             sda.Fill(dt);  
  64.             con.Close();  
  65.             dr = dt.NewRow();  
  66.             dr.ItemArray = new object[] { 0, "--Select State--" };  
  67.             dt.Rows.InsertAt(dr, 0);  
  68.            
  69.             comboBox2.ValueMember = "stateid";  
  70.             comboBox2.DisplayMember = "statename";  
  71.             comboBox2.DataSource = dt;  
  72.   
  73.   
  74.         }  
  75.     }  
  76.   
  77. }  
Output chamber

Output

We are selecting Country as India from the first Combo box; the state combo box will show only the state relevant to India.

select country

Select state

Hope you liked this. Thank you for reading. Have a good day.