Looping through a combo box to find valuemember

tcl4p

Well-known member
Joined
Feb 29, 2008
Messages
48
Programming Experience
1-3
I have a combo box loaded by a data adapter. What I'd like to do is to loop through the combo box and match the value member of the combo box to a variable which I have already set and when a match is made select that item. I just can't find correct syntax to do this.

Thanks,
Tom
 
Thanks for the reply. Perhaps I should have been more explicit to what I need. In this situation I have already loaded the combo box, but nothing is selected. I have a value in a variable that I want to match to a value in the combo box and when the match is made, select that value. I assumed I would loop through the combo box and within the loop look for the match.
Thanks,
Tom
 
Try
VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim i As Integer = 0
        Dim myItem As String = "tcl4p"
        For i = 0 To ComboBox1.Items.Count - 1
            If ComboBox1.Items.Contains(myItem) Then
                MessageBox.Show("Found It")
                Exit For
            End If
        Next
End Sub
You probably want to do something if you don't find a match as well, but I'll leave that up to you.
 
Actually the hack's suggestion will not work for you. If i understand you well, you want it to matches the Value and not the Display Member. Is that right? If so then you should get the DataSource of the ComboBox and then iterate the DataTable.
VB.NET:
        Dim table As DataTable = DirectCast(Me.ComboBox1.DataSource, DataTable)
        For i As Integer = 0 To table.Rows.Count - 1
            Dim displayItem As String = table.Rows(i)(ComboBox1.DisplayMember).ToString()
            Dim valueItem As String = table.Rows(i)(ComboBox1.ValueMember).ToString()

            If valueItem = "TheValueYouAreSearchingFor" Then
               ' something to do e.g.
               Me.ComboBox1.SelectedIndex = i
            End If
        Next
 
There's no looping needed. What's the ValueMember for? It tells the control what property or column of the SelectedItem to get the SelectedValue from. All you need to do is set the SelectedValue and the corresponding item will be selected.
 
maybe this would help you selecting a value in a combo box

Private Sub cboBranch_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboBranch.SelectedValueChanged
Branch_ID = cboBranch.SelectedValue
GetYearLevel()

End Sub




Private Sub GetYearLevel()

Dim str As String
Dim ds As New DataSet
Dim con As New clsConn
Try

str = "select yl_id, yl_name from file_gradeyearlevel"
ds = con.returnQuery(str)

With cboYearLevel
If ds.Tables(0).Rows.Count > 0 Then
.ValueMember = "yl_id"
.DisplayMember = "yl_name"
.DataSource = ds.Tables(0)
Else
cboYearLevel.Text = ""
End If
End With
Catch ex As Exception

End Try
End Sub
 
I agree with you John but, possibly he is trying to avoid the situations where none of the value members match the textbox entry
If you set SelectedValue to a value that don't exist Combobox will deselect (SelectedIndex=-1 / SelectedValue=Nothing).
 
Ah you mean it will not throw an exception. If you've tested it and it behaves like so then of course it's much better than iterating all items...
Yes, I tested it, didn't you? ;) I usually don't speculate when giving advice, but explain based on what I know and have tested. If there is relevant considerations I'm aware of I usually explain that too.
 
I found this thread as I am searching for the same functionality....perhaps I am missing something, but the proposed solution of setting the valuemember property to the desired value does not work for me. I am also trying to avoid writing code to loop through all of the items in the cbo (or the corresponding datasource/datatable)

Using vbnet2008, developing a windows app

I have a cbo bound to a datatable with an ID and Name fields.
When I bind the cbo to the datatable, I set the ID to the .valuemember and Name to .displaymember. The cbo has an item count of 72 after binding.

When I want to set the cbo to a specific item based on a known value (let's call it gradeID), I've tried this (among other things...)

cbo.valuemember=gradeID

and it throws this error -"Cannot bind to the new value member. Parameter name: value".

I've verified that the gradeID is a valid choice (it's index is 24 in the cbo). As others have gotten this to work, I must be missing something...Thanks for any feedback.
 
shlittle said:
the proposed solution of setting the valuemember property to the desired value
Setting SelectedValue property is the proposed solution.
 
my apologies....I mistyped in my original post ....i am using
cbo.selectedvalue=desired_value, and getting the error message as stated in my original post.
 
my apologies....I mistyped in my original post ....i am using
cbo.selectedvalue=desired_value, and getting the error message as stated in my original post.

That doesn't make sense. The error message you are getting would only result from setting the ValueMember, not the SelectedValue. Perhaps you should copy and paste both your code and the error message rather than typing it by hand so that you, and we, know that you're providing us with accurate information.
 
Back
Top