How do you set up a dataset and load it into a combobox?

emaduddeen

Well-known member
Joined
May 5, 2010
Messages
171
Location
Lowell, MA & Occasionally Indonesia
Programming Experience
Beginner
Greeting Everyone,

Can you tell me how to set up a dataset with database data and load the data from the dataset into a combobox?

We wish to show a name in the combobox and when the user selects from the combobox the ID is returned.

Thanks.

Truly,
Emad
 
Assume DS is the DataSet

VB.NET:
Dim vCB as new ComboBox
With vCB
.Location = new Point(xInt, yInt)
.DataSource = DS.Tables(0) 'Assuming only one table in the DataSet
.ValueMember = "YourIDColumn"
.DisplayMember = "YourTextColumn"
.BindingContext = YourForm.BindingContext
End With
YourForm.Controls.Add(vCB)
 
Hi,

Thanks for the help.

The combobox now loads with data.

I do have a question. Right now there are not many rows in the lookup table. When that table grows to 1000 or more will it make any impact on the speed which the combobox gets loaded up?

Truly,
Emad
 
If you are loading that many records you would be better using a DataGridView - that way you can set up a search box and/or restrict the total number of records to a manageable volume - and of course you can sort the columns by default.
 
Depends on what you are using your combobox for. If it's a "read-only" form displaying data, then combobox will be fine, as it will display only the one field (that is if you want to display names for ID fields)

Also, why not use the GUI to create the data controls... that's why Microsoft put it there. It's far easier to use, does the same job but reduces coding errors (typos etc)
 
Depends on what you are using your combobox for. If it's a "read-only" form displaying data, then combobox will be fine, as it will display only the one field (that is if you want to display names for ID fields
)

From a personal point of view, I would not really like to trawl through a thousand entries on a ComboBox - having said that it would be as easy to set up search criteria on either control. It was just a preference!


Also, why not use the GUI to create the data controls... that's why Microsoft put it there. It's far easier to use, does the same job but reduces coding errors (typos etc)

Another preference - but quite often born out of complex design! If the forms and controls are dynamic it's easier to incorporate the variables
 
Hi Everyone,

Thanks for the replies.

I originally set up a lookup button and also had the StudentID validation check the database to see if what the user entered was in there. If it was not then another form with a datagrid was shown but that caused problems. I have another post which included screen shots and a sample application I'm hoping someone can correct for me to I can get the lookup form with the datagrid to write the StudentID back after the user double clicks on it from the datagrid.

The issue I have is that this is an MDI application that allows the user to open another form to browse and insert rows into the database. From that 2nd form the lookup button opens a 3rd form that contains the datagrid. If you open up the sample application from my other post you will see that I can't post a value back to the 2nd open form which called the 3rd form.

Maybe you can look and test the application and let me know what I need to add to it so it will work.

Truly,
Emad
 
When that table grows to 1000 or more will it make any impact on the speed which the combobox gets loaded up?

Would you like to use a combobox that has 1000 items? Now imagine that youre old, and you have trouble using a mouse... ;)
 
Always a good idea to take a step back and think to yourself if you'd like to use the UI you have in mind before getting too involved in the details.

I can't count the number of times where I've been in meetings where something horrendous like this has been suggested. Asking them if they'd like to use it usually changes that in a hurry. For those that still insist on going forward with bad UX prototype something (SketchFlow is what I use) and have them actually try and work with the thing.
 
Greetings Matt,

Thanks for letting me know about SketchFlow. I just went to the Microsoft site and like it so I will try it out.

Also for the Students table we will be using a DataGrid where the user can select from and the ClassID can be from the ComboBox since there are only 10 different classes at the school.

Truly,
Emad
 
You can consider using a TextBox with AutoComplete instead, or look for code samples of combo boxes that have auto complete (the more you type in the text box, the fewer entries you get in the list

You may choose to do this as a popup window, with a datagridview of all the entries and a search box at the top that uses the .Filter property of the DataTable (that has your 1000 entries) .DefaultView

VB.NET:
Sub searchTextBox_TextChanged(..) Handles ..

If searchTextbox.Text.Length < 3 Then Return 'only search if 3 chars have been typed

theDataTable.DefaultView.Filter = "[columnName] LIKE '%" & searchTextbox.Text & "%'"
 
Hi cjard,

Thanks for the helpful reply.

I opted for the pop-up window containing a DataGridView when the student ID from the attendance form is not found and the user double clicks a row in the DataGridView or navigates it until the desired row is found then double clicks it. I will also be providing a button as well to select the chosen row. After the user double clicks the student lookup window closes and the related details from the lookup are populated into the student details of the attendance form.

I'm new to VB 2008 and I'm trying to copy the behavior that my Clarion used to have. There a lot I took for granted that Clarion did and I have to think more of what goes on "Under the hood" of any program I now do in VB 2008.

I also need to find out how to use an auto-complete entry field. Are they good for large tables?

Truly,
Emad


Truly,
Emad
 
Last edited:
Back
Top