Combobox relies on previous combobox

frankm9639

Active member
Joined
Aug 24, 2006
Messages
25
Programming Experience
1-3
I have an app where I want to have the user select a choice from a combobox. Such as "CompanyA". Then, some textboxes are filled in with company information from the company table. I have one dataset and multiple data adapters that populate the dataset tables. Here's the part I think I'm messing up.

When the user selects, say, "CompanyA", I want the choices for the next combobox to only show "items" for that company from the item table. But, if I try to use selectedindex on the first combobox and try to check the combobox.text, it is always blank. Any ideas? I can post some code, but if you tell me in general the process, I can try that first. By process, I mean, "first you bind this, then set that, then do this". Then I can see if I'm doing it correctly. I'm pretty new, so I don't want to give you some cheezy newbie code. Thanks!
 
Hard work, this data access, isnt it?


You can use the dataset designer to build 3 relates tables, fill them all with relevant data and let the relationships manage the combos. I would post a project but your VS2003 wouldnt be able to open my VS2005 project.. you can download the free version of VB2005 if you like.

In 2.0 (2005) you would/could do:

Make a dataset with 3 tables:

States
Towns
Companies

have one database table that has data like:

State, Town, Company
S1, T1, C1
S1, T1, C2
S1, T2, C1
S1, T2, C2
S2, T1, C1
S2, T1, C2
S2, T2, C1
S2, T2, C2


Fill your States datatable with:
SELECT DISTINCT state FROM table

Fill your Towns datatable with
SELECT DISTINCT state, town FROM table

Fill your Companies with:
SELECT DISTINCT town, company FROM table


Establish relationships between state-state and town-town


From the data sources window, expand States, States_Towns, and Towns_Companies.

Drop a combo on from each. Binding sources are created to manage the relationships (1.1 doesnt have binding sources, you would have to manage the relationships yourself in code, with GetChildRows, I suspect..)

Change the DropDownStyle to DropDownList for each. Remove the binding on .Text = (whatever it is bound to)

Make the States combo have a DataSource = StatesBindingSource, Towns combo DataSource = Towns BindingSource and Companies combo DataSource = Companies BindingSource.
Set the relevant display members on all combos.

Run the project.


This takes about 10 minutes, including creating the database table (the longest op for me because I dont have access so i must create it manually with SQL statements in VS2005) and not a line of code is written by me, other than the SQLs..

Here are some pictures, and an example project. You wont be able to open the project in 2003, but you can run the exe:
 

Attachments

  • Image1.png
    Image1.png
    113.6 KB · Views: 26
  • Image2.png
    Image2.png
    13.4 KB · Views: 24
  • CascadingComboExample.zip
    95.6 KB · Views: 26
Back
Top