[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.
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.
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.
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.
Re: [2005] Generating a datatabIe dt2 with a subset of the columns from an original datat