Question

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?

Was it helpful?

Solution

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

OTHER TIPS

Off the top of my head I think you can do:

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

Check out my question and answer. It allows you to break out the individual entries from a Database into list boxes.

Add 2 List Boxes and a text box to a form then copy this code, You will have to substitute your own Server and database etc entries.

Imports System
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Collections
Imports System.Data.SqlClient

Public Class Form1

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    ' get the data
    Dim SQLConnectionString As String = "Data Source=HL605\RIVWARE;Database=RIVWARE;Integrated Security=true;"
    Dim mySQLConnection As New SqlConnection(SQLConnectionString)

    ' Populate the list box using an array as DataSource. 
    mySQLConnection.Open()
    Dim SQLDataTable As New System.Data.DataTable

    'Create new DataAdapter
    Dim mySQLDataAdapter = New SqlDataAdapter("SELECT * FROM [Rivware].[dbo].[RivetTypes]", mySQLConnection)
    mySQLDataAdapter.Fill(SQLDataTable)
    ListBox1.DataSource = SQLDataTable
    ListBox1.DisplayMember = "RivetType"

    ' remove validation data from list
    Dim Count As Integer
    Dim X As Integer
    'get the column of data to search for
    For Count = 0 To ListBox1.DataSource.Columns.Count - 1
        X = InStr(ListBox1.DataSource.Columns.Item(Count).ToString, "Name")
        If X <> 0 Then Exit For
    Next
    ' now search for any invalid rows, in that column. those containing "---"
    Dim TheTable As DataTable = CType(ListBox1.DataSource, DataTable)
    Dim theRow As DataRow() = TheTable.Select()
    Dim RowNumber As Integer
    For RowNumber = 0 To UBound(theRow) - 1
        If theRow(RowNumber).Item(Count).ToString <> "---" Then
            ' data is OK so transer it to the other listbox
            ListBox2.Items.Add(theRow(RowNumber).Item(Count - 1).ToString)
        End If
    Next

    ListBox2.ClearSelected()
End Sub 'NewNew

Private Sub ListBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox2.SelectedIndexChanged
    TextBox1.Text = ListBox2.SelectedItem.ToString()
End Sub
End Class 'ListBoxSample3
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top