Add missing rows from one DataTable to another using C# and VB.Net in Windows Application

ryutenkan
 
on Mar 09, 2020 12:56 AM
Sample_185956.zip
1170 Views

Hi community,

I need your help.

How to add only missing rows from one table to another?

Data are from Access Datatbase with no primary key.

i tried this but with no success :

DB_DataSet = New DataSet()
DB_DataTable = New DataTable(DB_Challenge)
DB_DataTable2 = New DataTable(DBxl)

DB_DataSet.Tables.Add(DB_DataTable)
DB_DataSet.Tables.Add(DB_DataTable2)

Dim dt1 As DataTable = DB_DataSet.Tables(DB_Challenge)
Dim dt2 As DataTable = DB_DataSet.Tables(DBxl)


Dim common As List(Of DataRow) = (From r1 In dt1.AsEnumerable()
Join r2 In dt2.AsEnumerable() On r1("NOM") Equals r2("NOM")
Select r1).ToList()


For Each dr As DataRow In common
For i As Integer = dt2.Rows.Count - 1 To 0 Step -1
Dim drdiff As DataRow = dt1.Rows(i)
If dr("NOM").ToString() <> dt1.Rows(i)("NOM").ToString() Then
dt1.Rows.Add(drdiff)
End If
Next
dt1.AcceptChanges()
Next

DataGridView2.DataSource = dt1

Thanks

Download FREE API for Word, Excel and PDF in ASP.Net: Download
arunkurmi
 
on Mar 09, 2020 07:28 AM

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