Merging or adding 2 different DataTables into Single DataTable
-
Hi All, I want to merge 2 tables. These 2 tables have different columns but the no.of records same.
DataTable dt1, DataTable dt2;
dt1: contains records like below ===============================
colA colB colC
a1 b1 c1
a2 b2 c2
a3 b3 c3dt2: contains records like below ===============================
colD colE colF
d1 e1 f1
d2 e2 f2
d3 e3 f3I have to make these two table into one single table like below
Required DataTable
colA colB colC colD colE colF
a1 b1 c1 d1 e1 f1
a2 b2 c2 d2 e2 f2
a3 b3 c3 d3 e3 f3How can we do that? I approached like, taken a DataTable and added columns of dt1 and dt2 the this table. then added the records to main DataTable through looping each record of dt1 and dt2. Is there any other way do this with out using loop statements. Both the tables are different, they don't have any relation between them, only the no.of records were same. Please suggest me how to this. Thanks in advance.
-
Hi All, I want to merge 2 tables. These 2 tables have different columns but the no.of records same.
DataTable dt1, DataTable dt2;
dt1: contains records like below ===============================
colA colB colC
a1 b1 c1
a2 b2 c2
a3 b3 c3dt2: contains records like below ===============================
colD colE colF
d1 e1 f1
d2 e2 f2
d3 e3 f3I have to make these two table into one single table like below
Required DataTable
colA colB colC colD colE colF
a1 b1 c1 d1 e1 f1
a2 b2 c2 d2 e2 f2
a3 b3 c3 d3 e3 f3How can we do that? I approached like, taken a DataTable and added columns of dt1 and dt2 the this table. then added the records to main DataTable through looping each record of dt1 and dt2. Is there any other way do this with out using loop statements. Both the tables are different, they don't have any relation between them, only the no.of records were same. Please suggest me how to this. Thanks in advance.
There's two way to do this without using a loop. The best way is to have your database server get you the data in the fashion you want using a SELECT query. An alternative is to use LIST to do it. In either case, hopefully, you have key relationships between these two tables, right?
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008
But no longer in 2009... -
There's two way to do this without using a loop. The best way is to have your database server get you the data in the fashion you want using a SELECT query. An alternative is to use LIST to do it. In either case, hopefully, you have key relationships between these two tables, right?
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008
But no longer in 2009...Hi thanks for your response.
But as i told in my post, there is no relation ship between them. Just they are totally 2 different tables, Only the no.of records were same.
I can't get the data from database as you told, bcz no key relation ship between them.
Any alternative.
-
Hi thanks for your response.
But as i told in my post, there is no relation ship between them. Just they are totally 2 different tables, Only the no.of records were same.
I can't get the data from database as you told, bcz no key relation ship between them.
Any alternative.
Then you have no choice but to loop through them and build it yourself. You'll have to take care to make sure the records are matched up the way you want them. If all you did was retrieve the data with something like:
SELECT * FROM table
with no sorting information, you can get the records without an guarantee of the order of them, from both tables. It's up to you to match the records up yourself.A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008
But no longer in 2009...