Home All Groups Group Topic Archive Search About

Populating a combobox from dataset

Author
16 Apr 2006 12:09 PM
c_shah
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?

Author
16 Apr 2006 1:02 PM
+Vice
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.

Show quoteHide quote
"c_shah" <shah.chi***@netzero.net> wrote in message
news:1145189385.511387.73310@g10g2000cwb.googlegroups.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?
>
Author
16 Apr 2006 2:36 PM
c_shah
>>
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.
Author
16 Apr 2006 7:11 PM
+Vice
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
Show quoteHide quote
"c_shah" <shah.chi***@netzero.net> wrote in message
news:1145198166.853110.100960@e56g2000cwe.googlegroups.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.
>
Author
16 Apr 2006 6:32 PM
Cor Ligthert [MVP]
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

Show quoteHide quote
"c_shah" <shah.chi***@netzero.net> schreef in bericht
news:1145189385.511387.73310@g10g2000cwb.googlegroups.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?
>