Creating a subset table/view

  • Thread starter Thread starter Sabine
  • Start date Start date
S

Sabine

How can I crate a subset table or view from a table? I
only want certain columns and all the rows? I do NOT want
to loop through the rows and add those to my new table or
view. I just want to specify a couple of columns and
retrieve a subset table or view. Someone has an idea?

Thanks,

Sabine
 
Hi Sabine,

The following code should accomplish creating a subset table that has 2 columns out of 11 and all the rows:

Dim da1 As SqlDataAdapter = New SqlDataAdapter("select * from Customers", cn)
da1.Fill(ds, "customers")

Dim dtSubset As DataTable
dtSubset = ds.Tables("Customers").Copy 'Copy all the rows and columns
Dim count As Integer = dtSubset.Columns.Count
Dim i As Integer
For i = count To 3 Step -1
Dim dc As DataColumn = dtSubset.Columns(i - 1)
dtSubset.Columns.Remove(dc) 'Delete all the columns except the first 2 columns
Next i
DataGrid1.DataSource = dtSubset

The Remove method has an oveload that accepts a Column Name as an argument so you may specify a column name instead of a DataColumn object.

I hope this helps!


Thanks,
Hussein Abuthuraya
Microsoft Developer Support

This posting is provided "AS IS" with no warranties, and confers no rights.

Are you secure? For information about the Microsoft Strategic Technology Protection Program and to order your FREE Security Tool Kit, please visit
http://www.microsoft.com/security.
 
Back
Top