Resolved Multiple Column ListView Load Issue

ggunter

Well-known member
Joined
Apr 23, 2008
Messages
137
Programming Experience
Beginner
I'm trying to load a listview control that contains two columns. Here is my current code:

VB.NET:
Try
    Dim lvwCollection As New ListView.ListViewItemCollection(Me.lvwWorkbooks)

    For Each row As DataRow In dtNatt.Rows
       Dim nuItem As New ListViewItem(row.Item("nattsPath").ToString(), 0)

       If Not (IsNothing(row.Item("nattsRegion").ToString())) Then
          nuItem.SubItems.Add(row.Item("nattsRegion").ToString())
       Else
          nuItem.SubItems.Add("   ")
       End If

       lvwCollection.Add(nuItem)

       nuItem = Nothing
    Next

    Me.lvwWorkbooks.Items.AddRange(lvwCollection)
 Catch ex As Exception
    MsgBox(ex.ToString())
 End Try
lvwWorkbooks = the name of the listview control on the form.
dtNatt = the data table

When I run this code, I receive an error message in the message box but when I click "Ok" the form appears with the listview loaded correctly.

The error message I get is 'System.ArgumentException: Cannot add or insert the item 'drive:\directory\filename' in more than one place. You must first remove it from its current location or clone it.'.

I've never tried to use a listview in DotNet so please be gentle. I tried the code in Help but that didn't work.

Even though what I have appears to work, just knowing an error is being generated will drive me nuts.

Would someone please point me in the right direction? There has to be a way to do this without throwing an error and I really want to learn to do this the correct way.
 
Last edited:
After working on this all morning, I figure it out a half hour after giving in and posting the question.

For anyone who may have a similiar question, here is my fix...
VB.NET:
Try
   Dim str(2) As String
   Dim nuItem As ListViewItem

   For Each row As DataRow In dtNatt.Rows
      str(0) = row.Item("nattsPath").ToString()
      str(1) = row.Item("nattsRegion").ToString()

      nuItem = New ListViewItem(str)
      Me.lvwWorkbooks.Items.Add(nuItem)
   Next

   nuItem = Nothing
Catch ex As Exception
   MsgBox(ex.ToString())
End Try

Guess I was overcomplicating the issue.
 
Back
Top