Results 1 to 10 of 10

Thread: [RESOLVED] How to merge rows of a datatable to another datatable

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2007
    Posts
    15

    [RESOLVED] How to merge rows of a datatable to another datatable

    Hi all,
    I'm searching the quickest way to add the rows of a datatable to another datatable.
    I can't use merge because i need to filter the rows of the first datatable in order to take only certain rows.
    Does anybody knows what0s the quickest way to perform this operation?
    Thanks in advance
    Last edited by jackmoros; Oct 12th, 2007 at 07:32 AM.

  2. #2
    Frenzied Member Zakary's Avatar
    Join Date
    Mar 2005
    Location
    Canada, Quebec, Montreal
    Posts
    1,654

    Re: How to merge rows of a datatable to another datatable

    The first thing that come to my mind, is to loop throught the First DataTable, then using ImportRow, your may append them to a second DataTable.


    Code:
     Dim dt1 As New DataTable
     Dim dt2 As New DataTable
    
       For Each dr As DataRow In dt1.Rows
          dt1.Rows.Remove(dr)
          dt2.ImportRow(dr)
       Next
    Using VS 2010 on Fw4.0

  3. #3
    Addicted Member
    Join Date
    May 2006
    Location
    Manchester, England
    Posts
    255

    Re: How to merge rows of a datatable to another datatable

    I guess you could use a DataView to filter out what you need, and then copy those filtered rows over to your second DataTable?
    At home - VB.NET 2005/2008 Express, Visual Web Developer 2005 Express
    At work - VS 2008 Standard (VB)
    .NET 2.0/3.5


    Visual Studio Express Learning Centre | How do I videos | MSDN VB Express Forum | MSDN VB Developer Centre

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to merge rows of a datatable to another datatable

    vb.net Code:
    1. For Each row As DataRow In table1.Select("filter goes here")
    2.     table2.ImportRow(row)
    3. Next row
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    New Member
    Join Date
    May 2007
    Posts
    15

    Re: How to merge rows of a datatable to another datatable

    Thanks to all of you, eventually i used the first method, the one proposed by Zakary, it's fast and seems to work properly.
    P.S. I tried to use the method proposet by jmcilhinney but it didn't work, the error is that the row already belongs to another datatable.

    Thanks a lot

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] How to merge rows of a datatable to another datatable

    Firstly, Zakary's method doesn't work for your request because it doesn't provide a filter. Secondly, my code will work as is. The only reason you'd get that error message is if you used Rows.Add instead of ImportRow.
    Last edited by jmcilhinney; Oct 12th, 2007 at 10:15 AM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    New Member
    Join Date
    May 2007
    Posts
    15

    Re: [RESOLVED] How to merge rows of a datatable to another datatable

    Ops sorry infact i was using Zakary's but checking if the row was good row by row...

  8. #8
    Frenzied Member Zakary's Avatar
    Join Date
    Mar 2005
    Location
    Canada, Quebec, Montreal
    Posts
    1,654

    Re: [RESOLVED] How to merge rows of a datatable to another datatable

    Both method are good, in my method, I was just considering that your filter was already done.
    Using VS 2010 on Fw4.0

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] How to merge rows of a datatable to another datatable

    Both method are good, in my method, I was just considering that your filter was already done.
    But how can you assume that if you're taking the rows directly from a DataTable? It's not possible to filter an actual DataTable. You have to either use a DataView or the table's Select method to filter.
    Ops sorry infact i was using Zakary's but checking if the row was good row by row...
    Why do that when the table can do it for you? How is my code any different to Zakary's except that it does the filtering for you? If his works then mine works, but it means you don't have to do any manual checking. Like I said, if you got the error message you said you did then you did NOT use my code. You must have been calling Rows.Add rather than ImportRow.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    New Member
    Join Date
    May 2007
    Posts
    15

    Re: [RESOLVED] How to merge rows of a datatable to another datatable

    Ok, so next week i'll try again to use your method, but this time i'll pay more attention.
    Txs again

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width