Populating ComboBox

djohnston

Active member
Joined
Jun 1, 2006
Messages
33
Programming Experience
Beginner
OK so basically I have a combobox and a stored procedure that pulls a list of customers in alphabetical order. I need to populate the combobox with those customers. How would I go about doing so. I have been looking up code and keep running into a dead end.
 
You could add all the customers to an array, then do:
VB.NET:
combobox.items.addrange(ArrayName)

Try this as an example (Add one button and one combox to a form:
VB.NET:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Names(5) As String
        Names(0) = "Craig"
        Names(1) = "Adam"
        Names(2) = "John"
        Names(3) = "Yuri"
        Names(4) = "Geoff"
        Names(5) = "Peter"
        Names.Sort(Names)

        ComboBox1.Items.AddRange(Names)


    End Sub
 
How are you retrieveing the data? Are you populating a DataTable using a DataAdapter or are you using a DataReader? If it's a DataReader then something like Craig's code will do the trick. If you're using a DataAdapter then you should simply bind the DataTable to the ComboBox:
VB.NET:
myComboBox.DisplayMember = "the name of the column you want displayed"
myComboBox.DataSource = myDataTable
You can also set the ValueMember if you want to be able to easily get the ID associated with the selected name. You should read about those properties to see how they are used.
 
You don't retrive a DataSet in isolation. Presumably you're using a DataAdapter or TableAdapter to retrieve the data. Apart from that a DataSet contains DataTables that contain the data. The moral of the story is that if you have your data in an object that can be used as a data source, which you do because it's in a DataTable, then you should assign it to the DataSource property of the ComboBox. In fact, if your DataSet was created in the designer you can even set the DataSource of the ComboBox in the designer too, so whatever data you retrieve from the database will be automatically displayed and any changes you make will be automatically reflected.
 
Got Dataset

OK i got the dataset, now when I display the information i only display 1 part of the dataset. Once they select lets say customer name ABC
there is a customer number that is in that dataset as well. I am going to need both of those so it knows what database to put the information in.
lets say abc_123 how do I pull all of the information from the dataset so i can build the string and the connection. I know how to build the string and the connection just not the command to get all of the information from the dataset.
 
Probably the last question.

Ok check this out I have mastercustomerid's 1 2 3 4 5.
Let say 1 is connected to customerid's 1,2, and 2 is connected to 3,4 and 3 is connected to 4,5. When I display it in the combobox it shows all of the instances of the mastercustomerid that connect to customerid's because that is the only field I am showing. I need it to show the mastercustomerid once for all of the other fields. Big help like i said in about 2 more weeks or so Im going to start answering questions on the forum. I have already answered a couple question a few back.
 
Back
Top