Solved

Compare two data table and highlight the mismatch

Posted on 2010-09-24
4
2,300 Views
Last Modified: 2013-11-26
Hi,

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");



        try

        {

            //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);



                ds.Relations.Add(r);



                //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.

                table.BeginLoadData();



                foreach (DataRow parentrow in ds.Tables[0].Rows)

                {

                    DataRow[] childrows = parentrow.GetChildRows(r);

                    if (childrows == null || childrows.Length == 0)

                        table.LoadDataRow(parentrow.ItemArray, true);

                }



                table.EndLoadData();



            }

        }

        catch (Exception ex)

        {

            throw ex;

        }



        return table;

    }

Open in new window

0
Comment
Question by:silterra
  • 3
4 Comments
 
LVL 16

Expert Comment

by:SriVaddadi
Comment Utility
Do these tables have parent\child relationship?
0
 

Author Comment

by:silterra
Comment Utility
Hi,
No.
0
 

Author Comment

by:silterra
Comment Utility
hi,
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);
GridView1.DataBind();
Pls help.

ST02-000001-ex.xls
0
 

Accepted Solution

by:
silterra earned 0 total points
Comment Utility
hi,
I already find the solution.
Thks.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now