none
VB.Net 2010 - Retrieve MemberValue from ComboBox RRS feed

  • Question

  • Hi,

    I have been working on this for three days now and just can’t find a simple answer. (It really can’t be that hard to do surly). but now I can’t see the trees for the forest anymore.

    I populate a Combobox from an SQL database, this works fine and the data shows up fine in the Dataset Visualizer.

    ComboList is called cmbStateList
    Two Database fields
    DisplayMember “State”
    ValueMember “ID”

    What I want to do is simply return the ValueMember of when an item is selected from the combobox.

    When I use “cmbStateList.SelectedValue” in the cmbStateList_SelectedIndexChanged what is returned is the index or position of the item in the list, not the ValueMember (“ID”) field of the data.

     

     

    Here is the code I am using, really hope someone can help with this simple problem

     

    Load the State List
    SQL = "Select * from State"

    Command = New SqlCommand(SQL, CNN)

    DA.SelectCommand = Command

    DA.Fill(DS, "State")

     

    Bind the ComboBox
    Me.cmbStateList.Items.Clear()

    Me.cmbStateList.DataSource = DS.Tables("State")

    Me.cmbStateList.DisplayMember = "State"

    Me.cmbStateList.ValueMember = "ID"

     

     

    Select the ComboBox (Have tried DropDownClosed and SelectedIndexChanged)
    Private Sub cmbStateList_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cmbStateList.SelectedIndexChanged

         MsgBox(Me.cmbStateList.SelectedValue)

    End Sub

     

     

    Thanking you in advance

    Chris


    • Edited by Ten0ne Tuesday, December 20, 2011 5:23 AM
    Tuesday, December 20, 2011 5:08 AM

Answers

  •  

    I am really starting to believe VB.net sucks, can’t believe you can spend 3 full days just trying to read the value from a Combobox.

    Why would any company make something so simple to do so hard…

    Worked out if you sort the list (using the control) you get the position of the item in the list, unsorted you get the valuemember.. Really this can’t be True? What am I doing wrong

     

     


    Chris N
    • Marked as answer by Ten0ne Wednesday, December 21, 2011 7:44 AM
    Tuesday, December 20, 2011 6:55 AM

All replies

  • Hi,

    I have been working on this for three days now and just can’t find a simple answer. (It really can’t be that hard to do surly). but now I can’t see the trees for the forest anymore.

    I populate a Combobox from an SQL database, this works fine and the data shows up fine in the Dataset Visualizer.

    ComboList is called cmbStateList
    Two Database fields
    DisplayMember “State”
    ValueMember “ID”

    What I want to do is simply return the ValueMember of when an item is selected from the combobox.

    When I use “cmbStateList.SelectedValue” in the cmbStateList_SelectedIndexChanged what is returned is the index or position of the item in the list, not the ValueMember (“ID”) field of the data.

     

     

    Here is the code I am using, really hope someone can help with this simple problem

     

    Load the State List
    SQL = "Select * from State"

    Command = New SqlCommand(SQL, CNN)

    DA.SelectCommand = Command

    DA.Fill(DS, "State")

     

    Bind the ComboBox
    Me.cmbStateList.Items.Clear()

    Me.cmbStateList.DataSource = DS.Tables("State")

    Me.cmbStateList.DisplayMember = "State"

    Me.cmbStateList.ValueMember = "ID"

     

     

    Select the ComboBox (Have tried DropDownClosed and SelectedIndexChanged)
    Private Sub cmbStateList_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cmbStateList.SelectedIndexChanged

         MsgBox(Me.cmbStateList.SelectedValue)

    End Sub

     

     

    Thanking you in advance

    Chris

    • Edited by Ten0ne Tuesday, December 20, 2011 6:25 AM
    Tuesday, December 20, 2011 6:19 AM
  •  

    I am really starting to believe VB.net sucks, can’t believe you can spend 3 full days just trying to read the value from a Combobox.

    Why would any company make something so simple to do so hard…

    Worked out if you sort the list (using the control) you get the position of the item in the list, unsorted you get the valuemember.. Really this can’t be True? What am I doing wrong

     

     


    Chris N
    • Marked as answer by Ten0ne Wednesday, December 21, 2011 7:44 AM
    Tuesday, December 20, 2011 6:55 AM
  •  

    I am really starting to believe VB.net sucks, can’t believe you can spend 3 full days just trying to read the value from a Combobox.

    Why would any company make something so simple to do so hard…

    Worked out if you sort the list (using the control) you get the position of the item in the list, unsorted you get the valuemember.. Really this can’t be True? What am I doing wrong

     

     


    Chris N


    You must sort the Dataset before binding it to the control.

    How lame is this, you cant set the controls function to sort because then you cant find the valueMember.

    Am I really that far of the mark I am missing somthing?

    Anyway I got it to work after 4 days of searching and trying all kinds of stuff...


    Chris N
    Wednesday, December 21, 2011 7:44 AM