Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Merging or adding 2 different DataTables into Single DataTable

Merging or adding 2 different DataTables into Single DataTable

Scheduled Pinned Locked Moved C#
tutorialquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    K V Sekhar
    wrote on last edited by
    #1

    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 c3

    dt2: contains records like below ===============================

    colD colE colF

    d1 e1 f1
    d2 e2 f2
    d3 e3 f3

    I 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 f3

    How 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.

    D 1 Reply Last reply
    0
    • K K V Sekhar

      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 c3

      dt2: contains records like below ===============================

      colD colE colF

      d1 e1 f1
      d2 e2 f2
      d3 e3 f3

      I 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 f3

      How 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.

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      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...

      K 1 Reply Last reply
      0
      • D Dave Kreskowiak

        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...

        K Offline
        K Offline
        K V Sekhar
        wrote on last edited by
        #3

        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.

        D 1 Reply Last reply
        0
        • K K V Sekhar

          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.

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          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...

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups