?
Solved

Compare Two ListViews

Posted on 2011-09-24
34
Medium Priority
?
619 Views
Last Modified: 2013-11-27
Greetings,

 I use Visual Studio 2010, Visual Basic Project. Is it possible to compare two listview colums and only export the onces that are true. I will be exporting to excel and I know how to export it, just not how to get the values that are a match. Please help with providing code!

Thanks~
0
Comment
Question by:Jack_Jones
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 17
  • 5
  • 5
  • +2
34 Comments
 
LVL 12

Expert Comment

by:Deepu Chowdary
ID: 36594349
0
 
LVL 1

Author Comment

by:Jack_Jones
ID: 36594367
I know how to do the sort colum, looking more at how to extract a value if it's a match in listview1 and listview2. Like a nice loop to check if true then export it.
0
 
LVL 1

Expert Comment

by:rockas1982
ID: 36601406
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Exp As String
        For A = 0 To ListView1.Items.Count - 1
            For B = 0 To ListView1.Items.Count - 1
                If ListView1.Items(A).Text = ListView2.Items(B).Text Then
                    Exp = Exp & ListView1.Items(A).Text
                End If
            Next
        Next
        MsgBox(Exp)
    End Sub

print screen
0
Linux Academy Android App Now Supports Chromecast

We have some fantastic news for our Android fans. We’re so excited to announce that the Linux Academy Android app is now available with Chromecast support. That’s right – simply download the latest update of the Linux Academy App and start casting your favorite course videos!

 
LVL 1

Author Comment

by:Jack_Jones
ID: 36602215
So for me it only checks 1 item for some reason?
0
 
LVL 1

Expert Comment

by:rockas1982
ID: 36602219
could you paste the code ?
0
 
LVL 1

Author Comment

by:Jack_Jones
ID: 36602242
I also get,

 Variable 'Exp' is used before it has been assigned a value. A null reference exception could result at runtim.

Dim Exp As String
        Try
            For A = 0 To ListView1.Items.Count - 1
                For B = 0 To ListView1.Items.Count - 1
                    If ListView1.Items(A).Text = ListView2.Items(B).Text Then
                        Exp = Exp & ListView1.Items(A).Text
                    End If
                Next
            Next
            ListView3.Items.Add(Exp)
        Catch
        End Try

Open in new window

0
 
LVL 1

Expert Comment

by:rockas1982
ID: 36602285
try this

Dim Exp As String
        Try
            For A = 0 To ListView1.Items.Count - 1
                For B = 0 To ListView1.Items.Count - 1
                    If ListView1.Items(A).Text = ListView2.Items(B).Text Then
                        ListView3.Items.Add(ListView1.Items(A).Text)
                    End If
                Next
            Next
        Catch
        End Try

Open in new window


p.s. the warning you get it's not important
0
 
LVL 1

Author Comment

by:Jack_Jones
ID: 36602331
stops on the first true value
0
 
LVL 1

Expert Comment

by:rockas1982
ID: 36602358
i don't understand why .. do you have team viewer to show me what are you doing ? or send me the project if it can run on any pc
0
 
LVL 1

Author Comment

by:Jack_Jones
ID: 36602418
I do have team viewer
0
 
LVL 1

Expert Comment

by:rockas1982
ID: 36602439
send it here [EMAIL ADDRESS REMOVED]
0
 
LVL 1

Author Comment

by:Jack_Jones
ID: 36602448
Sent ;)
0
 
LVL 1

Author Comment

by:Jack_Jones
ID: 36635087
Well toying around with it still, is it easier for it to find the false values?
0
 
LVL 1

Author Comment

by:Jack_Jones
ID: 36712144
No luck yet, anyone? Please lol!
0
 
LVL 1

Author Comment

by:Jack_Jones
ID: 36714024
Rockas1982 and I left off with this, and it don't generate anything.

Dim A As Integer
        Dim B As Integer

        Try
            For A = 0 To CompliantSkus.Columns(0).ListView.Items.Count
                For B = 0 To VehicleSkus.Columns(0).ListView.Items.Count
                    If VehicleSkus.Columns(0).ListView.Items(A).ToString = CompliantSkus.Columns(0).ListView.Items(A).ToString Then
                        foundskus.Items(A).SubItems.Add(VehicleSkus.Columns(0).ListView.Items(B).Text)
                        Exit For
                    End If
                Next B
            Next A
        Catch

        End Try

Open in new window

0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 36714533
Line 8 should just be
foundskus.Add(VehicleSkus.Columns(0).ListView.Items(B).Text)

You could also try removing line 9 just in case it's exiting both for loops.

Oh, and in the old version, to get rid of the warning, you should have just done Exp="" before starting the loops.
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 36714538
Also, either remove the try-catch or put something in the catch. If it's crashing, you won't know and also won't be told why because you don't handle your exceptions.
0
 
LVL 1

Author Comment

by:Jack_Jones
ID: 36716054
Tommy,

 Might be getting somewhere.

InvalidArgument=Value of '83' is not valid for 'index'.
Parameter name: index

Is generated error from;

 If VehicleSkus.Columns(0).ListView.Items(A).ToString = CompliantSkus.Columns(0).ListView.Items(A).ToString Then

Open in new window

0
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 36716404
The index in the line is not correct:
If VehicleSkus.Columns(0).ListView.Items(A).ToString = CompliantSkus.Columns(0).ListView.Items(A).ToString Then

A is for indexing CompliantSkus
B is for indexing VehicleSkus.

Try:
If VehicleSkus.Columns(0).ListView.Items(B).ToString = CompliantSkus.Columns(0).ListView.Items(A).ToString Then
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 36716487
Ack, can't believe I missed that. Thanks Shaun. That will fix the index problem (and the error).
0
 
LVL 1

Author Comment

by:Jack_Jones
ID: 36793486
Shaun,

InvalidArgument=Value of '147' is not valid for 'index'.
Parameter name: index

If VehicleSkus.Columns(0).ListView.Items(B).ToString = CompliantSkus.Columns(0).ListView.Items(A).ToString Then 

Open in new window

0
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 36814370
The index runs 0 to Count - 1.

Change these two lines:
            For A = 0 To CompliantSkus.Columns(0).ListView.Items.Count
                For B = 0 To VehicleSkus.Columns(0).ListView.Items.Count
TO:
            For A = 0 To CompliantSkus.Columns(0).ListView.Items.Count - 1
                For B = 0 To VehicleSkus.Columns(0).ListView.Items.Count - 1

An alternative is to use the For Each construct:
For Each A as ListItem in CompliantSkus.Columns(0).ListView.Items
   For Each B as ListItem in VehicleSkus.Columns(0).ListView.Items

Doing this, you have a ListItem object which can be easier to follow in code instead of using the index (IMHO).
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 36816615
I agree that the for each is easier since it's less likely to mess up.
0
 
LVL 1

Author Comment

by:Jack_Jones
ID: 36891912
Yeah im still working on this, but even if they are not equal it won't display the values...
0
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 36892524
Can you provide the code as you have it now?
0
 
LVL 1

Author Comment

by:Jack_Jones
ID: 36896915
Shaun pretty sick, ill try to get that to you asap just wanted to let you know im just relaxing today.
0
 
LVL 1

Author Comment

by:Jack_Jones
ID: 36914414
Shaun, sorry for the delay! No error message is generated, there are items that are = but not added to the listview.
0
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 36917225
Can you provide the code as you have it now?
0
 
LVL 1

Author Comment

by:Jack_Jones
ID: 36937268
Here ya go buddy.

Dim A As Integer
        Dim B As Integer

            For A = 0 To CompliantSkus.Columns(0).ListView.Items.Count - 1
                For B = 0 To VehicleSkus.Columns(0).ListView.Items.Count - 1
                    If VehicleSkus.Columns(0).ListView.Items(A).ToString = CompliantSkus.Columns(0).ListView.Items(A).ToString Then
                        foundskus.Items(A).SubItems.Add(VehicleSkus.Columns(0).ListView.Items(B).Text)
                        Exit For
                    End If
                Next B
            Next A

Open in new window

0
 
LVL 26

Accepted Solution

by:
Shaun Kline earned 2000 total points
ID: 36943073
This line is the culprit:
   If VehicleSkus.Columns(0).ListView.Items(A).ToString = CompliantSkus.Columns(0).ListView.Items(A).ToString Then
You are using the same index for both lists. This would only find items at the same position in both lists that are the same.

Try this:
   If VehicleSkus.Columns(0).ListView.Items(B).ToString = CompliantSkus.Columns(0).ListView.Items(A).ToString Then

Also, this line may cause you issues as you are using the index from the FOR loop for CompliantSkus
when adding items to foundskus:
   foundskus.Items(A).SubItems.Add(VehicleSkus.Columns(0).ListView.Items(B).Text)

You may be able to do this:
   foundskus.Items.Add(VehicleSkus.Columns(0).ListView.Items(B).Text)
0
 
LVL 1

Author Comment

by:Jack_Jones
ID: 36944907
awesome will try it out, let ya know once it's done!
0
 
LVL 1

Author Closing Comment

by:Jack_Jones
ID: 36965649
Cheers~
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Six Sigma Control Plans
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question