-6

I have two datatables

dtTable1:
Name    Id  Contact
Gems    A1  8888880000
George  A2  0000008888

dtTable2:
Col1    Col2    Col3
XXX1    YYY1    ZZZ1
XXX2    YYY2    ZZZ2
XXX3    YYY3    ZZZ3
XXX4    YYY4    ZZZ4

My required DataTable is:

Name    Id  Contact         Col1    Col2    Col3
Gems    A1  8888880000  XXX1    YYY1    ZZZ1
George  A2  0000008888  XXX2    YYY2    ZZZ2
null    null   null     XXX3    YYY3    ZZZ3
null    null   null     XXX4    YYY4    ZZZ4

How can I do it with C#

nitu
  • 3
  • 4
  • There's not a framework function to accomplish this, so you'll need to come up with a process. e.g. Loop through rows and add to a new datatable. – D Stanley Jun 19 '14 at 19:09
  • Are you talking about `System.Data.DataTable`? Your “required Datatable” is not a `System.Data.DataTable`. – Dour High Arch Jun 19 '14 at 19:09
  • http://stackoverflow.com/questions/13156626/join-2-datatables-with-many-columns –  Jun 19 '14 at 19:12
  • All the three Tables are System.Data.DataTable. first two tables do not have any common column. – nitu Jun 19 '14 at 19:28

3 Answers3

0
DataTable mergeDataTables(DataTable dt1, DataTable dt2)
{
    if(dt1.Rows.Count != dt2.Rows.Count ) throw new Exception();

    var dtResult = new DataTable();

    //add new columns
    foreach (DataColumn col in dt1.Columns)
        dtResult.Columns.Add(col.ColumnName, col.DataType);

    foreach (DataColumn col in dt2.Columns)
        dtResult.Columns.Add(col.ColumnName, col.DataType);

    //fill data
    for (int i = 0; i < dt1.Rows.Count; i++)
    {
        dtResult.Rows.Add(dt1.Rows[i].ItemArray.Concat(dt2.Rows[i].ItemArray).ToArray());
    }

    return dtResult;
}
dovid
  • 6,354
  • 3
  • 33
  • 73
  • With this solution, I can Create the Column headers only. But I have different number of rows in the DataTables. if(dt1.Rows.Count != dt2.Rows.Count ) throw new Exception(); it throws exception at the start. – nitu Jun 19 '14 at 19:46
0

Here you go. Might not be pretty but does the job. No matter which DataTable has more row it will add them.

private static DataTable MergeTables(DataTable dt1, DataTable dt2)
{
    DataTable merged = new DataTable();

    //copy column struct from dt1
    merged = dt1.Clone();

    //create columns from dt2
    foreach (DataColumn col in dt2.Columns)
    {
        merged.Columns.Add(col.ColumnName);
    }

    int rows;

    if (dt1.Rows.Count > dt2.Rows.Count)
    {
        rows = dt1.Rows.Count;
    }
    else
    {
        rows = dt2.Rows.Count;
    }

    for (int i = 0; i < rows; i++)
    {
        DataRow row = merged.NewRow();

        if ( i < dt1.Rows.Count)
        {
            for (int c = 0; c < dt1.Columns.Count; c++)
            {
                row[c] = dt1.Rows[i][c];
            }
        }

        if (i < dt2.Rows.Count)
        {
            for (int c2 = dt1.Columns.Count; c2 < dt2.Columns.Count + dt1.Columns.Count; c2++)
            {
                row[c2] = dt2.Rows[i][c2-dt1.Columns.Count];
            }
        }
        merged.Rows.Add(row);
    }
    return merged;
}
Tsukasa
  • 6,342
  • 16
  • 64
  • 96
0

I have changed solution of lomed.

May be this not perfect solution. But it works for the similar scenario as required in the question:

DataTable MergeDataTables(DataTable dt1, DataTable dt2)
    {
        var dtResult = new DataTable();

        foreach (DataColumn col in dt1.Columns)
            dtResult.Columns.Add(col.ColumnName, col.DataType);

        foreach (DataColumn col in dt2.Columns)
            dtResult.Columns.Add(col.ColumnName, col.DataType);

        //cond: to check which if datatable1 is bigger
        if (dt1.Rows.Count > dt2.Rows.Count)
        {               
            for (int i = 0; i < dt1.Rows.Count; i++)
            {
                if (i < dt2.Rows.Count)
                    dtResult.Rows.Add(dt1.Rows[i].ItemArray.Concat(dt2.Rows[i].ItemArray).ToArray());
                else
                {
                    DataRow dr = dtResult.NewRow();
                    foreach (DataColumn col in dt1.Columns)                       
                        dr[col.ColumnName] = dt1.Rows[i][col.ColumnName];
                    dtResult.Rows.Add(dr);
                }                

            }
        }
        //if rows equal or datatable2 is bigger
        else
        {                
            for (int i = 0; i < dt2.Rows.Count; i++)
            {
                if (i < dt1.Rows.Count)
                    dtResult.Rows.Add(dt1.Rows[i].ItemArray.Concat(dt2.Rows[i].ItemArray).ToArray());
                else
                {
                    DataRow dr = dtResult.NewRow();
                    foreach (DataColumn col in dt2.Columns)                                                  
                        dr[col.ColumnName] = dt2.Rows[i][col.ColumnName];
                    dtResult.Rows.Add(dr);
                }
            }                
        }

       return dtResult;
    } 
Hassan
  • 5,360
  • 2
  • 22
  • 35