PC Review
Forums
Newsgroups
Microsoft DotNet
Microsoft Dot NET Framework Forms
Setting the SelectedItem of a ComboBox using an ID
Forums
Newsgroups
Microsoft DotNet
Microsoft Dot NET Framework Forms
Setting the SelectedItem of a ComboBox using an ID
![]() |
Setting the SelectedItem of a ComboBox using an ID |
|
|
Thread Tools | Rate Thread |
|
|
#1 |
|
Guest
Posts: n/a
|
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 |
|
|
|
#2 |
|
Guest
Posts: n/a
|
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 "Lee Ottaway" <lee.ottaway@nuesoft.co.uk> wrote in message news:17ae13cd.0311040239.418cc93e@posting.google.com... > 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 |
|
|
|
#3 |
|
Guest
Posts: n/a
|
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 |
|
|
|
#4 |
|
Guest
Posts: n/a
|
Yes, I've come to the same conclusion.
Thanks for your input. Lee *** Sent via Developersdex http://www.developersdex.com *** Don't just participate in USENET...get rewarded for it! |
|
![]() |
|
| Thread Tools | |
| Rate This Thread | |
|
|

Main Page 

