Link to home
Start Free TrialLog in
Avatar of Erick37
Erick37Flag for United States of America

asked on

MultiExtended ListBox SelectedValue

What is the proper way to extract the SelectedValue property from a data bound ListBox that has multiple selections?
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Hi Erick;

This can be accomplished by doing the following.

For Each selected As String In ListBox1.SelectedItems
    ' Each time through the loop
    ' will give the next selected item
    ' from the list box
    Console.WriteLine(selected)
Next


I hope this was of some help

-Fernando
Avatar of Erick37

ASKER

Hi Fernando,  

I get the following error using your suggestion:

An unhandled exception of type 'System.InvalidCastException' occurred in microsoft.visualbasic.dll
Additional information: Cast from type 'DataRowView' to type 'String' is not valid.
Hi Erick;

What is stored in the Listbox and how was it declared?

Avatar of Erick37

ASKER

It is a bound listbox using the NorthWind database for testing.  Settings are as follows:

DataSource: DataSet11.Sales_by_Year
DisplayMember: OrderID
ValueMember: ShippedDate

Using ListBox2.SelectedValue will return the correct value for the ValueMember for only the currently selected item.  I need to retrieve the value for all selections because the ListBox is set as MultiExtended, so multiple items can be selected.

Thanks.
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Erick37

ASKER

That is exactly what I was looking for, thanks!