LRAC LRAC - 5 months ago 22
Vb.net Question

How to pass listview into array?

I have a ListView with 4 columns and close to 1500 rows. I want to know how to pass it to an array. I'd like the rows and columns in the ListView to match the dimensions in the array.

Dim ListView1 As ListView
ListView1 = New ListView()

Answer

Given the data is already in the ListView you could do something like this:

    Dim matrix(ListView1.Items.Count - 1, 3)

    For r As Integer = 0 To ListView1.Items.Count - 1
        Dim itemX = ListView1.Items(r)

        matrix(r, 0) = itemX.Text
        matrix(r, 1) = itemX.SubItems(0).Text
        matrix(r, 2) = itemX.SubItems(1).Text
        matrix(r, 3) = itemX.SubItems(2).Text
    Next

UPDATE

So this is how you do it by dynamically reference columns - Subitems:

Dim matrix(ListView1.Items.Count - 1, 3)

For r As Integer = 0 To ListView1.Items.Count - 1
    Dim itemX = ListView1.Items(r)

    matrix(r, 0) = itemX.Text ' The first item in the array 
    For c As integer = 1 To itemX.Subitems.Count
        matrix(r, c) = itemX.Subitem(c - 1).Text
    Next
Next