Please help - technical question on ListBox...

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Dear Experts

I got one technical question on using ListBox. I don't know how to solve it, please help.

Scenario
I placed a ListBox on the Form and I would like to allow my users to do multiple selection. After users select the item(s), I want to collect all the corresponding "SelectValue"(s)(SelectedValue is a property of the ListBox, as you know)

However, no matter how hard I tried, I was unable to iterate through all the selected items and found out the "SelectedValue"(s). Could you please tell me how to code

The setting of the ListBox is the following
Selection Mode: Multiple Extende
DataSouce: Dataset1.Customers (I am using ADO.NET
DisplayMember: CustomerNam
ValueMember: CustomerID (it is the source of "SelectedValue"

Thanks a lot for your help
Vincen
 
All items added to list boxes are derived from objects. When you bind
a datasource to a list box you are in effect binding a dataview and
the objects of a dataview are datarowviews.

To iterate through the selected items you you can use the for or
foreach constructs:

Datarowrow dr;

for (i=0;i<listbox.selecteditems.count;i++)
{
dr=(datarowview) listbox.selecteditem;
//do what you want with the row
}

foreach (Datarowview dr in listbox.selecteditems)
{
//do what you want with the row although if you modify the row you
will probably get an error as the enumerator will be modified.
}
 
Back
Top