PC Review Forums Newsgroups Microsoft DotNet Microsoft Dot NET Framework Forms How to iterate over a combobox's items?

Reply

How to iterate over a combobox's items?

 
Thread Tools Rate Thread
Old 12-01-2007, 11:46 PM   #1
=?Utf-8?B?UXVpbWJseQ==?=
Guest
 
Posts: n/a
Default How to iterate over a combobox's items?


Say cbRoadwayType is my combobox:

for (int i = 0; i < cbRoadwayType.Items.Count; i++)
{
cbRoadwayType.SelectedIndex = i;
int val = (int)cbRoadwayType.SelectedItem // INVALID EXCEPTION cast

if (val != FILTER_ALL && val != FILTER_NOT_SET)
{
roadwayList.Add(val);
}
}

But, I get an invalid exception cast. How do I just get the value of the
combobox item, based on an index?!
  Reply With Quote
Old 12-01-2007, 11:58 PM   #2
=?Utf-8?B?UXVpbWJseQ==?=
Guest
 
Posts: n/a
Default RE: How to iterate over a combobox's items?

Oops, sorry, my original code was:

for (int i = 0; i < cbRoadwayType.Items.Count; i++)
{
int val = (int)cbRoadwayType.Items[i];

if (val != FILTER_ALL && val != FILTER_NOT_SET)
{
roadwayList.Add(val);
}
}


"Quimbly" wrote:

> Say cbRoadwayType is my combobox:
>
> for (int i = 0; i < cbRoadwayType.Items.Count; i++)
> {
> cbRoadwayType.SelectedIndex = i;
> int val = (int)cbRoadwayType.SelectedItem // INVALID EXCEPTION cast
>
> if (val != FILTER_ALL && val != FILTER_NOT_SET)
> {
> roadwayList.Add(val);
> }
> }
>
> But, I get an invalid exception cast. How do I just get the value of the
> combobox item, based on an index?!

  Reply With Quote
Old 13-01-2007, 02:32 PM   #3
Peter Thornqvist
Guest
 
Posts: n/a
Default Re: How to iterate over a combobox's items?

Are the combox items ints to begin with?

--
Regards, Peter


  Reply With Quote
Old 14-01-2007, 10:22 AM   #4
Chad Z. Hower
Guest
 
Posts: n/a
Default Re: How to iterate over a combobox's items?

Quimbly wrote:
> for (int i = 0; i < cbRoadwayType.Items.Count; i++)
> {
> int val = (int)cbRoadwayType.Items[i];
>
> if (val != FILTER_ALL && val != FILTER_NOT_SET)
> {
> roadwayList.Add(val);
> }
> }


We dont know what type is in your combobox. What did you add?

Furthermore, you should use a foreach instead of a normal for loop in
this situation.

--
Chad Z. Hower
Microsoft Regional Director
"Programming is an art form that fights back"
http://www.KudzuWorld.com/
Need a professional technical speaker at your event?
http://www.woo-hoo.net
  Reply With Quote
Old 14-01-2007, 05:52 PM   #5
IKEAS
Guest
 
Posts: n/a
Default Re: How to iterate over a combobox's items?

maby you must use foreach

foreach(object o in combo.items)
{
try
{
int i = (int)o;
.... somes codes
}
catch
{
Console.WritLine("error with object type " + o.GetType().ToString())
}
}

Quimbly a écrit :
> Say cbRoadwayType is my combobox:
>
> for (int i = 0; i < cbRoadwayType.Items.Count; i++)
> {
> cbRoadwayType.SelectedIndex = i;
> int val = (int)cbRoadwayType.SelectedItem // INVALID EXCEPTION cast
>
> if (val != FILTER_ALL && val != FILTER_NOT_SET)
> {
> roadwayList.Add(val);
> }
> }
>
> But, I get an invalid exception cast. How do I just get the value of the
> combobox item, based on an index?!

  Reply With Quote
Old 14-01-2007, 09:00 PM   #6
Peter Thornqvist
Guest
 
Posts: n/a
Default Re: How to iterate over a combobox's items?

> maby you must use foreach

It should work either way. I suspect the OP didn't store ints in the
combobox to start with. That's why he gets the invalid cast error

--
Regards, Peter


  Reply With Quote
Old 15-01-2007, 10:12 PM   #7
Peter Thornqvist
Guest
 
Posts: n/a
Default Re: How to iterate over a combobox's items?

> The problem appears to be that the ComboBox.Items property doesn't expose
> the values in the list, but instead a DataRowView obj, which doesn't cast
> well to an int! There must be a way to easily iterate over items in a
> combobox, no?!


If the items are DataRowViews, you can't typecast them directly to ints. Can
you show the code you use to fill the combobox?

--
Regards, Peter


  Reply With Quote
Old 16-01-2007, 08:49 PM   #8
Keith Patrick
Guest
 
Posts: n/a
Default Re: How to iterate over a combobox's items?

Sounds like you're binding directly to the dataset, which means you need one
more level of indirection. Try this:
foreach (DataRowView item in cbRoadwayType.Items) {
Int32 val = (Int32) item["ValueColumnName"];
...
}




  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