Wednesday, 10 August 2016

Cascading Combobox in Windows Application using C#


In this tutorial I’ll show you how to use cascading combo boxes in windows application using C#, where we take two combo box and bind it and cascade it.




INITIAL CHAMBER:

Step1) Open Your Visual Studio 2010, Go to File - -> New - -> Projects - -> Under Visual C#  - -> Windows.

C:\Users\Nilesh\Desktop\datagridview.jpg

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


Step2) 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.


C:\Users\Nilesh\Desktop\service based database.jpg




DATABASE CHAMBER:

Step3)  Get to your Database [Database.mdf], we will create two table - -> tbl_country and tbl_state. Go to the database.mdf - -> Table - -> Add New table, design your table like this:


Tbl_country:


C:\Users\nilu\Desktop\tbl_country.jpg


Tbl_state:

C:\Users\nilu\Desktop\tbl_state.jpg




DESIGN CHAMBER:

Step5)  Now open your Form1.cs [Design] file, where we create our design for Cascading ComboBox.
We will drag two ComboBox from tool box to Form1.cs [Design], you will see your Form look like this.

Form1.cs [Design]:
C:\Users\nilu\Desktop\cascading combobox design.jpg





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 Below 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:


C:\Users\nilu\Desktop\Output of combobox.jpg

We are selecting Country as India from the first Combo box, the state combo box will show only the state relevant to India.
C:\Users\nilu\Desktop\Output of combobox1.jpg

C:\Users\nilu\Desktop\Output of combobox2.jpg


Hope you like this. Thank you for Reading. Have a good day.