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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

listview 2

Status
Not open for further replies.

brew2

Programmer
Joined
Apr 29, 2007
Messages
21
Location
US
Working with a listview the data is entered from Access into a collection. Then it is supposed to populate a listview with this code.
CM is a set to a class of people.
Code:
For Each CM In c
            sKey = Format(CM.ID, "0000")
            With lvwDisplay.Items
                Dim mItem As ListViewItem = New ListViewItem
                mItem.Text = sKey
                mItem.SubItems.Add(IIf(CM.MarriedName =  vbNullString, CM.LastName & ", " & CM.FirstName, _
                        CM.MarriedName & ", " & CM.FirstName & " (" & CM.LastName & ")"))
                mItem.SubItems.Add(CM.Address)
                mItem.SubItems.Add(IIf(CM.City <> "", CM.City & ", " & CM.State & " " & CM.Zip, vbNullString))
                mItem.SubItems.Add(CM.ClassYear)
                mItem.SubItems.Add(IIf(CM.Deceased, "D", ""))
            End With
The collection has all of the data but this routine does not populate the listview. Nor does it throw an exception.

Thanks for the help.

 
Move the Dim mItem statement before the With statement. Then add mItem to the listview after the End Width statement
Code:
...
[b][COLOR=blue]Dim mItem As ListViewItem = New ListViewItem[/color][/b]
With lvwDisplay.Items
   blah, blah, blah...
End With
[b][COLOR=blue]lvwDisplay.Items.Add(mItem)[/color][/b]

 
I would add that it might be even better to move the Dim mItem right before the For Each statement.

It even seems that your With statement looks nice but doesn't do anything, remove it.

Code:
[b]Dim mItem As ListViewItem[/b]
For Each CM In c
            sKey = Format(CM.ID, "0000")
            [s]With lvwDisplay.Items[/s]
                [s]Dim mItem As ListViewItem = New ListViewItem[/s]
                [b]mItem = New ListViewItem[/b]
                mItem.Text = sKey
                mItem.SubItems.Add(IIf(CM.MarriedName =  vbNullString, CM.LastName & ", " & CM.FirstName, _
                        CM.MarriedName & ", " & CM.FirstName & " (" & CM.LastName & ")"))
                mItem.SubItems.Add(CM.Address)
                mItem.SubItems.Add(IIf(CM.City <> "", CM.City & ", " & CM.State & " " & CM.Zip, vbNullString))
                mItem.SubItems.Add(CM.ClassYear)
                mItem.SubItems.Add(IIf(CM.Deceased, "D", ""))
            [s]End With[/s]
            [b]lvwDisplay.Items.Add(mItem)[/b]


Hope it helps.
 
Thanks to both of you for the help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top