krinpit
asked on
VB.NET Combobox - tracking item IDs
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.
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.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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(ByV al 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
Private Sub frmSyncComboboxes_Load(ByV
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
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
End Sub
ASKER
Actually the line
Dim myDict As DictionaryEntry
is inconsequential here....
Dim myDict As DictionaryEntry
is inconsequential here....
Glad to have helped.
ASKER
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.