0

I have a ListView control set up in details mode, and on a button press I would like to retrieve all column values from that row in the ListView.

This is my code so far:

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    Dim items As ListView.SelectedListViewItemCollection = _
    Me.ManageList.SelectedItems
    Dim item As ListViewItem
    Dim values(0 To 4) As String
    Dim i As Integer = 0
    For Each item In items
        values(i) = item.SubItems(1).Text
        i = i + 1
    Next
End Sub

But values just comes out as an empty array. Any ideas? I just want the values array to be filled with the data of that ListView row.

Cheers.

2
  • Are any values actually selected when before you click the button? Commented Apr 20, 2010 at 8:34
  • Yeah, I make sure I've selected something :) Commented Apr 20, 2010 at 8:48

4 Answers 4

2

Here's the single-line solution:

mylistview.Items.Cast(Of ListViewItem).Select(Function(lvi As ListViewItem)lvi.SubItems(1).Text).ToArray()
Sign up to request clarification or add additional context in comments.

Comments

1

You need to iterate the SubItems, not the selected items. Fix:

If Me.ManageList.Items.Count <> 1 Then Exit Sub
Dim row As ListViewItem = Me.ManageList.Items(0)
Dim values(0 To row.SubItems.Count-1) As String
Dim i As Integer = 0
For Each item As ListViewItem.ListViewSubItem In row.SubItems
  values(i) = item.Text
  i = i + 1
Next

Comments

0

Just to check, are you aware that item.SubItems(1).Text will get the texts from the second column? And that since you're using SelectedItems it'll only look at the currently selected items in the ListView.

Edit: Also, are you sure there will always just be a maximum of 5 selected items in the ListView? Otherwise you'll have problems with

Dim values(0 To 4) As String

6 Comments

Yeah, I'm aware it's 0 based and looking at the selected items is what I want :)
Then I can't think of anyhting more since the code above works (for up to 5 selections).
You don't have any hidden (0 width) columns so that you might be reading the wrong one?
If I try: MessageBox.Show(values(2)) or something the messagebox comes up with no text, making me think the array is empty. But yeah, the code does compile and run fine.
And nah I don't have any hidden columns.
|
0

Old question, I know... maybe this helps for someones reference.. stumbled upon this question in a search for something else.

This works... not sure if the original poster somehow used an empy colelciton of selected items...

Also, dynamically resizing array based on selected items to overcome unforseen bugs...

    Dim valueArray(mylistview.SelectedItems.Count - 1) As String
    Dim i As Integer

    For Each Item As ListViewItem In mylistview.SelectedItems
        valueArray(i) = Item.Text
        i += 1
    Next

To get subitems, use item.subitems(1).text ... subitems(2).text.. etc

Cheers

Comments

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.