Results 1 to 5 of 5

Thread: [2005] Generating a datatabIe dt2 with a subset of the columns from an original datat

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2006
    Posts
    589

    [2005] Generating a datatabIe dt2 with a subset of the columns from an original datat

    Hi,

    I have a datatable dt1 having the columns: col1, col2, col3...colN. dt1 is populated with data

    From dt1 , I want to generate another table having only a subset of the dt1 columns. For example, i want a table: dt2 that only has the column: col3, col5...

    I still want the genreated table dt2 to be populated with data from dt1, except taht the columns I don t need from dt1 will just be ignored while generating dt2.

    Thanks.
    Thanks a lot for your help.

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

    Re: [2005] Generating a datatabIe dt2 with a subset of the columns from an original datat

    There is no automatic way to do that. You're just going to have to create a new DataTable and add to its Columns collection, then loop through the rows and transfer the data.
    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

  3. #3
    Lively Member
    Join Date
    Jul 2005
    Posts
    101

    Re: [2005] Generating a datatabIe dt2 with a subset of the columns from an original datat

    You could always clone the first table into the second table, then use table.remove and remove the columns you don't want, however here's a more direct way using table.defaultview.totable:

    Code:
       Dim test As DataTable = New DataTable("Test")
       Dim test2 As DataTable
       test2 = test.DefaultView.ToTable("Test2", False, New String() {"col3", "col5", "etc.."})
    Obviously you need to change it to fit your code but that should be enough to get you going.
    Last edited by Essential; Dec 11th, 2008 at 06:56 PM.

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

    Re: [2005] Generating a datatabIe dt2 with a subset of the columns from an original datat

    Quote Originally Posted by Essential
    You could always clone the first table into the second table, then use table.remove and remove the columns you don't want, however here's a more direct way using table.defaultview.totable:

    Code:
       Dim test As DataTable = New DataTable("Test")
       Dim test2 As DataTable
       test2 = test.DefaultView.ToTable("Test2", False, New String() {"col3", "col5", "etc.."})
    Obviously you need to change it to fit your code but that should be enough to get you going.
    Hey, cool! I was aware of the DataView.ToTable method but I was unware that it allowed you to choose columns. Excellent.
    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
    Fanatic Member
    Join Date
    Aug 2006
    Posts
    589

    Re: [2005] Generating a datatabIe dt2 with a subset of the columns from an original datat

    amazing, thanks guys.
    Thanks a lot for your help.

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