Solved

Find ValueMember in ComboBox

Posted on 2003-11-18
24
3,562 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
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
 

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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Open dialog with server side controls in it 3 58
SSIS GUID Variable 2 57
T-SQL and CLR parameter strings 9 66
Help with modifying xml file using VB.NET 5 12
It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

752 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