VB - Win App: Populating one dropdown after another is filled in

clarkiedave

New member
Joined
Sep 20, 2007
Messages
2
Location
Manchester
Programming Experience
1-3
Hi,

Im "VERY" new to VB.NET, I have 2 combo boxes (ddlCustomer, ddlSite).

What i want to happen is on load the ddlCustomer box to be populated with the names of the customers (customername) and on change of this i want the ddlSite box to be populated with a list of sites corresponding to the customer selected. There is a sql backend to the system, the two table im using are tblCustomer and tblSites and they are joined on a field (custid) which is in both tables.

All help gratefully recieved :eek:)
 
You will require a "FillBy" query on your sites tableAdapter which uses @CustID. - your SQL would be something like SELECT * FROM sites WHERE CustID = @CustID

You then set it so that "on dropdown" of the 2nd combobox, you call this query.

I.E -

Form Load event - Fill all Customers to the customer dataTable.
cboCustomer is bound to this dataTable.

Select a Customer from the list.

cboSite DropDown event - Fill the Sites dataTable using your FillByCustID query, and the line of code will be something like;
me.sitesTableAdapter.FillByCustID(me.dataSet.Sites, cboCustomer.SelectedValue)

^ this will then load only the sites for the selected customer.

you could probably fiddle the code so that you could call the Site fill when the SelectedValue of the first combobox changes - there is an event for a combobox called SelectedValueChanged but I've never really used it that way.

Another way would be to use a button, kind of like a middle man - (a) Select a customer (b) Press the button to load sites for that customer
...you could also throw in some validation, something like;



VB.NET:
Private Sub btnLoadSites_Click(..................) handles me.btnLoadSites.Click

If me.cboCustomers.text = nothing Then
   messageBox.show("Please select a customer from the list first!")
Else
   me.sitesTableAdapter.FillByCustID(me.MyDataSetName.Sites, cboCustomer.SelectedValue)
End If

End Sub

have a play around, its the best way to learn :D
 
Back
Top