Solved

Find ValueMember in ComboBox

Posted on 2003-11-18
24
3,341 Views
Last Modified: 2007-12-19
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
Comment
Question by:idalton
  • 9
  • 9
  • 3
  • +2
24 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 9771883
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
 

Author Comment

by:idalton
ID: 9771898
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 9771909
Okay, I am getting lost in the words.  What do you really mean here?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 9771918
Are you talking about search through the table for a value?
0
 

Author Comment

by:idalton
ID: 9771925
Yes I want to search through the combobox for a certain value that I want to set the "selectedindex" on.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 9771965
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
 
LVL 29

Expert Comment

by:David H.H.Lee
ID: 9772021
idalton ,
Try this, find by value comboBox

DestinationCombo.Items.FindByValue("Hello").Selected = True
0
 

Author Comment

by:idalton
ID: 9772026
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
 

Author Comment

by:idalton
ID: 9772052
FindByValue is not a function of the combobox object.
0
 
LVL 29

Expert Comment

by:David H.H.Lee
ID: 9772186
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
 

Author Comment

by:idalton
ID: 9772206
Here is the error

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

Expert Comment

by:mmarinov
ID: 9772212
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:idalton
ID: 9772229
If you see by my first posting that same code is what started this thread.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 9772247
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
 
LVL 28

Expert Comment

by:mmarinov
ID: 9772249
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
 

Author Comment

by:idalton
ID: 9772278
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 9772293
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
 
LVL 29

Expert Comment

by:David H.H.Lee
ID: 9772317
TheLearnedOne ,
So do i , i thought this is a asp.net problems...hmmmm
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 9772332
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
 

Author Comment

by:idalton
ID: 9772647
I have answered my own question. Thanks for the help. The answer was

 DestinationCombo.SelectedValue = "Hello"

Thanks
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 9773480
Ask for your points to be refunded in Community Support.
0
 

Author Comment

by:idalton
ID: 9773489
thanks for the info
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 10268137
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
 

Accepted Solution

by:
modulo earned 0 total points
ID: 10321361
PAQed, with points refunded (50)

modulo
Community Support Moderator
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now