Solved

VB.NET Combobox - tracking item IDs

Posted on 2004-07-30
6
2,351 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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

Suggested Solutions

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…
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

856 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