繁体   English   中英

根据 Combobox1 所选项目填充 ComboBox2

[英]Populate ComboBox2 depending on Combobox1 selected items

我是一名学生,也是编程的新手,我有两个组合框,combobox1 和 combobox2 组合框包含移动公司,如诺基亚、三星、HTC 和组合框 2 包含移动模型,如三星、s3 等,我想对这两个组合框进行排序,我的意思是当我在 combobox1 中单击 nokia 时,所有 nokia 型号都应该在 combobo2 列表中可见,所以我决定使用外键关系

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

我希望如果我在第一个组合框中选择诺基亚,那么第二个组合框将选择 IDr = 1 的所有型号要使用什么? 我怎样才能做到这一点?

在第二个组合框中,您需要多个项目数组。 因此,当在第一个组合框中选择一个项目时,第二个组合框项目是数组中的项目。

首先定义2个数组。

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

然后在第一个组合框的 SelectedIndexChanged 事件中放置一个 if 语句。 这样,如果第一个更改,您可以更改第二个。

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

您需要处理第一个ComboBox控件的SelectedIndexChanged事件以将项目填充/添加到第二个ComboBox

尝试这个:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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