Solved

Retrieve Index of Matching value in Combobox

Posted on 2009-03-31
11
712 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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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 …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

821 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