简体   繁体   中英

Populate ComboBox2 depending on Combobox1 selected items

i am a student, and a newbie to programming, i have two comboboxes, combobox1 and combobox2 combobox1 contains mobile company's like nokia,samsung,htc and combobox2 contains mobile models like samsung,s3 and etc, i want to sort the two combobox i mean when i click the nokia in the combobox1 then all the model of nokia should be visible in the list of combobo2, so i have decided to used foreign key relationship

Registry_name -table
- IDr (primary key)
- name



Material -table
- ID (primary key)
- IDr (foreign key to manufacturer)
- name

Example for the data:

Registry_name table

IDr name
-------------- ----------
1 Nokia
2 Samsung
3 HTC


Material table

id idr name
------- -------------- ----------
1 1 C7
2 1 Lumia 900
3 1 Lumia 920
4 2 Galaxy S II
5 2 Galaxy S III
6 3 Desire X
7 3 Windows Phone 8X
8 3 One S

I want that if i select nokia in the first combobox then the second combobox will select all the models which are IDr = 1 what to use? how can i do that?

You would need multiple arrays of items for in the second combobox. So when a item get selected in the first combobox the second combobox items are the one from the array.

Define 2 arrays at first.

  string[] nokias = { "2314", "s3522" }; // and so on
  string[] samsung = { "s3",  "s4" };

Then after that you put a if statement in the SelectedIndexChanged Event for the first combobox. That way you can change the second one if the first one changes.

  if(combobox1.selecteditem.text == "nokia"){
     foreach(string str in nokias){
         combobox2.items.add(str);
     }
  }

You need handle the SelectedIndexChanged event of the first ComboBox control to fill/add the items into second ComboBox

Try This:

comboBox1.SelectedIndexChanged += new          
                        System.EventHandler(this.comboBox1_SelectedIndexChanged);
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    //code for filling the combobox 2
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM