iGundersen
iGundersen

Reputation: 63

Find duplicats and display in new datagridview

I have two datagridviews:

DataGridView1: This is generated from a csv file with this code:

For Each line As String In System.IO.File.ReadAllLines("C:\path\test.csv")
                Form1.DataGriView1.Rows.Add(line.Split(";"))
 Next

The.csv file has this format:

12345;SOME TEXT;2000000;12345678901;2014-07-31;

23456;SOME TEXT;2000000;10987654321;2014-07-11;

DataGridView2 This is generated from an excel file with this code:

 Dim MyConnection As System.Data.OleDb.OleDbConnection
            Dim DtSet As System.Data.DataSet
            Dim MyCommand As System.Data.OleDb.OleDbDataAdapter

            MyConnection = New System.Data.OleDb.OleDbConnection _
                ("provider=Microsoft.Jet.OLEDB.4.0; Data Source='C:\Users\path\excel.xls'; Extended Properties=Excel 8.0;")
            MyCommand = New System.Data.OleDb.OleDbDataAdapter _
                    ("select * from [Sheet$]", MyConnection)
            DtSet = New System.Data.DataSet
            MyCommand.Fill(DtSet)
            Form1.DataGridView2.DataSource = DtSet.Tables(0)
            MyConnection.Close()

The Excel document has 6 columns, with headers.

This is how the datagridview are named

The question:

I need to find duplicates and display the data in a third gridview. Specifically, I want to see if the value in column column1 in DataGridView1 are in column polnr in DataGridView2. If it does, I want the entire line from DataGridView copied to DataGridView3

Yes, I have tried it myself and searched through the whole internet. Can someone help me?

Upvotes: 0

Views: 117

Answers (1)

Nocturnal
Nocturnal

Reputation: 386

this should work with dgv 1 + 3 being unbound

    Dim rowlist As New ArrayList
    Dim dgv3row As New DataGridViewRow

    For Each dgv1row As DataGridViewRow In DataGridView1.Rows
        For Each dgv2row As DataGridViewRow In DataGridView2.Rows

            If dgv1row.Cells("Column1").Value = dgv2row.Cells("polnr").Value Then

                For Each dgvcell As DataGridViewCell In dgv1row.Cells
                    rowlist.Add(dgvcell.Value)
                Next

                If rowlist.Count > 0 Then
                    Dim dgv3rowindex As Integer = DataGridView3.Rows.Add()

                    dgv3row = DataGridView3.Rows(dgv3rowindex)

                    For Each dgv3cell As DataGridViewCell In dgv3row.Cells
                        dgv3cell.Value = rowlist(dgv3cell.ColumnIndex)
                    Next

                    rowlist.Clear()
                End If
            End If
        Next
    Next

Upvotes: 1

Related Questions