Solved

Retrieve Index of Matching value in Combobox

Posted on 2009-03-31
11
709 Views
Last Modified: 2012-06-27
Hi EE,

I am wanting a procedure that will look through items in a combo box to see which one matches the id value I need and then set the index of the item as the selected item in the combo.
I created something similar to what I used to use in VB 6, but it doesn't work.
What am I doing wrong?
I've also included the code I use to fill the combobox, just in case it's needed.
Thanks
Public Function ListFind(ByVal cbo As ComboBox, ByVal iValue As Integer) As Integer
        Dim i As Integer
 
        For i = 0 To cbo.Items.Count - 1
            If cbo.Items(i) = iValue Then
                cbo.SelectedIndex = i
                Exit For
            End If
 
        Next
 
    End Function
 
 
Public Sub FillComboBox(ByVal cnt As ComboBox)
        'example fill states
        Dim ds As New DataSet
 
        If DatabaseToUse() = BizObjs.DBType.Access Then
            'Access Database
            Dim cFields As New cFieldTableNames
            cFields.GetStrTableName = msTblName
            cFields.ComboTable = msTblName
            SelectStr = cFields.ReturnComboString()
            Dim dc As New OleDb.OleDbConnection(ConnectionString)
            Dim da As New OleDb.OleDbDataAdapter(mSelectStr, dc)
 
            Try
                da.Fill(ds)
                dc.Close()
 
                cnt.DataSource = ds.Tables(0)
                cnt.DisplayMember = cFields.SendToCorrectFieldProcedure(3)
                cnt.ValueMember = cFields.SendToCorrectFieldProcedure(1)
 
            Catch ex As Exception
 
                MessageBox.Show(ex.Message)
                Throw ex
 
            Finally
                dc.Close()
                da.Dispose()
            End Try
        End If
 
    End Sub

Open in new window

0
Comment
Question by:Sheritlw
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 13

Expert Comment

by:kaylanreilor
ID: 24036325
So the data into your combo are integers ? (If cbo.Items(i) = iValue Then)
If yes I suppose that cFields.SendToCorrectFieldProcedure(1) returns an integer, doesn't it ?
So after all those statement, could you explain what doesn't work exactly ? For example, into the ListFind function, do you have all the data into cbo when you set a breakpoint there ?
0
 
LVL 13

Expert Comment

by:kaylanreilor
ID: 24036333
Concerning what is inside cbo (the type of the data) check also that, even if they are supposed to be integers, they are not actually strings !
Thus you could try: If cbo.Items(i) = iValue.ToString() Then...
0
 

Author Comment

by:Sheritlw
ID: 24036339
The cFields.SendtoCorrectFieldProcedure brings back the primary field name, ie. StateID.
The error is Operator " =" is not defined for type datarowview and type integer.
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

Author Comment

by:Sheritlw
ID: 24036425
Unfortunetely, That doesn't work.
Doesn't anyone know how to loop through a combobox values and get the primary id so that you can match it up with a value.
For example, I have a company table that has a related field StateID.  When I fill the company form, i also get the stateid for the company.  I then fill my combos.  Now I need to find out what item in the list has the same stateID so I can make it the selected item.

In VB 6, it was so EASY...
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 24036607
why re-invent the wheel for finding a specific item in a combobox?  The control already has the functionality built in....

// This will find the first item with "TEST" as it's value.
Dim index As Integer = comboBox1.FindStringExact("TEST")
// this will select the first item that has "TEST" as it's value.
comboBox1.SelectedIndex = index

Source: http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.findstringexact.aspx
0
 

Author Comment

by:Sheritlw
ID: 24036631
The problem is, I need to find the value of the Valuemember that holds the Integer StateID.
I don't have the text value of the state (the state name)
I also tried using the find string but it didn't work.
Please advise.
Thanks
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 24036773
Dim dt As DataTable = DirectCast(comboBox1.DataSource, DataTable)
Dim yourState as string
For i As Integer = 0 To dt.Rows.Count - 1
    Dim displayText As String = dt.Rows(i)(comboBox1.DisplayMember).ToString()
    Dim valueItem As String = dt.Rows(i)(comboBox1.ValueMember).ToString()
    ' Process the object depending on the type
    if valueItem = "TheValueYouAreSearchingFor" then
       yourState = displayText
   end if
Next
0
 
LVL 53

Accepted Solution

by:
Dhaest earned 500 total points
ID: 24036839
Perhaps this can help you -- setting the combobox.selectedindex
Dim dt As DataTable = DirectCast(comboBox1.DataSource, DataTable)
Dim yourState as string
For i As Integer = 0 To dt.Rows.Count - 1
    Dim displayText As String = dt.Rows(i)(comboBox1.DisplayMember).ToString()
    Dim valueItem As String = dt.Rows(i)(comboBox1.ValueMember).ToString()
    ' Process the object depending on the type
    if valueItem = "TheValueYouAreSearchingFor" then
       comboBox1.SelectedIndex = i
       yourState = displayText
   end if
Next
0
 

Author Comment

by:Sheritlw
ID: 24036845
Thank you again... You really helped me out!
0
 

Author Closing Comment

by:Sheritlw
ID: 31565174
Thanks for the great answer
0
 

Expert Comment

by:rmsoftjsr
ID: 34840547
What ? if My Two customer in DisplayMember have identical name but different id.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Exit the loop 4 42
VB.Net - KeyPress Event 4 36
export data table to excel in vb.net 10 25
Calculate number of nights between two dates 5 20
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
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 …

776 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