2

I have two identical DataTables from two different databases (but the ids are unique!). So now I want to have combine this data into a single DataTable. I have no idea what to do.

I tried the following:

DataTable one = new DataTable();
one = baza_win.pobierz_dane("SELECT...");
DataTable two = new DataTable();
two = baza_win2.pobierz_dane("SELECT...");
//DataTable sum = one + two;
DataTable sum = new DataTable();
sum.Clone(one);
sum.Merge(two,false);

But this doesn't work at sum.Clone(one);

Any ideas?

asteri
  • 11,402
  • 13
  • 60
  • 84
user2614557
  • 23
  • 1
  • 1
  • 3

4 Answers4

4
sum = one.Copy();
sum.Merge(two);
Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107
Prasad
  • 220
  • 3
  • 9
0

I think you need Copy instead of Clone:

DataTable one = new DataTable();
one = baza_win.pobierz_dane("SELECT...");
DataTable two = new DataTable();
two = baza_win2.pobierz_dane("SELECT...");
//DataTable sum = one + two;
DataTable sum = new DataTable();
sum.Copy(one);
sum.Merge(two,false);
Giannis Paraskevopoulos
  • 18,261
  • 1
  • 49
  • 69
0

copied from link
http://msdn.microsoft.com/en-us/library/fk68ew7b.aspx

duplicate of how to join two datatable datas into one datatable to show in one gridview in asp.net

private static void DemonstrateMergeTable() { DataTable table1 = new DataTable("Items");

    // Add columns
    DataColumn column1 = new DataColumn("id", typeof(System.Int32));
    DataColumn column2 = new DataColumn("item", typeof(System.Int32));
    table1.Columns.Add(column1);
    table1.Columns.Add(column2);

    // Set the primary key column.
    table1.PrimaryKey = new DataColumn[] { column1 };

    // Add RowChanged event handler for the table.
    table1.RowChanged += 
        new System.Data.DataRowChangeEventHandler(Row_Changed);

    // Add some rows.
    DataRow row;
    for (int i = 0; i <= 3; i++)
    {
        row = table1.NewRow();
        row["id"] = i;
        row["item"] = i;
        table1.Rows.Add(row);
    }

    // Accept changes.
    table1.AcceptChanges();
    PrintValues(table1, "Original values");

    // Create a second DataTable identical to the first.
    DataTable table2 = table1.Clone();

    // Add three rows. Note that the id column can't be the  
    // same as existing rows in the original table.
    row = table2.NewRow();
    row["id"] = 14;
    row["item"] = 774;
    table2.Rows.Add(row);

    row = table2.NewRow();
    row["id"] = 12;
    row["item"] = 555;
    table2.Rows.Add(row);

    row = table2.NewRow();
    row["id"] = 13;
    row["item"] = 665;
    table2.Rows.Add(row);

    // Merge table2 into the table1.
    Console.WriteLine("Merging");
    table1.Merge(table2);
    PrintValues(table1, "Merged With table1");

}

private static void Row_Changed(object sender, 
    DataRowChangeEventArgs e)
{
    Console.WriteLine("Row changed {0}\t{1}", 
        e.Action, e.Row.ItemArray[0]);
}

private static void PrintValues(DataTable table, string label)
{
    // Display the values in the supplied DataTable:
    Console.WriteLine(label);
    foreach (DataRow row in table.Rows)
    {
        foreach (DataColumn col in table.Columns)
        {
            Console.Write("\t " + row[col].ToString());
        }
        Console.WriteLine();
    }
}
Community
  • 1
  • 1
Md. Parvez Alam
  • 4,326
  • 5
  • 48
  • 108
0

try the following... CLONE the first table so it has proper structure, THEN merge two into it.

DataTable sum = one.Clone()
sum.Merge(two,false);
DRapp
  • 47,638
  • 12
  • 72
  • 142