Hi paulrajmca,
Check this example. Now please take its reference and correct your code.
HTML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="UserId" HeaderText="UserId" />
<asp:BoundField DataField="UserName" HeaderText="UserName" />
</Columns>
</asp:GridView>
<br />
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="UserId" HeaderText="UserId" />
<asp:BoundField DataField="UserName" HeaderText="UserName" />
</Columns>
</asp:GridView>
<br />
<asp:GridView ID="GridView3" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="UserId" HeaderText="UserId" />
<asp:BoundField DataField="UserName" HeaderText="UserName" />
</Columns>
</asp:GridView>
Namespaces
C#
using System.Data;
VB.Net
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt1 = new DataTable();
dt1.Columns.AddRange(new DataColumn[2] { new DataColumn("UserId"), new DataColumn("UserName") });
dt1.Rows.Add("01", "Test Series 01");
dt1.Rows.Add("02", "Test Series 02");
dt1.Rows.Add("03", "Test Series 03");
dt1.Rows.Add("04", "Test Series 04");
this.GridView1.DataSource = dt1;
this.GridView1.DataBind();
DataTable dt2 = new DataTable();
dt2.Columns.AddRange(new DataColumn[2] { new DataColumn("UserId"), new DataColumn("UserName") });
dt2.Rows.Add("01", "Test Series 01");
dt2.Rows.Add("02", "Test Series 08");
dt2.Rows.Add("03", "Test Series 03");
this.GridView2.DataSource = dt2;
this.GridView2.DataBind();
DataTable dt3 = dt1.Copy();
foreach (DataRow row1 in dt2.Rows)
{
bool isRecordExist = false;
foreach (DataRow row2 in dt1.Rows)
{
if (row1["UserId"] == row2["UserId"] && row1["UserName"] == row2["UserName"])
{
isRecordExist = true;
}
}
if (!isRecordExist)
{
dt3.ImportRow(row1);
}
}
this.GridView3.DataSource = dt3;
this.GridView3.DataBind();
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim dt1 As DataTable = New DataTable()
dt1.Columns.AddRange(New DataColumn(1) {New DataColumn("UserId"), New DataColumn("UserName")})
dt1.Rows.Add("01", "Test Series 01")
dt1.Rows.Add("02", "Test Series 02")
dt1.Rows.Add("03", "Test Series 03")
dt1.Rows.Add("04", "Test Series 04")
Me.GridView1.DataSource = dt1
Me.GridView1.DataBind()
Dim dt2 As DataTable = New DataTable()
dt2.Columns.AddRange(New DataColumn(1) {New DataColumn("UserId"), New DataColumn("UserName")})
dt2.Rows.Add("01", "Test Series 01")
dt2.Rows.Add("02", "Test Series 08")
dt2.Rows.Add("03", "Test Series 03")
Me.GridView2.DataSource = dt2
Me.GridView2.DataBind()
Dim dt3 As DataTable = dt1.Copy()
For Each row1 As DataRow In dt2.Rows
Dim isRecordExist As Boolean = False
For Each row2 As DataRow In dt1.Rows
If row1("UserId") = row2("UserId") AndAlso row1("UserName") = row2("UserName") Then
isRecordExist = True
End If
Next
If Not isRecordExist Then
dt3.ImportRow(row1)
End If
Next
Me.GridView3.DataSource = dt3
Me.GridView3.DataBind()
End If
End Sub
Screenshot