?
Solved

Retrieve Index of Matching value in Combobox

Posted on 2009-03-31
11
Medium Priority
?
723 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
[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
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses
Course of the Month9 days, 2 hours left to enroll

764 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