The following will compare two datatables and return the differences. These datatables only have two columns each. Like many of you I had attempted to use the 'merge' method and 'getchanges', but that does not work for the purpose of simply comparing two datatables. I dumped the differences into a DataGridView:
Could probably be done cleaner, but I hope this helps someone else.Code:'Datatables are defined in the public space and filled with separate code: Dim sdt As New DataTable Dim pnpdt As New DataTable Dim xdt As New DataTable ''''''' Dim xrow As DataRow For Each row As DataRow In sdt.Rows Dim sdtval As String = row.Item("RefDes") Dim Mfr As String = row.Item("Mfr") If pnpdt.Rows.Contains(sdtval) = False Then xrow = xdt.NewRow xrow("RefDes") = sdtval xrow("Mfr") = Mfr xrow("Notes") = sdtval & " - Found in sdt, but not found in pnpdt." xdt.Rows.Add(xrow) End If Next Dim yrow As DataRow For Each rowy As DataRow In pnpdt.Rows Dim pnpval As String = rowy.Item("RefDes") Dim Mfr As String = rowy.Item("Mfr") If sdt.Rows.Contains(pnpval) = False Then yrow = xdt.NewRow yrow("RefDes") = pnpval yrow("Mfr") = Mfr yrow("Notes") = pnpval & " - Found in pnpdt, but not found in sdt." xdt.Rows.Add(yrow) End If Next Dim zrow As DataRow For Each rowz As DataRow In sdt.Rows Dim pnpval As String = rowz.Item("RefDes") 'SMT Ref Des If pnpdt.Rows.Contains(pnpval) = True Then Dim BMfr As String = rowz.Item("Mfr") Dim Prow As DataRow = (From column In pnpdt.Rows Where column("RefDes") = pnpval).First Dim PMfr As String = Prow.Item("Mfr") If BMfr <> PMfr Then zrow = xdt.NewRow zrow("RefDes") = pnpval zrow("Mfr") = "MISMATCH" zrow("Notes") = pnpval & " - (" & BMfr & ") does not match (" & PMfr & ")." xdt.Rows.Add(zrow) End If End If Next DataGridView1.DataSource = xdt Dim Cnotes As DataGridViewColumn = Me.DataGridView1.Columns("Notes") Cnotes.Width = 500




Reply With Quote
