Setting the SelectedItem of a ComboBox using an ID

  • Thread starter Thread starter Lee Ottaway
  • Start date Start date
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
 
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
 
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
 
Back
Top