Solved

VB.NET Combobox - tracking item IDs

Posted on 2004-07-30
6
2,322 Views
Last Modified: 2012-06-21
Hi,

I've been using the ComboBox control for quite some time now, but one thing I've never got around to finding out is whether you can track the ID of each item in the ComboBox.

For example, I have a table on a database with a list of People and each record is identified by a Primary Key.
What I want to be able to do is populate a ComboBox with People's names and when an item is selected from the ComboBox I want to immediately know the primary key of that item (not the SelectedIndex).

Is there some method of "tagging" each item in the ComboBox with an ID?

Up to now, I've been synchronising my combobox with an ArrayList. As you can imagine, this gets quite frustrating after implementing the nth ComboBox  :-\

Thanks,
-John.
0
Comment
Question by:krinpit
  • 3
  • 2
6 Comments
 
LVL 21

Assisted Solution

by:tovvenki
tovvenki earned 20 total points
Comment Utility
Hi,
use the valuemember property of the combobox like
ComboBox2.DataSource = dsCombo.Tables("tblItems")
ComboBox2.DisplayMember = "ItemDescription"
ComboBox2.ValueMember = "ItemID"

now  the SelectedValue property of the ComboBox will return you the id of the item.

regards,
venki
0
 

Author Comment

by:krinpit
Comment Utility
venki,

That is something I didn't know and is well worth knowing.

But how do you do this without specifying a datasource? I'm not trying to acheive anything in particular here, but I'd like to know if and how it is possible.

Thanks,
-John.
0
 
LVL 4

Accepted Solution

by:
jrandallsexton earned 105 total points
Comment Utility
John:

Venki is on the right track.

When you pull your results back from the database, let's say you're using a datareader, you could do something like this:

Dim idList as new sortedList

Do while myReader.Read
     dim strPerson as string
     dim strPersonID as string
     strPerson = myReader("PersonName").ToString
     strPersonID = myReader("PersonID").ToString
     idList.Add(strPersonID, strPersonName)
Loop

'now you have a sorted list in the following format:
keys: are the personids; values: are the person names

Now, bind the sortedlist to the combobox much as venki had suggested.

cboPerson.DataSource = idList
cboPerson.DisplayMember = idlist.value
cboPerson.ValueMemeber = idlist.key

'By the way, this code only represents the idea of how to do it - I do not guarantee my syntax is correct.



'then you can use
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:krinpit
Comment Utility
Thanks jrandallsexton. That's exactly what I was looking for. I actually modified your solution to use the DictionaryEntry class. See my full solution below

    Private Sub frmSyncComboboxes_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim myPeople As New ArrayList()
        Dim myDict As DictionaryEntry

        myPeople.Add(New DictionaryEntry(1, "John"))
        myPeople.Add(New DictionaryEntry(2, "Joe"))
        myPeople.Add(New DictionaryEntry(3, "Jack"))
        myPeople.Add(New DictionaryEntry(4, "Jim"))

        Me.ComboBox1.DataSource = myPeople
        Me.ComboBox1.DisplayMember = "Value"
        Me.ComboBox1.ValueMember = "Key"
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim person As DictionaryEntry = Me.ComboBox1.SelectedItem
        MessageBox.Show(person.Key & " - " & person.Value)
    End Sub
0
 

Author Comment

by:krinpit
Comment Utility
Actually the line  
    Dim myDict As DictionaryEntry
is inconsequential here....
0
 
LVL 4

Expert Comment

by:jrandallsexton
Comment Utility
Glad to have helped.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
This video discusses moving either the default database or any database to a new volume.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

9 Experts available now in Live!

Get 1:1 Help Now