Hi ryutenkan,
Check this sample. now take its reference and correct your code.
Namespaces
C#
using System.Data;
VB.Net
Imports System.Data
Code
C#
private void Form1_Load(object sender, EventArgs e)
{
DataTable dt1 = new DataTable();
dt1.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
new DataColumn("Name", typeof(string)),
new DataColumn("Country",typeof(string)) });
dt1.Rows.Add(1, "John Hammond", "United States");
dt1.Rows.Add(2, "Mudassar Khan", "India");
dt1.Rows.Add(3, "Robert Schidner", "Russia");
DataTable dt2 = new DataTable();
dt2.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
new DataColumn("Name", typeof(string)),
new DataColumn("Country",typeof(string)) });
dt2.Rows.Add(2, "Mudassar Khan", "India");
dt2.Rows.Add(4, "Suzanne Mathews", "France");
dt2.Rows.Add(3, "Robert Schidner", "Russia");
DataTable dt3 = dt2.Copy();
foreach (DataRow row1 in dt1.Rows)
{
foreach (DataRow row2 in dt2.Rows)
{
if (row1["Id"].ToString() != row2["Id"].ToString())
{
dt3.ImportRow(row1);
}
}
}
DataView view = new DataView(dt3);
DataTable distinctValues = view.ToTable(true, "Id", "Name", "Country");
this.dataGridView1.DataSource = distinctValues;
}
VB.Net
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim dt1 As DataTable = New DataTable()
dt1.Columns.AddRange(New DataColumn(2) {New DataColumn("Id", GetType(Integer)), New DataColumn("Name", GetType(String)), New DataColumn("Country", GetType(String))})
dt1.Rows.Add(1, "John Hammond", "United States")
dt1.Rows.Add(2, "Mudassar Khan", "India")
dt1.Rows.Add(3, "Robert Schidner", "Russia")
Dim dt2 As DataTable = New DataTable()
dt2.Columns.AddRange(New DataColumn(2) {New DataColumn("Id", GetType(Integer)), New DataColumn("Name", GetType(String)), New DataColumn("Country", GetType(String))})
dt2.Rows.Add(2, "Mudassar Khan", "India")
dt2.Rows.Add(4, "Suzanne Mathews", "France")
dt2.Rows.Add(3, "Robert Schidner", "Russia")
Dim dt3 As DataTable = dt2.Copy()
For Each row1 As DataRow In dt1.Rows
For Each row2 As DataRow In dt2.Rows
If row1("Id").ToString() <> row2("Id").ToString() Then
dt3.ImportRow(row1)
End If
Next
Next
Dim view As DataView = New DataView(dt3)
Dim distinctValues As DataTable = view.ToTable(True, "Id", "Name", "Country")
Me.dataGridView1.DataSource = distinctValues
End Sub