Rocky
Rocky

Reputation: 4524

How to combine 2 datatable and create a new datatable with that in Windows application?

I have 2 datatable DataTable1 and DataTable2, DataTable1 have the column as A,B,C,D and DataTable2 has the Column as P,Q,R,S I want to add both the datatable column and want to create a new datatable as A,B,C,D,P,Q,R,S

Some one please help me how to do that

Upvotes: 0

Views: 800

Answers (2)

Dane Balia
Dane Balia

Reputation: 5367

This is not practical, and bad design. Simply because you cannot take two different things and bring them together without "commonality" as the Stig mentioned in his comment.

Think about bring together 2 datatables, one has 5 rows and the other as 100. Unless there is something to join them by, I wouldn't suggest trying to do this. Alternatively if you must have functionality, then I suggest you iterate through the datatables building a new datatable consisting of Datable1 and DataTable2.

Below is sample of Join:

from table1 in dt1.AsEnumerable()
join table2 in dt2.AsEnumerable() on table1["Location"] equals table2["Location"]
select new
{
    Location = table1["Location"],
    Visa_Q1 = (int)table1["Visa_Q1"],
    Visa_Q2 = (int)table1["Visa_Q2"],
    Visa_Q3 = (int)table2["Visa_Q3"],
    Visa_Q4 = (int)table2["Visa_Q4"],
};

Upvotes: 0

Stig
Stig

Reputation: 1323

You can merge 2 tables like this

var dtA = new DataTable();
dtA.Columns.Add("A");
dtA.Columns.Add("B");
dtA.Columns.Add("C");

var dtB = new DataTable();
dtB.Columns.Add("D");
dtB.Columns.Add("E");
dtB.Columns.Add("F");

dtA.Merge(dtB);

Upvotes: 1

Related Questions