Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
how do I link two Comboboxes together in C# that the two Comboboxes binding to a dataset.
Posted
Updated 27-Feb-11 11:47am
v2
Comments
Henry Minute 27-Feb-11 17:51pm    
Without knowing more about what you are trying to do and the structure of your database, it is almost impossible to make any suggestions.
Dave Kreskowiak 27-Feb-11 20:47pm    
Define, in functional terms, what you mean by "link"... What do you want to happen? There is no such thing as "linking" control together.
Sunasara Imdadhusen 28-Feb-11 0:30am    
Not clear!!
saeeddoe 28-Feb-11 1:36am    
I have two table as Requesters and Location. Requesters are some departments like 'Maintenance','HSE','Production'and etc. and each Requester contain some Location. this two tables have relationship one-to-many. it means that for example Maintenance Department have some Location such as 'Maintenance Manager Office','Palanning Office'and etc. and Production Department contain some Location like 'Control Room','Labratory' and etc. so I want first binding this two table to 2 diferent comboboxes and second when I choose one Requester from Requester Combobox, only the Location related to that Requester should be shown in Location Combobox when the form load. I mean this.
Sunasara Imdadhusen 28-Feb-11 2:12am    
Now i understand.. i have added answer.

Hi
First of all you have to fill Requesters drop down your code is like


C#
Page_Load()
{
SQL = "SELECT DISTINCT DepartmentID,Department FROM Requesters"
SQLDataAdapter da = new SQLDataAdapter(SQL, connection);
DataSet ds = new DataSet();
da.Fill(ds);
drpRequesters.DataTextField = "Department";
drpRequesters.DataValueField = "DepartmentID";
drpRequesters.DataSource = ds.Tables[0];
drpRequesters.DataBind();
FillLocation();
}



Now you have to fill Location drop down based on Department selection

C#
private void FillLocation()
{
  if(drpRequesters.SelectedIndex >= 0)
  {
drpLocation.Items.Clear();
    SQL = "SELECT DISTINCT LocationID,Location FROM Locations WHERE DepartmentID=" + drpRequesters.SelectedItem.Value;
SQLDataAdapter da = new SQLDataAdapter(SQL, connection);
DataSet ds = new DataSet();
da.Fill(ds);
drpLocation.DataTextField = "Location";
drpLocation.DataValueField = "LocationID";
drpLocation.DataSource = ds.Tables[0];
drpLocation.DataBind();
  }
}


Final step is you have create change event of Dipartment like
C#
drpDepartment_SelectedIndexChanged()
{
 FillLocation();
}


You should set
XML
AutoPostback="True" 
for drpDepartment from .aspx page

Thanks,
Imdadhusen
 
Share this answer
 
Comments
Dalek Dave 28-Feb-11 3:32am    
Have a 5!
Manfred Rudolf Bihy 28-Feb-11 3:40am    
You haven't voted yet as far as a I can tell! So I'll just offer my 5.
Dalek Dave 28-Feb-11 4:25am    
Oops, but corrected! :)
Sunasara Imdadhusen 28-Feb-11 6:05am    
Thanks for your vote!
saeeddoe 28-Feb-11 11:49am    
my friend thank you very much for your response.
I suggest follow these steps

1. Get data from the Requesters table into a dataset
2. Get data from the Location table into the same dataset
3. Create a data relation relating the records in the above tables
4. Create 2 binding source for Requester and Location table and set the datasource of Requester to the dataset and Location to Requester datasource, and datamember of Location to the datarelation created in the step 3.
5. Set the datasource to respective comboboxes.

You can see a detailed example here.

- Vivek
 
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