1.11M Members

VB.Net ComboBox - How to Retrieve DisplayMember Text?

 
0
 

Note, I screwed up and asked how to get DisplayMember text, I meant to put ValueMember...

I have a combo box I'm populating from a datatable. It has two columns, one for display and one for value. I can find the index of the associated item in the combobox list using the FindString method, but how do I retrieve the value member string from the return of this?

Dim selectedText as String = ""
        Dim indexitem As Integer = ComboBox1.FindString(ComboBox1.Text)
        
        ComboBox1.SelectedItem = indexitem 'If I type the value in the combo box, selected item is nothing
        selectedText = ComboBox1.SelectedValue.ToString 'Throws Null Reference Exception if user typed in value into combobox rather than selecting
        Debug.Print("Found {0} at index {1}, which turns out to be code: {2}", ComboBox.Text, indexitem.ToString, selectedText)
 
0
 

I do apologize... I answered my own question. I noticed that I was trying to set the "SelectedItem". I should've been trying to set the "SelectedIndex".

If you happen to know of a more elegant solution, please feel free to reply.

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: