Click here to Skip to main content
16,016,249 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
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?
Posted

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];

}
 
Share this answer
 
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[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900