Setting the selected index of a combobox with only a value

I need to be able to set the selectedindex of a combobox by only knowing the value that it needs to be selected to.

cb.DropDownStyle = ComboBoxStyle.DropDownList

    If Not dtForm.Rows(R).Item("UserSelection") = "NULL" Then
       cb.SelectedValue = dtForm.Rows(R).Item("UserSelection")
 End If


 dtForm.Rows(R).Item("UserSelection") will give me say the string "TESTING123", "TESTING123" has already been added to that combox but it needs to be selected because  dtForm.Rows(R).Item("UserSelection") returned "TESTING123".


LVL 1
JPERKS1985Asked:
Who is Participating?
 
arif_eqbalConnect With a Mentor Commented:
I think just
    cb.SelectedItem = "TESTING123"
would also work, if the items added to the Combo are just text

0
 
AlexFMCommented:
Dim index as Integer
index = cb.FindStringExact(dtForm.Rows(R).Item("UserSelection"))

if index >= 0 then
  cb.SelectedIndex = index
end if
0
 
lojk.Net and Infrastructure ConsultantCommented:
When a databound combobox is set to dropdownlist i find that simply combobox.text="Description" seems to set the correct item.
0
2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

 
JPERKS1985Author Commented:
none of these suggestions have worked. what a weird problem.
0
 
JPERKS1985Author Commented:
There was code below that code that would reset the values

  If Not dtForm.Rows(R).Item("UserSelection") = "NULL" Then
                  cb.SelectedItem = dtForm.Rows(R).Item("UserSelection")
            End If


Ended up working once I moved the code below the ocnflicting code. Thanks everyone.
0
 
Emanuele_CiriachiCommented:
Oh, and I found out why: I was using reflection, so that when I call

frm.Visible = True

its LoadData method is fired; this method will mess up with the global OleDbDataReader which is my DR, making a mess. Now I fixed everything using a different, custom DataReader in this function, and not the global one. Sorry for wasting your time, thanks anyway.
0
All Courses

From novice to tech pro — start learning today.