Hey all

I have a problem in my code I can't seem to find a solution for..
One combobox is being filled up with a datatable like this:

CbUsers.Datasource = usersDAO.getUsers()

The getUsers() returns a datatable from my acces database.
Users have a name and id, I want to use both of them but it should be something like this:

CbUsers.DisplayMember = "username"
CbUsers.valueMember = "userid"

In other words, I want to see the user name, but work with the user ID. So far the code worked, but it only displays stuff.

Now, in a method I want to use the id of the selected user in the combobox but that has been given me some trouble.

If I use this line I get a DataRowView:

CbUsers.selectedValue

When I try to retrieve information from it, it throws an exception.
I've tried lots of different code and it never worked..

If I put the DataRowView in a local variable and do this:

id = drv.items(0)

I only get exceptions..

Can someone help me fix my code? Or maybe provide a way to work with a combobox that displays usernames but works with their id's?

Thanks in advance,
Lupus

A bit extra information for my problem:

The reason I wanted to do it like this is because it can happen that two users have the same name. So I wanted to work with something unique. I thought working with the userid would help but seeing my code doesn't work I had to find a temporay "fix".

In the same table they also have a username (or a login) which is unique as well, I forgot about this field! So right now I'm working with their username but this also results that the combobox displays the usernames instead of the persons name. To get my value I then use: CbUsers.Text

But I still want an answer to my first post though.

I'm guessing nobody can help me? :(

You need to use SelectedItem Property.

Dim dr As DataRowView = CType(ComboBox1.SelectedItem, DataRowView)

If Not IsNothing(dr) Then
   ....        
End If

Thank you very much! :)

It works like how I wanted, I can't believe I looked over that!
That proves how much of a newbie I am at this.. :)

Thanks again!

Regards,
Lupus

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.