Sandip's Programming Zen

An attempt to share tech/coding experiences

Merge two DataTable in .Net

with 4 comments

Recently I had a need to merge two DataTables with same structure, DataSet has built-in function called Merge but not in DataTable.
So I wrote my own, this is my function to merge two DataTables…

public static DataTable mergeDTs(DataTable dt1, DataTable dt2)
{
DataTable dtResult = dt1.Clone();

foreach (DataRow dr in dt1.Rows)
{
dtResult.Rows.Add(dr.ItemArray);
}
foreach (DataRow dr in dt2.Rows)
{
dtResult.Rows.Add(dr.ItemArray);
}

return dtResult;
}

Hope this helps.

Advertisements

Written by Sandip

July 9, 2008 at 11:25 am

Posted in .Net

4 Responses

Subscribe to comments with RSS.

  1. this was great, thanks. it actually seemed to work better than .merge() that is included in vb.net

    Stu

    February 17, 2009 at 9:55 pm

  2. greeat……

    Anton

    May 15, 2009 at 2:49 pm

  3. Greatest Logic ever invented ..u R the next Einstein

    Dax

    September 6, 2010 at 1:11 pm

  4. Thank you for your code helped me a lot

    Izaias

    September 9, 2011 at 3:07 am


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: