skip to Main Content

Conditional Dropdowns

if you select T1, T2 should only reflect values based on T1 and if you selected up to T2

then T3 should reflect only values based on T2

Hope someone can help I want to create a dropdown winforms c# 3 dropdowns to be exact

 __________
|__________|<-- selection 1 - T1
 __________
|__________|<-- selection 2 - T2
 __________
|__________|<-- selection 3 - T3

my data will look like this

This data will come from a data source
and will look like below

T1 - Application
T2 - Adobe
T3 - Flash
----------------
T1 - Application
T2 - Adobe
T3 - Dreamweaver
---------------
T1 - Application
T2 - Adobe
T3 - Photoshop
---------------
T1 - Application
T2 - Microsoft
T3 - Word
---------------
T1 - Application
T2 - Microsoft
T3 - Excel

So with the above data fi you select Application on T1 the T2 should be either Adobe or Microsoft and if you select one of them it should only show the T3 relevant to T2

Any help would be appreciated.

4

Answers


  1. You only bind the first combobox during design time, to the T1 values.

    Then on a selectedindexchanged event of that combobox, you retrieve the selected item and and bind the second combobox with a filtered list based on that value.

    You do the same with the selectedindexchanged of the second combobox and the binding of the 3rd combobox.

    Login or Signup to reply.
  2. set

    Autopostback = true

    for first two drop downs. And handle their SelectedIndexChanged event. On page load event, bind the first drop down in following way.

    
        dd1.Items.Add(new ListItem("Application", "0"));
        dd1.Items.Add(new ListItem("abc", "1"));
    
    

    Then on dd1_SelectedIndexChanged event use following code

    
        protected void dd1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if ddl.selectedindex = 0 then
            {
                 dd2.Items.Add(new ListItem("Adobe", "0"));
                 dd2.Items.Add(new ListItem("Microsoft", "1"));
            }  
        }
    
    

    and so on so forth..

    Login or Signup to reply.
  3. Load data only in the first combo box.

    • On SelectedIndexChanged event handler for that comboBox, load data in second combo box; clear and disable 3rd, 4th…. combo boxes.
    • Likewise, on SelectedIndexChanged event handler for 2nd comboBox, load data in 3rd combo box; clear and disable 4th, 5th… combo boxes
    • and so on….

    Edit: Sample code added

    The sample code provided below is only for reference and by no means as per any guidelines

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.Items.Add("Application");
            comboBox1.Items.Add("Adobe");
            comboBox1.Items.Add("Flash");
        }
    
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBox2.Items.Clear();
            comboBox3.Items.Clear();
            comboBox4.Items.Clear();
            comboBox5.Items.Clear();
    
            comboBox3.Enabled = comboBox4.Enabled = comboBox5.Enabled = false;
    
            if (comboBox1.Text != "Application")
                comboBox2.Items.Add("Application");
            if (comboBox1.Text != "Adobe")
                comboBox2.Items.Add("Adobe");
                comboBox2.Items.Add("Dreamweaver");
        }
    
        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBox3.Items.Clear();
            comboBox4.Items.Clear();
            comboBox5.Items.Clear();
    
            comboBox4.Enabled = comboBox5.Enabled = false;
            comboBox3.Enabled = true;
    
            if (comboBox2.Text != "Application")
                comboBox3.Items.Add("Application");
            if (comboBox2.Text != "Adobe")
                comboBox3.Items.Add("Adobe");
            comboBox2.Items.Add("Photoshop");
        }
    
    Login or Signup to reply.
  4. It is all depends on what this win forms will be used for. But in any case data binding will be on load to only T1 drop down.

    In web you would use OnChange event and call JavaScript function to either use AJAX/jQuery to populate T2 box or just submit form to repopulate T2 with new values (bit harder and slower but will give you same result)
    Same goes for T3 box which will be populated when T2 is changed.I would not use Postback because form will be submitted on every change and then you may lose data on other fields or you will have to verify that data repopulate all other fields properly.

    On windows app you will have to use drop down events to do basically the same but in C# coding.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search