vb.netdata-bindinglistboxlistboxitems

Cannot get the text of multiple selected items in a VB.NET ListBox because it's of type "datarowview"


I've got listbox1 in VB 2010, which is bound to a data source and displays values from a dataset. I bound it using the Designer - i.e. not via the code. I just selected the datasource in the properties of listbox1.

Now I want to retrieve the selected values. When I leave the listbox as single select, then ListBox1.SelectedValue.ToString does the job - it gives me the text of the selected item.

But I need it to allow multiple selections. This is my code:

    Dim items As ListBox.SelectedObjectCollection
    items = ListBox1.SelectedItems
    For Each i As String In items
        MsgBox(i)
    Next

And this is the error I get:

Conversion from type 'DataRowView' to type 'String' is not valid.

I've tried a few different ways to get the values of the selected items but there doesn't seem to be any straightforward way to do it. Is it impossible? Is it necessary to declare a new dataset and fill the listbox programmatically or something?


Solution

  • For Each drv As DataRowView In ListBox1.SelectedItems
            MessageBox.Show(drv.Item(0).ToString)
        Next