Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3890
  • Last Modified:

Find ValueMember in ComboBox

I am trying to find the value that matches my variable and set the index, but I'm getting errors.

The combobox is bound to a sql table:
 DestinationCombo.DisplayMember = DisplayName
 DestinationCombo.ValueMember = ValueName
 DestinationCombo.DataSource = SourceTable

Then I try to set the index:
For iCtr = 0 To DestinationCombo.Items.Count - 1
     If DestinationCombo.Items(iCtr).value = "Hello" Then
        DestinationCombo.Items(iCtr).Selected = True
         Exit For
     End If
Next

And I get the following error:
Public member 'value' on type 'DataRowView' not found.

How do search for a Value property without getting the error. I don't have this problem searching the DisplayMember.

Thanks
0
idalton
Asked:
idalton
  • 9
  • 9
  • 3
  • +2
1 Solution
 
Bob LearnedCommented:
The ComboBox has two methods to search with:  FindString and FindStringExact.  FindString searches for an item starting with the string, and FindStringExact looks at the whole string.

i = DestinationCombo.FindString("Hello")
0
 
idaltonAuthor Commented:
That is correct and I use that when I am searching the DisplayMember, but that doesn't work when trying to search the ValueMember of the item.

thanks
0
 
Bob LearnedCommented:
Okay, I am getting lost in the words.  What do you really mean here?
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
Bob LearnedCommented:
Are you talking about search through the table for a value?
0
 
idaltonAuthor Commented:
Yes I want to search through the combobox for a certain value that I want to set the "selectedindex" on.
0
 
Bob LearnedCommented:
Let's take this idea out for a spin:

The display member is what the values displayed in the list box.  The value member is what field the selected value is stored in.

Now, with that in mind, why are you saying that you want to search the combo box's value member?  To me you should be using the FindString to find the index, and not a For...Next block.

I may not be on your same wavelength, so forgive my confusion.
0
 
David H.H.LeeCommented:
idalton ,
Try this, find by value comboBox

DestinationCombo.Items.FindByValue("Hello").Selected = True
0
 
idaltonAuthor Commented:
Give an example:

ComboBox has one entry:

DisplayMember = "Texas"
ValueMember = "TX"

Now I am retrieving data from the DB that will set the index on the combobox to whatever is coming back from the DB. The DB returns "TX" and you want to set the SelectedIndex for the combobox to that entry, but to do that you need to be able to find "TX" and the FindString and FindStringExact only will work on "Texas" because it resides in the text portion of the entry not the value portion of the entry.

Hopefully this clears up what I am getting at.

Thanks
0
 
idaltonAuthor Commented:
FindByValue is not a function of the combobox object.
0
 
David H.H.LeeCommented:
idalton ,
Impossible! This method sure be work to get selected value from combobox.
Can you please state out what error you get? It work for me.
0
 
idaltonAuthor Commented:
Here is the error

C:\Net Projects\AUM\FrmMaintenance.vb(837): 'FindByValue' is not a member of 'System.Windows.Forms.ComboBox.ObjectCollection'.
0
 
mmarinovCommented:
here it is

       For iCtr = 0 To DropDownList1.Items.Count - 1
            If DestinationCombo.Items(iCtr).Value = "Hello" Then
                DestinationCombo.SelectedIndex = iCtr
            End If
        Next

B..G
0
 
idaltonAuthor Commented:
If you see by my first posting that same code is what started this thread.
0
 
Bob LearnedCommented:
I am brain dead!  Mixing up my VB6 with my VB.NET.  I get it now!

You want to search for TX, and not Texas.

FindByValue is for an ASP.NET list box.
0
 
mmarinovCommented:
no it's not :-(
you use
DestinationCombo.Items(iCtr).Selected = True

this can select more than one item in the combo

the code i've posted
DestinationCombo.SelectedIndex = iCtr

select only the item you want

just try it

B..G
0
 
idaltonAuthor Commented:
So I tried your code and I recieved the same error as I did. The error occurred on the following line:

If DestinationCombo.Items(iCtr).Value = "Hello" Then

Error:
Public member 'Value' on type 'DataRowView' not found.
0
 
Bob LearnedCommented:
Untested code:

Dim row As DataRow
dim view As DataRowView
dim i As Integer

For Each view in DestinationCombo.Items

   row = CType(view, DataRow)

   If row(ValueName).ToString = "Hello" Then
      DestinationCombo.SelectedIndex = i
   EndIf

   i+=1
 
Next
0
 
David H.H.LeeCommented:
TheLearnedOne ,
So do i , i thought this is a asp.net problems...hmmmm
0
 
Bob LearnedCommented:
It is hard to tell, unless otherwise stated.  This is an open forum, so unless you ask you have to make assumptions.  I didn't finally make the connection until I saw 'System.Windows.Forms.ComboBox.ObjectCollection'.  I made the assumption that this was a Windows Forms problem.
0
 
idaltonAuthor Commented:
I have answered my own question. Thanks for the help. The answer was

 DestinationCombo.SelectedValue = "Hello"

Thanks
0
 
Bob LearnedCommented:
Ask for your points to be refunded in Community Support.
0
 
idaltonAuthor Commented:
thanks for the info
0
 
Bob LearnedCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

PAQ with points refunded

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

TheLearnedOne
EE Cleanup Volunteer
0
 
moduloCommented:
PAQed, with points refunded (50)

modulo
Community Support Moderator
0

Featured Post

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

  • 9
  • 9
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now