views:

31

answers:

3

I have a ListView control on my form set up like this in details mode:

alt text

What I would like to do, is get all the values of the data cells when the user presses the Delete booking button.

So using the above example, my array would be filled with this data:

values(0) = "asd"
values(1) = "BS1"
values(2) = "asd"
values(3) = "21/04/2010"
values(4) = "2"

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(i).Text
        i = i + 1
    Next
End Sub

But only values(0) gets filled with the first data cell of the selection (in this case "asd") and the rest of the array entries are blank. I have confirmed this with a breakpoint and checked the array entries myself.

I'm REALLY lost on this, and have been trying for about an hour now. Any help would be appreciated, thanks :)

Also please note that there can only be one row selection at once. Thanks.

A: 

I don't know how you populate the list but for me it seems that you need to iterate the item.Subitems to get the values. I don't know VB but I think you can use a something like :

For i As Integer = 0 To item.SubItems.Count
   values(i) = item.SubItems(i).Text
costin
I populate it from a subroutine in my module. And I am iterating through the subitems aren't I? With the `item.SubItems(i).Text`.
Joe
In your code the i variable counts the items not the subitems.
costin
A: 

Try this instead..

    Dim values() As String

    If items.Count = 1 Then
        subitems = items(0).SubItems
        ReDim values(0 To subitems.Count - 1)
        For i As Integer = 0 To subitems.Count
            values(i) = subitems(i).Text
        Next
    Else
        Throw New Exception("boom")
    End If
gbogumil
A: 

Costin is right about needing to iterate the subitems and if you're sure that there'll only be one selection iterating through the items just confuse things, I'd write it as:

  Dim values As New List(Of String)
  If ManageList.SelectedItems.Count > 0 Then
     For Each item As ListViewItem.ListViewSubItem In ManageList.SelectedItems(0).SubItems
        values.Add(item.Text)
     Next
  End If
ho1
Ahhhh, it works perfectly! Thank you! :D
Joe