|
-
Dec 11th, 2008, 11:57 AM
#1
Thread Starter
Fanatic Member
[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.
-
Dec 11th, 2008, 05:36 PM
#2
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.
-
Dec 11th, 2008, 06:34 PM
#3
Lively Member
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.
-
Dec 11th, 2008, 07:09 PM
#4
Re: [2005] Generating a datatabIe dt2 with a subset of the columns from an original datat
 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.
-
Dec 12th, 2008, 09:10 AM
#5
Thread Starter
Fanatic Member
Re: [2005] Generating a datatabIe dt2 with a subset of the columns from an original datat
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|