根据Combobox1选择的项目填充ComboBox2 [英] Populate ComboBox2 depending on Combobox1 selected item
问题描述
我是一名学生,也是编程的新手,我有两个组合框,combobox1和combobox2 combobox1包含移动公司的像诺基亚,三星,HTC和combobox2包含三星,s3等移动模型,我想排序两个combobox我的意思是当我点击combobox1中的nokia然后所有的nokia模型应该在combobo2列表中可见,所以我决定使用外键关系
Registry_name -table
- IDr(主键)
- 名称
Material -table
- ID(主键)
- IDr(制造商的外键)
- 名称
数据示例:
Registry_name表
IDr名称
-------------- ----------
1诺基亚
2三星
3 HTC
物料表
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欲望X
7 3 Windows Phone 8X
8 3 One S
如果我选择我想要诺基亚在第一个组合框中然后第二个组合框将选择IDr = 1的所有模型使用什么?我怎么能这样做?
我没有做过一切只是给出一些代码
First bind页面加载事件中的Combobox1
private void Page_Load(object sender,EventArgs e)
{
//建立数据库连接并收集数据作为选择查询
combobox1.DataSource = Registry_name表数据;
combobox1.DataTextField =name;
combobox1.DataValueField =IDr;
}
然后,combobox1 selectionchange事件根据此选定值对过滤数据进行查询
private void combobox1_SelectionChanged(object sender,EventArgs e)
{
int id = Convert.ToInt32(combobox1.SelectedItem.Value);
//使你的sql连接
//并且查询应该像
string query =select * from material_table其中IDr =+ id +;
SqlDataAdapter da = new SqlDataAdapter(query,con);
DataSet ds = new DataSet();
da.Fill(ds);
if(ds.Table.Count> 0)
combobox2.DataSource = ds.Table [0];
}
您可以使用ComboBox.SelectedIndexChanged事件。
查看此链接以获取更多详细信息。它可能会帮助你理解。
http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selectedindexchanged(v = vs.110).aspx [ ^
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?
I have not done everything just given some code
First bind Combobox1 in pageload event
private void Page_Load(object sender, EventArgs e) { //Make your database connection and collect data as select query combobox1.DataSource=Registry_name table data; combobox1.DataTextField="name"; combobox1.DataValueField="IDr"; }
Then combobox1 selectionchange event make a query with filter data as per selected value of this
private void combobox1_SelectionChanged(object sender, EventArgs e) { int id=Convert.ToInt32(combobox1.SelectedItem.Value); //make your sql connection //And query should be like string query=" select * from material_table where IDr="+id+""; SqlDataAdapter da=new SqlDataAdapter(query,con); DataSet ds=new DataSet(); da.Fill(ds); if(ds.Table.Count >0) combobox2.DataSource=ds.Table[0];
}
You can use ComboBox.SelectedIndexChanged Event.
look at this link for further Detail. It might help you understand.
http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selectedindexchanged(v=vs.110).aspx[^]
这篇关于根据Combobox1选择的项目填充ComboBox2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!