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

x
?
Solved

Combobox - 2 values - ID and Name

Posted on 2004-10-28
8
Medium Priority
?
483 Views
Last Modified: 2010-04-23
With the standard VB.NET ComboBox is it possible to store two values within the drop down menu. What I want to do is store the ID and the Name within the ComboBox but only save the ID relating to the name to the database.  Then went the record is loading pickup the ID and match it with the Name within the drop down menu.

Is this possible? If so now?
0
Comment
Question by:PeterErhard
[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
8 Comments
 

Expert Comment

by:SubSonicS
ID: 12440162
You can create a custom string and work on it with Left and Right Functions.

As for example this string should reserve the first 4 characters for the id(to store up to 9999) ids. 5 chracters for 99999 and so on...

This is a little example: (example name is "Smith")

my string should be like this: 12   Smith
P.S. it is easy to store it reserving the chars...

If i want to get the ID then i should use a command like this
dim myID
myID = myComboBox.Items.Item(1)
myID = Left(myID, 5)
myID = lTrim(myID)
myID = rTrim(myID)
Now i have my ID number...

To get the name should be:
dim myNAME
myNAME = myComboBox.Items.Item(1)
myNAME = right(myNAME, (Len(myNAME) - 5))
Now i have my name.....

With a For Next function you can get all IDs
And with the instr() function you can search for IDs and/Or for Names!

Hope this help..

0
 

Author Comment

by:PeterErhard
ID: 12440274
Cheers for that.

I was hoping that you could have 2 fields within the combo box with 1 containing the ID which is hidden but you can get that value when a name is selected.

Is that not possible?
0
 
LVL 14

Accepted Solution

by:
ptakja earned 2000 total points
ID: 12440960
Sure you can. BUT, you need to do with with databinding.  Create a DataTable object that has two fields: A display field and a Value field. Something like this:

' Create new DataTable object
        Dim dt As New DataTable

' Add a couple columns to it
        dt.Columns.Add("Display", GetType(String))
        dt.Columns.Add("Value", GetType(Integer))

' Array of names:
        Dim aNames As String() = {"Jim", "Joe", "Mary", "Cathy", "Jeff"}

         ' Add names to data table by building DataRows
         for ii as Integer = 0 to aNames.Count -1
             Dim row As DataRow = dt.NewRow
             row("Display") =  aNames(ii)
             row("Value") = ii
             dt.Rows.Add(row)
         Next

        ' Now bind to combo

        ComboBox1.DataSource = dt
        ComboBox1.DisplayMember = "Display"
        ComboBox1.ValueMember = "Value"

And you are good to go.

To ge the value of the selected item use:

Combobox1.SelectedValue
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:PeterErhard
ID: 12441088
Thanks for that ptakja.  I'm using the following and it works perfectly:

****************************************************
        Dim cmd As SqlCommand = cnn.CreateCommand
        cmd.CommandType = CommandType.StoredProcedure
        cmd.CommandText = "procLoadDisabilityOptions"

        Dim reader As SqlDataReader = cmd.ExecuteReader

        Dim dt As New DataTable()
        dt.Columns.Add("DisabilityIDAuto", GetType(Integer))
        dt.Columns.Add("Disability", GetType(String))

        While reader.Read
            With reader
                Dim row As DataRow = dt.NewRow
                row("Disability") = .GetValue(.GetOrdinal("Disability"))
                row("DisabilityIDAuto") = .GetValue(.GetOrdinal("LookUp_DisabilityIDAuto"))
                dt.Rows.Add(row)
            End With
        End While

        reader.Close()

        ComboBox_MainDisability.DataSource = dt
        ComboBox_MainDisability.DisplayMember = "Disability"
        ComboBox_MainDisability.ValueMember = "DisabilityIDAuto"
****************************************************

Is there a way once I have the ValueMember saved in the database that when loading the data I can go straight to the item within the drop down menu with the ID I have saved?
0
 
LVL 2

Expert Comment

by:ChillAmmar
ID: 12441464
Use The following for selecting the value in the combobox

ComboBox1.SelectedValue = <id From Database>

0
 
LVL 14

Expert Comment

by:ptakja
ID: 12442928
What ChillAmmar said. The SelectedValue property is read/write. Meaning if you set it to something AND an existing item in the combo list collection contains that value it will be selected.
0
 

Author Comment

by:PeterErhard
ID: 12458760
Thanks - all works as expected!

One final question. I'm using the drop down list for the combo-box. At the moment it always defaults to the first option within the list. How can I make it blank to begin with so the user remembers to select an option from the list?
0
 
LVL 14

Expert Comment

by:ptakja
ID: 12463945
Try adding an item before data binding.

ComboBox1.Items.Add("<SELECT ITEM>")
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

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