Results 1 to 4 of 4

Thread: [RESOLVED] [2005] Comparing Two Listviews

  1. #1

    Thread Starter
    Hyperactive Member Jonny1409's Avatar
    Join Date
    Mar 2005
    Posts
    308

    Resolved [RESOLVED] [2005] Comparing Two Listviews

    I have 2 Listviews, which both contain a list of names. For example :

    Listview1
    ---------
    Jon
    Jim
    Alan
    Lee
    Claire
    Paul

    ListView2
    ---------
    Susan
    James
    Tony
    Jon
    Alan

    What I'd like to do is compare the two listboxes, and if a name appears in both lists, I'd like to remove it from List2

    I have the following code which populates the Listviews, and I assume I need to add something into the For...Next Loop, but I'm not sure what?

    VB Code:
    1. Dim aRow As DataRow
    2. Dim X As ListViewItem = Nothing
    3. For Each aRow In ds.Tables(0).Rows
    4.      X = New ListViewItem(aRow.Item("Name").ToString, 0)
    5.      Listview2.Items.AddRange(New ListViewItem() {X})
    6. Next

    Can anyone help please ?

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2005] Comparing Two Listviews

    What you can do is to check if an item is not in listview1 then add it to listview2
    VB Code:
    1. Dim aRow As DataRow = Nothing
    2.         Dim txt As String = String.Empty
    3.         For Each aRow In ds.Tables(0).Rows
    4.             txt = aRow.Item("Name").ToString
    5.             If ListView1.FindItemWithText(txt) Is Nothing Then
    6.                 ListView2.Items.Add(txt, 0)
    7.             End If
    8.         Next

  3. #3
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [2005] Comparing Two Listviews

    How are you populating your dataset in the first place? you may simply be able to avoid the duplicates by using different SQL syntax if you are pulling this data from a database. That puts the work on the database versus on the windows app, and will likely save time because you dont have to fill up 2 controls and then filter them. You simply only get back the records needed to fill the listviews.

  4. #4

    Thread Starter
    Hyperactive Member Jonny1409's Avatar
    Join Date
    Mar 2005
    Posts
    308

    Re: [2005] Comparing Two Listviews

    Thanks Stanav and Kleinma for your help - both are excellent suggestions.
    I'll go with one of those.

    Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width