Solved

Combo Box ValueMember/Selected Value

Posted on 2010-11-09
8
523 Views
Last Modified: 2012-05-10
With EE help I have managed to get a working Combo Box that loads sales reps using the code attached.

I want to capture the UserID (which is the ValueMember) in the combo box as a long integer so I can use it for filter a list view. But with what I have so far, my cboSalesRep.ValueMember is null. So I am missing something.

THANKS FOR ANY HELP.
Private Sub cboSalesRep_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboSalesRep.SelectedIndexChanged
        'Set the Public variable to the Combo Box Value
        lngSelectedSalesRepID = CLng(Me.cboSalesRep.SelectedValue)
    End Sub

Open in new window

0
Comment
Question by:Buck_Beasom
[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
  • 3
  • 2
8 Comments
 
LVL 8

Expert Comment

by:TheMozz
ID: 34094718
Try using the Items property along with the selected index property:

lngSelectedSalesRepID = CLng(Me.cboSalesRep.Items[Me.cboSalesRep.SelectedIndex].Value)
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34094989
SelectedValue will only work with DataBinding. You need to cast the SelectedItem to your custom type and retrieve the ID from it.
0
 

Author Comment

by:Buck_Beasom
ID: 34095121
The Mozz plan throws an error. VB doesn't seem to like that syntax.

Carl, you've gotten me this far (and I thought I'd give you a break!) I don't know how to code "cast the SelectedItem to your custom type". I'm WAY new at this.

Thanks.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34095248
Something like:

      Dim item As ListItem = CType(cboSalesRep.SelectedItem, ListItem)
      Dim id As Integer = item.ID
0
 
LVL 8

Expert Comment

by:TheMozz
ID: 34095272
Does it help to add a call to ToString() at the end of your code:

lngSelectedSalesRepID = CLng(Me.cboSalesRep.SelectedValue.ToString)

Ref: http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Studio_.NET_2005/Q_23230044.html
0
 

Author Comment

by:Buck_Beasom
ID: 34095488
Carl:

I put the code in the cboSalesRep_SelectedIndexChanged event.

The comma after SelectedItem throws an error.
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 34095630
Saying what?
0
 

Author Closing Comment

by:Buck_Beasom
ID: 34095822
Saying "YOU DA MAN."

I tried to combine two steps into 1, so instead of using CType the way you had it, and then setting the lngSelectedRepID variable to the resulting id value, I tried using Clng.

When I did it this way:
Private Sub cboSalesRep_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboSalesRep.SelectedIndexChanged
        'Set the Public variable to the Combo Box Value
        Dim item As ListItem = CType(cboSalesRep.SelectedItem, ListItem)
        Dim lngItemID As Long = item.ID

        lngSelectedSalesRepID = lngItemID
    End Sub

It worked like a charm!

Thanks again!!!!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
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…
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…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

749 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