Hi yogesjoshi,
Check this example. Now please take its reference and correct your code.
HTML
<table>
<tr>
<td>
<asp:GridView ID="GridView1" runat="server" />
</td>
<td>
<asp:GridView ID="GridView2" runat="server" />
</td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:GridView ID="GridView3" runat="server" />
</td>
</tr>
</table>
Namespaces
C#
using System.Data;
VB.Net
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt1 = new DataTable();
dt1.Columns.Add("Department");
dt1.Columns.Add("name");
dt1.Columns.Add("marks");
dt1.Rows.Add("it", "abc", 80);
dt1.Rows.Add("cs", "xyz", 67);
dt1.Rows.Add("mec", "abc", 76);
dt1.Rows.Add("ece", "xyz", 73);
DataTable dt2 = new DataTable();
dt2.Columns.Add("Department");
dt2.Columns.Add("name");
dt2.Columns.Add("marks");
dt2.Rows.Add("it", "abc", 80);
dt2.Rows.Add("cs", "xyz", 50);
dt2.Rows.Add("mec", "abc", 76);
dt2.Rows.Add("eee", "abc", 40);
DataTable dt3 = dt1.Clone();
dt3.Columns.Add("status");
for (int i = 0; i < dt1.Rows.Count; i++)
{
string department = dt1.Rows[i]["Department"].ToString();
string name = dt1.Rows[i]["name"].ToString();
string mark = dt1.Rows[i]["marks"].ToString();
DataRow[] dr = dt2.Select("Department='" + department + "' AND name='" + name + "'");
if (dr.Length > 0)
{
if (dr[0].ItemArray[2].ToString() == mark)
{
dt3.Rows.Add(department, name, mark, "same record");
}
else
{
dt3.Rows.Add(department, name, mark, "modified record");
}
}
else
{
dt3.Rows.Add(department, name, mark, "deleted record");
}
}
List<DataRow> rows = dt2.AsEnumerable()
.Where(x => !dt1.AsEnumerable()
.Select(y => y.Field<string>("Department"))
.Contains(x.Field<string>("Department"))).ToList();
for (int i = 0; i < rows.Count; i++)
{
dt3.Rows.Add(rows[i].ItemArray[0], rows[i].ItemArray[1], rows[i].ItemArray[2], "new record");
}
GridView1.DataSource = dt1;
GridView1.DataBind();
GridView2.DataSource = dt2;
GridView2.DataBind();
GridView3.DataSource = dt3;
GridView3.DataBind();
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim dt1 As DataTable = New DataTable()
dt1.Columns.Add("Department")
dt1.Columns.Add("name")
dt1.Columns.Add("marks")
dt1.Rows.Add("it", "abc", 80)
dt1.Rows.Add("cs", "xyz", 67)
dt1.Rows.Add("mec", "abc", 76)
dt1.Rows.Add("ece", "xyz", 73)
Dim dt2 As DataTable = New DataTable()
dt2.Columns.Add("Department")
dt2.Columns.Add("name")
dt2.Columns.Add("marks")
dt2.Rows.Add("it", "abc", 80)
dt2.Rows.Add("cs", "xyz", 50)
dt2.Rows.Add("mec", "abc", 76)
dt2.Rows.Add("eee", "abc", 40)
Dim dt3 As DataTable = dt1.Clone()
dt3.Columns.Add("status")
For i As Integer = 0 To dt1.Rows.Count - 1
Dim department As String = dt1.Rows(i)("Department").ToString()
Dim name As String = dt1.Rows(i)("name").ToString()
Dim mark As String = dt1.Rows(i)("marks").ToString()
Dim dr As DataRow() = dt2.[Select]("Department='" & department & "' AND name='" & name & "'")
If dr.Length > 0 Then
If dr(0).ItemArray(2).ToString() = mark Then
dt3.Rows.Add(department, name, mark, "same record")
Else
dt3.Rows.Add(department, name, mark, "modified record")
End If
Else
dt3.Rows.Add(department, name, mark, "deleted record")
End If
Next
Dim rows As List(Of DataRow) = dt2.AsEnumerable().Where(Function(x) Not dt1.AsEnumerable().[Select](Function(y) y.Field(Of String)("Department")).Contains(x.Field(Of String)("Department"))).ToList()
For i As Integer = 0 To rows.Count - 1
dt3.Rows.Add(rows(i).ItemArray(0), rows(i).ItemArray(1), rows(i).ItemArray(2), "new record")
Next
GridView1.DataSource = dt1
GridView1.DataBind()
GridView2.DataSource = dt2
GridView2.DataBind()
GridView3.DataSource = dt3
GridView3.DataBind()
End Sub
Screenshot