Populating a combobox from dataset



I am a beginner and learning VB.net

There are two wasy to bind a combobox or a listbox
First approach is to assign a datasource and displaymember
Second approach is to iterate through datarow collection

This works
With ComboPaymentType
.DataSource = dsComboItems.Tables("PaymentType")
.DisplayMember = "PaymentType"
.ValueMember = "PaymentTypeID"
End With

The second approach
Dim row As DataRow 'Represents a row of data in Systems.data.datatable
For Each row In dsComboItems.Tables("PaymentType").Rows

How to assign value propery (in my case "paymenttypeId" property as a
value of the combobox item) in the second approach?


The ValueMember is only available to you when you bind. The trick to adding
the extra record in reference to your previous post is to add it to your
datatable instead of your combo box since it's getting it's data from there.
So long as you don't do a save on your datatable then you don't have to
worry about screwing up data in the table you are selecting the data from.


The ValueMember is only available to you when you bind
Does that mean I can only use value member property when i bind my
combobox to the dataset using data source? and I can't use value member
property when I iterate through the datarow collection in order to
populate combobox.

I am SQL DBA and started to learnVB.net so sorry about this newbie
Thank you for elaborating this..actually I can add default item
(--select from combobox--) in the second approach easily by
combobox.items.insert(0,"--select from combobox--") but was not sure
how to get value property.

Cor Ligthert [MVP]


Your first method is binding the properties from the datatable to the
properties from the combobox.

Your second method is filling up a combobox with values, so you can handle
those, for the same sake you just had added some values..

Therefore the first is binding, the second has nothing to do with that.

I hope this helps,



You need to separate your concept of a datatable and the combo box, it
appears to be confusing you. You are correct in that you can add items to
the combo box, but unless you bind it you can't really use the valuemember.
When binding to a table you must have 2 columns, one for Display and another
for Value or if not the DisplayMember will take place of the ValueMember.
In your example, you're not really adding a default item, what you're really
doing is inserting an item at index 0, the very first item. If you only
have a Display member and want to populate the control manually then
reference those items, you can get the value this way:
ComboBox1.Items.Item(ItemNo) or the currently selected item: ComboBox1.Text

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question