|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Populating a combobox from datasetThere 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? 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? > >> The ValueMember is only available to you when you bind>> Does that mean I can only use value member property when i bind mycombobox 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. 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. > 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? >
PrivateFontCollection Quastion
Anyone to help Newbie? Dynamically-created menus and mdi children Connection String error in VB.net Ticker Control Writing a Unicode String into file Proper way to accept data from sockets? VB.net windows app questioins datagridview/delegates MsChart error message in VB 2005 |
|||||||||||||||||||||||