簡體   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