tags:

views:

310

answers:

1

Hi,

I have two datatables.There are few identical columns in both of them.Now I need to compare each Identical column's cell in both the datatables and build a third datatable by merging the changes of identical column's cells and also the un-identical columns.Please help me with C# code in doing it.

Thanks, Vix

+1  A: 

It can be done using the Merge() method of DataTable.

The merge method compares the key columns and merge the rows into one table.

// Set the identical columns to compare by in first table
table1.PrimaryKey = new DataColumn[]
                        { idColumnOfTable1, anotherIDColumnOfTable1 };
// Set the identical columns to compare by in second table
table2.PrimaryKey = new DataColumn[]
                        { idColumnOfTable2, anotherIDColumnOfTable2 };

// The MissingSchemaAction.Add will add the non-identical columns
// Non-identical columns existing in from table 2 will be added to table1
table1.Merge(table2, false, MissingSchemaAction.Add);
Elisha
Can you please assist me with the code.How can it be done for a particular number of columns ?
Vix
I mean for each specific column.
Vix
Sure, updated answer. Hope it helps.
Elisha
Thanks.It did help.
Vix