Infolink

 

Search This Blog

Oct 27, 2012

Combobox in c# windows application

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
<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";

            }

        }
Build and run the application.


No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...