Solved

VB.NET Combobox - tracking item IDs

Posted on 2004-07-30
6
2,334 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
ID: 11675521
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
ID: 11675568
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
ID: 11678375
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:krinpit
ID: 11678665
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
ID: 11678695
Actually the line  
    Dim myDict As DictionaryEntry
is inconsequential here....
0
 
LVL 4

Expert Comment

by:jrandallsexton
ID: 11678778
Glad to have helped.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
This code tracks birthdays 3 60
Entity Framework 7 32
Capture logon name 13 37
Crystal reports vb.net 2 7
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 article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

920 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

15 Experts available now in Live!

Get 1:1 Help Now