I have beeb unable to figure out how to get data from a list view. It needs to be a multislect, and I need to get the data from a specific column. my listview consists of a 3 column with data populated from a database. I am after the data in the 3rd column. I can get it to work with a single select, but not multiselect. Any help would be appreciated.

Recommended Answers

All 4 Replies

For multiselected items. You use:

        Dim selectedItems = MyListView.SelectedItems

        ' iterated through the selected items
        For Each item As ListViewItem In selectedItems
            ' get data from the third column
            Dim MyData = item.SubItems(2).Text
        Next

Column 1: ListViewItem
Column 2: ListViewItem.SubItems(0)
Column 3: ListViewItem.SubItems(1)

awswome, thank you very much.

A ListView is usually used for display only. If you want to be able to select columns I suggest you try a DataGridView.

I posted this a couple of days ago. Unfortunately I posted it in another "ListView" thread. I hope it can still be of some help.

Sample code. Start with a blank four-column datagridview control and a textbox control set to multiline

 Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        'Keep track of the last selected sort order using the Tag property

        For Each col As Object In DataGridView1.Columns
            col.tag = -1
        Next

        'populate a four column DataGridView with some data

        DataGridView1.Rows.Add({"87013", "87013", "Fred", "Stalder"})
        DataGridView1.Rows.Add({"90215", "90215", "Virgil", "Samms"})
        DataGridView1.Rows.Add({"600021", "600021", "Rod", "Kinnison"})
        DataGridView1.Rows.Add({"2457", "2457", "Fred", "Flintstone"})
        DataGridView1.Rows.Add({"7259", "7259", "Nils", "Bergenholm"})

    End Sub

    Private Sub DataGridView1_CellClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick

        Dim dgv As DataGridView = sender
        TextBox1.Clear()

        For i As Integer = 0 To dgv.RowCount - 1
            TextBox1.AppendText(dgv.Rows(i).Cells(e.ColumnIndex).Value & vbCrLf)
        Next

    End Sub

End Class
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.