Search code examples
c#mysqldatatablecloneresultset

How to combine two or more DataTables dynamically in c#


I have two DataTables being filled.

DT1 and DT2

Each DataTable has the same column headers. However, DT2 may or may not have the same amount of rows.

ID | Type | Value

I need the new Table to add Rows as necessary based on the number of results that are returned in the "Type" column and set DT3 rows ID = DT1.ID and Value to "N/A"

       DT1                        DT2                             DT3
ID | Type   | Value        ID | Type   | Value        ID | Type   | Value
1   ItemCost   5000        27  ItemCost   3800        27  ItemCost   3800
2   TravCost   5700        28  TravCost   4851        28  TravCost   4851
3   UpCharge   3600                                   3   UpCharge   N/A
4   TaxCost    7000                                   4   TaxCost    N/A

Solution

  • Here is my code for this issue:

                DataTable dt1 = new DataTable();
                dt1.Columns.Add("ID");
                dt1.Columns.Add("Type");
                dt1.Columns.Add("Value");
                dt1.Rows.Add(new Object[] { "1", "ItemCost", "5000" });
                dt1.Rows.Add(new Object[] { "2", "TravCost", "5700" });
                dt1.Rows.Add(new Object[] { "3", "UpCharge", "3600" });
                dt1.Rows.Add(new Object[] { "4", "TaxCost", "7000" });
                DataTable dt2 = new DataTable();
                dt2.Columns.Add("ID");
                dt2.Columns.Add("Type");
                dt2.Columns.Add("Value");
                dt2.Rows.Add(new Object[] { "27", "ItemCost", "3800" });
                dt2.Rows.Add(new Object[] { "28", "TravCost", "4851" });
                DataTable dt3 = new DataTable();
                dt3 = dt2.Clone();
                foreach (DataRow item in dt2.Rows)
                {
                    dt3.Rows.Add(new object[] { item["ID"], item["Type"], item["Value"] });
                }
                foreach (DataRow item in dt1.Rows)
                {
                    DataRow[] drs = dt3.Select("Type='" + item["Type"].ToString() + "'");
                    if (drs.Count() == 0)
                    {
                        dt3.Rows.Add(new object[] { item["ID"], item["Type"], "N/A" });
                    }
                }