PC Review Forums Newsgroups Microsoft DotNet Microsoft Dot NET Framework Forms Setting the SelectedItem of a ComboBox using an ID

Reply

Setting the SelectedItem of a ComboBox using an ID

 
Thread Tools Rate Thread
Old 04-11-2003, 10:39 AM   #1
Lee Ottaway
Guest
 
Posts: n/a
Default Setting the SelectedItem of a ComboBox using an ID


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
  Reply With Quote
Old 05-11-2003, 02:18 PM   #2
Ken Cooper
Guest
 
Posts: n/a
Default Re: Setting the SelectedItem of a ComboBox using an ID

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



  Reply With Quote
Old 05-11-2003, 02:56 PM   #3
Ken Cooper
Guest
 
Posts: n/a
Default Re: Setting the SelectedItem of a ComboBox using an ID

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


  Reply With Quote
Old 05-11-2003, 04:53 PM   #4
Lee Ottaway
Guest
 
Posts: n/a
Default Re: Setting the SelectedItem of a ComboBox using an ID

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!
  Reply With Quote
Reply



Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off