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.
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.
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:
Tbl_state:
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]:
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.
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
-
- using System.Data.SqlClient;
-
- namespace cascadingdropdownlist
- {
- public partial class Form1 : Form
- {
- SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True");
- DataRow dr;
- public Form1()
- {
- InitializeComponent();
- refreshdata();
- }
-
- public void refreshdata()
- {
-
-
-
- con.Open();
- SqlCommand cmd = new SqlCommand("select * from tbl_country", con);
- SqlDataAdapter sda = new SqlDataAdapter(cmd);
- DataTable dt = new DataTable();
- sda.Fill(dt);
- con.Close();
- dr = dt.NewRow();
- dr.ItemArray = new object[] { 0, "--Select Country--" };
- dt.Rows.InsertAt(dr, 0);
- comboBox1.ValueMember = "countryid";
- comboBox1.DisplayMember = "countryname";
- comboBox1.DataSource = dt;
-
- }
-
- private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
- {
- if (comboBox1.SelectedValue.ToString()!= null)
- {
- int countryid = Convert.ToInt32(comboBox1.SelectedValue.ToString());
- refreshstate(countryid);
-
-
- }
- }
-
- public void refreshstate(int countryid)
- {
-
- con.Open();
- SqlCommand cmd = new SqlCommand("select * from tbl_state where countryid= @countryid", con);
- cmd.Parameters.AddWithValue("countryid", countryid);
- SqlDataAdapter sda = new SqlDataAdapter(cmd);
- DataTable dt = new DataTable();
- sda.Fill(dt);
- con.Close();
- dr = dt.NewRow();
- dr.ItemArray = new object[] { 0, "--Select State--" };
- dt.Rows.InsertAt(dr, 0);
-
- comboBox2.ValueMember = "stateid";
- comboBox2.DisplayMember = "statename";
- comboBox2.DataSource = dt;
-
-
- }
- }
-
- }
Output chamber We are selecting Country as India from the first Combo box; the state combo box will show only the state relevant to India.
Hope you liked this. Thank you for reading. Have a good day.