【问题标题】:Populate ComboBox2 depending on Combobox1 selected items根据 Combobox1 所选项目填充 ComboBox2
【发布时间】:2014-04-25 12:54:48
【问题描述】:

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

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 的所有型号,使用什么?我该怎么做?

【问题讨论】:

  • winforms 还是webforms 应用程序?你能告诉我们你的代码吗?
  • 是WPF应用吗?你用的是什么类型的数据库?你如何访问它?你使用 LINQ to SQL 吗?最重要的是:您尝试过什么?
  • @Sudhakar 它是 Winforms
  • @user3525082:你可以处理SelectedIndexChanged事件,看看我的回答
  • 请参阅stackoverflow.com/questions/13643895/… 以获取有关 Oracle 连接的同一问题的答案。

标签: c#


【解决方案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);
     }
  }

【讨论】:

  • 还有别的办法吗?
【解决方案2】:

您需要处理第一个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
}

【讨论】:

  • 你的意思是设置 (comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);in the (private void comboBox2_SelectedIndexChanged(object sender, EventArgs e) ?
猜你喜欢
  • 1970-01-01
  • 2017-12-01
  • 1970-01-01
  • 2012-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多