Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Compare two data table and highlight the mismatch

Posted on 2010-09-24
Medium Priority
Last Modified: 2013-11-26

I manage compare two data table, and bind the mismatch in a griview.
How to highlight the mismatch by cell in gridview using C# .net?

Pls refer Compare Two Data Table script:

Pls help.Thks.
public DataTable CompareDataTables(DataTable first, DataTable second)
        first.TableName = "FirstTable";
        second.TableName = "SecondTable";

        //Create Empty Table
        DataTable table = new DataTable("Difference");

            //Must use a Dataset to make use of a DataRelation object
            using (DataSet ds = new DataSet())
                //Add tables
                ds.Tables.AddRange(new DataTable[] { first.Copy(), second.Copy() });

                //Get Columns for DataRelation
                DataColumn[] firstcolumns = new DataColumn[ds.Tables[0].Columns.Count];

                for (int i = 0; i < firstcolumns.Length; i++)
                    firstcolumns[i] = ds.Tables[0].Columns[i];

                DataColumn[] secondcolumns = new DataColumn[ds.Tables[1].Columns.Count];

                for (int i = 0; i < secondcolumns.Length; i++)
                    secondcolumns[i] = ds.Tables[1].Columns[i];

                //Create DataRelation
                DataRelation r = new DataRelation(string.Empty, firstcolumns, secondcolumns, false);


                //Create columns for return table
                for (int i = 0; i < first.Columns.Count; i++)
                    table.Columns.Add(first.Columns[i].ColumnName, first.Columns[i].DataType);

                //If First Row not in Second, Add to return table.

                foreach (DataRow parentrow in ds.Tables[0].Rows)
                    DataRow[] childrows = parentrow.GetChildRows(r);
                    if (childrows == null || childrows.Length == 0)
                        table.LoadDataRow(parentrow.ItemArray, true);


        catch (Exception ex)
            throw ex;

        return table;

Open in new window

Question by:silterra
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
LVL 16

Expert Comment

ID: 33758194
Do these tables have parent\child relationship?

Author Comment

ID: 33759260

Author Comment

ID: 33759308
Attached data of Table A, Table B in worksheet Table A and Table B.
Worksheet Mismatch contains the mismatch data.
Table A is a base. I did comparison Table B against Table A based Step Name column in both Table A and Table B. How to highlight mismatch by cell in mismatch table?

GridView1.DataSource = CompareDataTables(Table B, Table A);
Pls help.


Accepted Solution

silterra earned 0 total points
ID: 33766414
I already find the solution.

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question