How to get difference between two DataTables in C#


Introduction : 
   Hi, Here i will Code you how to get the difference of two DataTables in C#,Here i have Shown in You three grid views  Difference between First and Second GridView will be placed in Third Grid.

What i have done Here :
   I have DataBase Table that contains the Name,Place and Country. first DataTble is Fetched to GridView1 as it is retrieved from Database. For second Datatable i have added all the Rows in the First Datatble and Added Extra two rows for 2nd DataTable. i used a Function to Separate and Get Difference. Difference wil be bind in GridView3.

ASPX Code :


<html>
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table>
    <tr>
    <td>
    <asp:GridView ID="GridView1" runat="server">
    </asp:GridView>
    </td>
    <td style="padding-left:20px">
    <asp:GridView ID="GridView2" runat="server">
    </asp:GridView>
    </td>
    </tr>
    <tr>
    <td align="center" colspan="2">
    <asp:GridView ID="GridView3" runat="server">
    </asp:GridView>
    </td>
    </tr>
    </table>
    </div>
    </form>
</body>
</html> 

Code Behind ( C# )

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace RDLCReport
{
    public partial class Page2 : System.Web.UI.Page
    {
      protected void Page_Load(object sender, EventArgs e)
      {
        SqlConnection con = new SqlConnection("Your Connection String");
        con.Open();
        SqlCommand cmd = new SqlCommand("select Name,Place,Country from Table1");
          using (con)
            {
                using (SqlDataAdapter da = new SqlDataAdapter())
                {
                    cmd.Connection = con;
                    da.SelectCommand = cmd;
                    DataTable dt1 = new DataTable();
                    da.Fill(dt1);
                    GridView1.DataSource = dt1;
                    GridView1.DataBind();
                    DataTable dt2 = new DataTable();
                    da.Fill(dt2);
                    dt2.Rows.Add( "Raji", "Kodambakam", "India");
                    dt2.Rows.Add("Kalai", "Tambaram", "India");
                    GridView2.DataSource = dt2;
                    GridView2.DataBind();
                    DataTable DiffTable = new DataTable();
                    DiffTable = getDifferentRecords(dt1, dt2);
                    GridView3.DataSource = DiffTable;
                    GridView3.DataBind();
                }
            }
        }
public DataTable getDifferentRecords(DataTable FirstDataTable, DataTable SecondDataTable)
   {

     //Create Empty Table 
       DataTable ResultDataTable = new DataTable("ResultDataTable");

    //use a Dataset to make use of a DataRelation object 
       using (DataSet ds = new DataSet())
       {
     
   //Add tables 
       ds.Tables.AddRange(new DataTable[] { FirstDataTable.Copy(), SecondDataTable.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 r1 = new DataRelation(string.Empty, firstColumns, secondColumns, false);
       ds.Relations.Add(r1);

       DataRelation r2 = new DataRelation(string.Empty, secondColumns, firstColumns, false);
       ds.Relations.Add(r2);

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

     //If FirstDataTable Row not in SecondDataTable, Add to ResultDataTable. 
         ResultDataTable.BeginLoadData();
        foreach (DataRow parentrow in ds.Tables[0].Rows)
          {
             DataRow[] childrows = parentrow.GetChildRows(r1);
             if (childrows == null || childrows.Length == 0)
             ResultDataTable.LoadDataRow(parentrow.ItemArray, true);
          }

     //If SecondDataTable Row not in FirstDataTable, Add to ResultDataTable. 
         foreach (DataRow parentrow in ds.Tables[1].Rows)
          {
           DataRow[] childrows = parentrow.GetChildRows(r2);
           if (childrows == null || childrows.Length == 0)
           ResultDataTable.LoadDataRow(parentrow.ItemArray, true);
           } ResultDataTable.EndLoadData();
         }
      return ResultDataTable;
        }
    }

}