Hi guys I'm here again asking for help

Can someone can gave me an idea about how to find all data in my first listview to another?

Two_Listview

Here is the picture.

You see their are a column name AM in my first Listview and in the second their are also AM

what i need to do is to find all AM in my first Listview to the second

how do i do that?

Please help me guys thanks in advance and merry christmas :)

Recommended Answers

All 13 Replies

the columns property of the listview should allow you to do what you want.

how to find all data in my first listview to another

Can you please explain what you mean by this?

commented: and I thought I was the only one who din not comprehend that +6

i have 2 excel files namely "Account.xls" and the other one is "Call and Visit.xls" all the data in "Account.xls" is go to listview1 and the "Call and Visit.xls" data go to the Listview2

in the picture i upload you will see they have both an "AM" Column Name..

I want to find all the "AM" in my Listview1 to the Listview2 any idea?

I want to find all the "AM" in my Listview1 to the Listview2

Still unclear. Do you mean you want to find all AM values from ListView1 that exist in ListView2 or do you want to find all AM values from ListView2 that exist in ListView1? Or do you want something else entirely?

Whatever it is that you want, how do you want to present the results?

i want to find all AM values from ListView1 that exist in ListView2

after finding all the AM in the listview1 to listview2 i will sum all their calls and visits by Month (January to December)

My take is you want to take the items in column 1 in listview1 and add them to column 1 in listview2, and that both columns show "AM". This code will do that:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        For I = 0 To ListView1.Items.Count - 1
            ListView2.Items.Add(ListView1.Items(I).Text)
        Next

    End Sub

A warning this code isn't generic to moving text from any column to another. This will only move item.text from listview.column(0) to listview.column(0)

No its not add..

i already have the data in Column1 in the listview2

if the **Column1 in Listview1** is exist in the **Column1 in Listview2** then
'execute the code

it is something related into this code but i try to figure it out.

Dim query As String
For Each item As ListViewItem In ListView1.Items
query = "Select * from [Calls and Visits$] where AM like '" & item.SubItems(1).Text & "%'"
'execute the query and do something with the results
Next

after finding all the AM in the listview1 to listview2 i will sum all their calls and visits by Month (January to December)

You already have that data in ListView2.

Yes i already have it but i want to do to find a specific "AM" from the listview1 to listview2

Maybe this is closer to what you want

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim NewItems As New List(Of ListViewItem)
        For I = 0 To ListView1.Items.Count - 1
            For J = 0 To ListView2.Items.Count - 1
                If ListView2.Items(J).Text = ListView1.Items(I).Text Then
                    NewItems.Add(ListView1.Items(I))
                End If
            Next
        Next
        MessageBox.Show(NewItems.Count)
    End Sub

This creates a list of items from listview1 that exist in listview2.

Just a guess, but I think Zick may be a user of google translate or something similar. Hence the odd wording.

Based on this:

Dim query As String
For Each item As ListViewItem In ListView1.Items
query = "Select * from [Calls and Visits$] where AM like '" & item.SubItems(1).Text & "%'"
'execute the query and do something with the results
Next

and this

after finding all the AM in the listview1 to listview2 i will sum all their calls and visits by Month (January to December)

I believe that he wants to search ListView2 for each "AM" entry in ListView1 and compute a yearly "Call" and "Visits" total per "AM" entry.

Perhaps Something Like this:

Private Sub Test()
   ' make some data
   Dim LV1Source As New DataTable
   Dim r As DataRow
   With LV1Source
      .Columns.Add("AM", GetType(String))
      .Columns.Add("Code Name", GetType(String))
      .Columns.Add("Department", GetType(String))
      .Columns.Add("Status", GetType(String))
      r = .NewRow : r(0) = "fred" : r(1) = "Something" : .Rows.Add(r)
      r = .NewRow : r(0) = "barney" : r(1) = "Something" : .Rows.Add(r)
   End With
   Dim LV2Source As New DataTable
   With LV2Source
      .Columns.Add("AM", GetType(String))
      .Columns.Add("Total Calls", GetType(Int32))
      .Columns.Add("Total Visits", GetType(Int32))
      .Columns.Add("Month", GetType(String))
      .Columns.Add("Year", GetType(Int32))
      r = .NewRow : r(0) = "fred" : r(1) = 10 : r(2) = 5 : r(3) = "January" : r(4) = 2011 : .Rows.Add(r)
      r = .NewRow : r(0) = "fred" : r(1) = 10 : r(2) = 5 : r(3) = "February" : r(4) = 2011 : .Rows.Add(r)
      r = .NewRow : r(0) = "fred" : r(1) = 10 : r(2) = 5 : r(3) = "March" : r(4) = 2011 : .Rows.Add(r)
      r = .NewRow : r(0) = "fred" : r(1) = 10 : r(2) = 5 : r(3) = "April" : r(4) = 2011 : .Rows.Add(r)

      r = .NewRow : r(0) = "fred" : r(1) = 5 : r(2) = 5 : r(3) = "January" : r(4) = 2012 : .Rows.Add(r)
      r = .NewRow : r(0) = "fred" : r(1) = 5 : r(2) = 5 : r(3) = "February" : r(4) = 2012 : .Rows.Add(r)
      r = .NewRow : r(0) = "fred" : r(1) = 5 : r(2) = 5 : r(3) = "March" : r(4) = 2012 : .Rows.Add(r)
      r = .NewRow : r(0) = "fred" : r(1) = 50 : r(2) = 15 : r(3) = "April" : r(4) = 2012 : .Rows.Add(r)

      r = .NewRow : r(0) = "barney" : r(1) = 25 : r(2) = 52 : r(3) = "January" : r(4) = 2012 : .Rows.Add(r)
      r = .NewRow : r(0) = "barney" : r(1) = 5 : r(2) = 5 : r(3) = "February" : r(4) = 2012 : .Rows.Add(r)
      r = .NewRow : r(0) = "barney" : r(1) = 5 : r(2) = 5 : r(3) = "March" : r(4) = 2012 : .Rows.Add(r)
      r = .NewRow : r(0) = "barney" : r(1) = 50 : r(2) = 15 : r(3) = "April" : r(4) = 2012 : .Rows.Add(r)
   End With

   Dim ListView1 As New ListView
   Dim ListView2 As New ListView
   'ListView1.DataBindings.Add("Items", LV1Source, "Rows", True)
   ' does not work
   ' oh well, do it the hard way

   For Each r In LV1Source.Rows
      ListView1.Items.Add(LVItemFromDR(r))
   Next
   For Each r In LV2Source.Rows
      ListView2.Items.Add(LVItemFromDR(r))
   Next

   ' ************ Done with data now the real code

   For Each item1 As ListViewItem In ListView1.Items
      Dim key As String = item1.SubItems(0).Text

      Dim tot As List(Of SummationItem) = _
                              (From item2 In ListView2.Items _
                              Where CType(item2, ListViewItem).SubItems(0).Text = key _
                              Group MonthlyTots = New With {.Calls = CInt(CType(item2, ListViewItem).SubItems(1).Text()), _
                                                     .Visits = CInt(CType(item2, ListViewItem).SubItems(2).Text())} _
                                 By Year = CInt(CType(item2, ListViewItem).SubItems(4).Text()) _
                              Into GroupedByYear = Group _
                              Select New SummationItem(Year, _
                                                       GroupedByYear.Sum(Function(MonthlyTots) MonthlyTots.Calls), _
                                                       GroupedByYear.Sum(Function(MonthlyTots) MonthlyTots.Visits)) _
                              ).ToList()

      Console.WriteLine(item1.SubItems(0).Text)
      For Each si As SummationItem In tot
         Console.WriteLine("      " & si.Year.ToString & "; Calls: " & si.TotalCalls.ToString & "     Visits: " & si.TotalVisits.ToString)
      Next si

   Next item1

End Sub

Public Class SummationItem ' helper class
   Public Year As Int32
   Public TotalCalls As Int32
   Public TotalVisits As Int32
   Public Sub New(ByVal Year As Int32, ByVal TotalCalls As Int32, ByVal TotalVisits As Int32)
      Me.Year = Year
      Me.TotalCalls = TotalCalls
      Me.TotalVisits = TotalVisits
   End Sub
End Class

' function to assist converting datarow to listviewitem
Private Function LVItemFromDR(ByVal r As DataRow) As ListViewItem
   Dim s(0 To r.ItemArray.Length - 1) As String
   For i As Int32 = 0 To r.ItemArray.Length - 1
      s(i) = r.ItemArray(i).ToString
   Next
   Return New ListViewItem(s)
End Function

Hi @TnTinMN thanks for the reply i already got the code for this thanks to @tinstaffl

your code work perfectly fine and i can use it for the future works :)
Thanks again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.