Hi ryutenkan,
Check this example. Now please take its reference and correct your code.
Code
C#
private void btnRemove_Click(object sender, EventArgs e)
{
// Get DataTables from Access Database.
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(3, "Suzanne Mathews", "France");
dt1.Rows.Add(4, "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(1, "John Hammond", "United States");
dt1.Rows.Add(2, "Mudassar Khan", "India");
dt2.Rows.Add(3, "Suzanne Mathews", "France");
dt2.Rows.Add(5, "Robert Schidner", "Russia");
List<DataRow> common = (from r1 in dt1.AsEnumerable()
join r2 in dt2.AsEnumerable() on r1["Id"] equals r2["Id"]
select r1).ToList();
foreach (DataRow dr in common)
{
for (int i = dt2.Rows.Count - 1; i >= 0; i--)
{
DataRow drDelete = dt2.Rows[i];
if (dr["Id"].ToString() == dt2.Rows[i]["Id"].ToString()
&& dr["Name"].ToString() == dt2.Rows[i]["Name"].ToString()
&& dr["Country"].ToString() == dt2.Rows[i]["Country"].ToString())
{
drDelete.Delete();
}
}
dt2.AcceptChanges();
}
dataGridView1.DataSource = dt2;
}
VB.Net
Private Sub btnRemove_Click(sender As System.Object, e As System.EventArgs) Handles btnRemove.Click
' Get DataTables from Access Database.
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(3, "Suzanne Mathews", "France")
dt1.Rows.Add(4, "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(1, "John Hammond", "United States")
dt1.Rows.Add(2, "Mudassar Khan", "India")
dt2.Rows.Add(3, "Suzanne Mathews", "France")
dt2.Rows.Add(5, "Robert Schidner", "Russia")
Dim common As List(Of DataRow) = (From r1 In dt1.AsEnumerable() _
Join r2 In dt2.AsEnumerable() On r1("Id") Equals r2("Id") _
Select r1).ToList()
For Each dr As DataRow In common
For i As Integer = dt2.Rows.Count - 1 To 0 Step -1
Dim drDelete As DataRow = dt2.Rows(i)
If dr("Id").ToString() = dt2.Rows(i)("Id").ToString() _
AndAlso dr("Name").ToString() = dt2.Rows(i)("Name").ToString() _
AndAlso dr("Country").ToString() = dt2.Rows(i)("Country").ToString() Then
drDelete.Delete()
End If
Next
dt2.AcceptChanges()
Next
dataGridView1.DataSource = dt2
End Sub
Screenshot