[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

VB.NET Combobox - tracking item IDs

Posted on 2004-07-30
6
Medium Priority
?
2,372 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 21

Assisted Solution

by:tovvenki
tovvenki earned 80 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 420 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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

649 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