Setting the SelectedItem of a ComboBox using an ID

L

Lee Ottaway

I have a class called cComboItem which I use to add two bits of data,
ID and Description which I then add to my combobox using the following
code:

Do While rd.Read()
Dim oItem As New cComboItem(rd("ID"), rd("Description"))
cboProviderType.Items.Add(oItem)
Loop

I then store just the selected ID field against the provider. However
once this form is closed and I re-open it, I want to be able to set
the selecteditem of the combobox back to proper entry using the ID
that has been stored in the database. I know I could loop through the
Provider Type table until I find the matching ID and put that text
into the combobox but there must be a way of simply saying to the
combo, here's the ID, now display me the description field that
matches this.

Any ideas?


Lee
 
K

Ken Cooper

I agree - THERE HAS TO BE AN EXISTING METHOD TO DO THIS - Surely? What are
we missing?

My problem is almost identical - I have a ComboBox which displays Job
Statuses where the ValueMember is JobStatusID.

When a user selects a Job, I want to display the JobStatus options, with the
current JobStatus selected from the known JobStatusID. My database saves the
ID not the Job Status text so the FindString methods do not help.

Is there is an existing method to determine what SelectedIndex should be
from a known item value?

Ken
 
K

Ken Cooper

In the absence of anything better the following seems neat enough ...

'The ComboBox displays a collection of objects

'DisplayMember is the property of each object which is shown as text

'ValueMember is the property of each object which provides the value for
each item

For i As Integer = 0 To combo.Items.Count - 1

If combo.Items(i)(combo.ValueMember) = intValue Then

combo.SelectedIndex = i

Exit For

End If

Next



Ken
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top