MultiExtended ListBox SelectedValue

What is the proper way to extract the SelectedValue property from a data bound ListBox that has multiple selections?
LVL 32
Who is Participating?
Fernando SotoConnect With a Mentor RetiredCommented:
Hi Erick;

Try this out

Dim SelectItems As DataRowView

For Each SelectItems In ListBox1.SelectedItems
    Console.WriteLine("Display Member = {0}", SelectItems.Row.ItemArray(0))
    Console.WriteLine("Value Member = {0}", SelectItems.Row.ItemArray(1))
Fernando SotoRetiredCommented:
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

I hope this was of some help

Erick37Author Commented:
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.
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Fernando SotoRetiredCommented:
Hi Erick;

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

Erick37Author Commented:
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.

Erick37Author Commented:
That is exactly what I was looking for, thanks!

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.