469,648 Members | 1,174 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,648 developers. It's quick & easy.

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
ComboPaymentType.Items.Add(row("PaymentType"))
Next

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

Apr 16 '06 #1
4 34829
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.

"c_shah" <sh*********@netzero.net> wrote in message
news:11*********************@g10g2000cwb.googlegro ups.com...
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
ComboPaymentType.Items.Add(row("PaymentType"))
Next

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

Apr 16 '06 #2
>>
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
questions...
in reference to your previous post

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.

Apr 16 '06 #3
C_Sjah,

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,

Cor

"c_shah" <sh*********@netzero.net> schreef in bericht
news:11*********************@g10g2000cwb.googlegro ups.com...
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
ComboPaymentType.Items.Add(row("PaymentType"))
Next

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

Apr 16 '06 #4
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
"c_shah" <sh*********@netzero.net> wrote in message
news:11**********************@e56g2000cwe.googlegr oups.com...
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
questions...
in reference to your previous post

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.

Apr 16 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by amber | last post: by
1 post views Thread by Steve | last post: by
1 post views Thread by Rob via .NET 247 | last post: by
8 posts views Thread by Lumpierbritches | last post: by
6 posts views Thread by buterfly0707 | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.