Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ListView (All Items) 1

Status
Not open for further replies.

JasonSummers

Programmer
Sep 11, 2002
26
US
How do I read all items in my ListView ???

I have a ListView control that I can drag and drop items into. I would like to read all contained (not only selected) items (no need for subitems) in this ListView into an array or another table. The goal being to use the items from the ListView to build a SQL select statement. I'm looking at the ListView.ListViewItemCollection.CopyTo but not really sure how to use this, or if that's the simple approach. Any suggestions or sample code will be appreciated. Thank You, Jason
 
You can just loop through all items using an ListViewItem object:

Code:
Dim itemX As ListViewItem
Dim i As Integer

For Each itemX In myListView
  myVar1(i) = itemX.Text
  myvar2(i) = itemX.SubItems(1).Text
  'And so on...
  i += 1 'to increment the index for the arrays
Next

or

Code:
Dim i, j As Integer
Dim myArray(myListView.Items.Count - 1, myListView.Columns.Count - 1) As String

For i = 0 To myListView.Items.Count - 1
  For j = 0 To myListView.Columns.Count - 1
    myArray(i,j) = myListView.Items(i).SubItems(j).Text
  Next
Next

In the second example you can use myArray(1,3) to get the text from the 4th column of the 2nd item (keep in mind the first item or column is index 0).

Regards, Ruffnekk
---
Is it my imagination or do buffalo wings taste just like chicken?
 
By the way, the ListView.Items.CopyTo method is not what you want to use. It copies all listview items as objects into an array; in other words, it doesn't fill an array of string values, but an array of ListViewItem objects.

Regards, Ruffnekk
---
Is it my imagination or do buffalo wings taste just like chicken?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top