Solved

VB.NET Combobox - tracking item IDs

Posted on 2004-07-30
6
2,343 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Server Error 11 58
How would you add MULTITHREADING to the attached C# code? 4 62
Simple Injector with Web Service 4 39
Where did System.Data.Objects go? 2 23
A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
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 …

785 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