Take the tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
 
Are any values actually selected when before you click the button? –  Neil Knight Apr 20 '10 at 8:34
 
Yeah, I make sure I've selected something :) –  Andrew Apr 20 '10 at 8:48
add comment

4 Answers

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
share|improve this answer
add comment

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
share|improve this answer
 
Yeah, I'm aware it's 0 based and looking at the selected items is what I want :) –  Andrew Apr 20 '10 at 8:37
 
Then I can't think of anyhting more since the code above works (for up to 5 selections). –  ho1 Apr 20 '10 at 8:39
 
You don't have any hidden (0 width) columns so that you might be reading the wrong one? –  ho1 Apr 20 '10 at 8:40
 
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. –  Andrew Apr 20 '10 at 8:41
 
And nah I don't have any hidden columns. –  Andrew Apr 20 '10 at 8:41
show 1 more comment

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

share|improve this answer
add comment

Here's the single-line solution:

mylistview.Items.Cast(Of ListViewItem).Select(Function(lvi As ListViewItem)lvi.SubItems(1).Text).ToArray()
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.