Go Premium for a chance to win a PS4. Enter to Win

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 7523
  • Last Modified:

Compare 2 DataTables

I will have 2 populated data tables:  


Here is the interface of the method that I would like to see a working example of:
Public Sub(byval Dt1 as datatable, byval Dt2 as datatable, byref DtInBoth as datatable, byRef DtOnlyInFirst as DataTable)

(I know, this is probably really simple to accomplish, but I am new to .NET...)

Definition of the fields being passed in:
Dt1 --> First data table to compare
Dt2 --> Second data table to compare
DtInBoth -->  Return datatable that contains only the rows found in both datatables
DtOnlyInFirst -->  Return datatable that contains the records that were only found in the first datatable

I would prefer an example in VB.NET, but will accept C# as well.
1 Solution
Are the two tables have same schema?
Do both tables have any relations to each other?
Are you loking for comparing each and every field in both tables?

What is your comparison criteria?

If you could provide some details .. may be there is an easy way?
tnewc59Author Commented:
Sure.  Here are some more details:

The tables 'should' have the same schema.  At a minumum, they will have the same primary key field.  

The tables will both be dynamically created, but the following is an example of what will typically occur:

Dt1 would have the following rows:
1001          'TEST'         'ANOTHER TEST'
1002          'HELLO'        'DESCRIPTION OF HELLO'

Dt2 would have the following:
1001          'TEST'       'ANOTHER TEST'

The return tables should be filled as follows:
1001           'TEST'           'ANOTHER TEST'

1002           'HELLO'          'DESCRIPTION OF HELLO'

In reality, I will need to compare the schemas as well, but I was hoping to see an example of the above comparisions to get me started down the right path.  The code that I really need is a bit more complex, but I think I can figure it out once I see a 'seed' idea.  If the solution I accept has the ability to gracefully handle small schema differences (other than the pk field being different as that will not occur) I will up the points.  
you can compare rows and columns of the datatables..

I dont think there is a standard compare function but a simple row sweep would go something like this(but only if the schema was the same

Private Sub X(ByVal Dt1 As DataTable, ByVal Dt2 As DataTable, ByRef DtInBoth As DataTable, ByRef DtOnlyInFirst As DataTable)
    Dim RowX As DataRow
    DtInBoth = New DataTable()
    DtOnlyInFirst = New DataTable()
    Dim foundrow As DataRow
    For Each rowx In dt1.rows
        foundrow = Dt2.Rows.Find(RowX.Item("PK_FIELD1"))
        If foundrow Is Nothing Then
        End If
End Sub


Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now