0

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()
4
  • Perhaps you could explain what you are trying to do because a ListView doesnt have dimensions. Commented Jun 14, 2016 at 19:51
  • Sure, with the values that I have in the list view I'd like them to be store in an array with the same format. For example: a listview with 4 columns and 100 rows would be stored in an array with the listview items stored in a 4X100 array. It would be done dynamically. Commented Jun 14, 2016 at 19:59
  • 2
    That will require a loop to copy 400 ListViewSubItems to the array; there is no "passing" the control to an array and it unpacks itself.. A better approach would be to put the data into a DataTable (it will BE in 4 rows x 100 cells very much like an array) then bind it to a DataGridView to display it, Commented Jun 14, 2016 at 20:04
  • I concur ^ with that guy! Commented Jun 14, 2016 at 20:18

2 Answers 2

0

Try this:

    Dim allItems(ListView1.Items.Count-1) As ListViewItem
    ListView1.Items.CopyTo(allItems, 0)

    ' To iterate
    For Each r In allitems
         ' Row is r.index 
         For Each c In r.SubItems
            ' Text in each column is c.Text
         Next
     Next
Sign up to request clarification or add additional context in comments.

Comments

-1

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

5 Comments

Hi sir, thanks a lot. Could one also show how the subitems could be displayed dynamically?
Sure, I notice how you you have the rows done dynamically; how would you have the column's dynamically displayed as well? For example- if I have 4 columns for one listview and 6 for another.
Now I get it. I just updated the answer with another example. Just don't forget to accept the answer, if it satisfies your question, of course.
Sir, this is awesome. For a final step, how would one take this same array and export it to a live excel sheet? A listview with 100 rows and 4 columns for example would be 100 rows and 4 columns in Excel. Thanks
This is a whole new story :). There are numerous ways. Post another questions with details on how you going to interact with Excel - add-in, VBA, something else. But in the first place, try to do it yourself. Ask questions when you get stuck.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.