Create new table in DataSet

  • Thread starter Thread starter Peter A
  • Start date Start date
P

Peter A

I have a DataSet that contains one table. I want to create two new
tables each of which contains all the rows but only some of the columns
from that first table. Can I do this without going back to the original
data source?

THanks.
 
Sure. Create the Table objects and use the Add method to append them to the
Tables collection.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
INETA Speaker
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
Visit www.hitchhikerguides.net to get more information on my latest book:
Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
and Hitchhiker's Guide to SQL Server 2005 Compact Edition (EBook)
 
Sure. Create the Table objects and use the Add method to append them to the
Tables collection.

Thanks - but what I do not know how to do is create the new tables based
on a subset of the data in the existing table.
 
I would
a.. Instantiate a new Table object
b.. Walk the Table(n).Columns collection and clone the desired columns
c.. Add these selected columns to the new Table object's Columns collection
d.. Add the newly configured table to the DataSet.Tables collection
hth

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
INETA Speaker
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
Visit www.hitchhikerguides.net to get more information on my latest book:
Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
and Hitchhiker's Guide to SQL Server 2005 Compact Edition (EBook)
-----------------------------------------------------------------------------------------------------------------------
 
If he's using .Net 2.0, can't he use the .ToTable with a DataView to make a
new table of selected items?

'tblAllCustomers is a DataTable with all [Customers] records from Northwind
'Create a dataview for spanish customers
Dim dv as New DataView(tblAllCustomers)
dv.RowFilter = "Country = 'Spain'"

'create a new data table based on the DataView
Dim tblSpanishCustomers As DataTable = dv.ToTable("SpanishCustomers")
Console.WriteLine("TableName: {0}", tblSpanishCustomers.TableName)
Console.WriteLine("Rows:")
For each row as DataRow in tblSpanishCustomers.Rows
Console.WriteLine(" {0}, {1}", row("City"), row("Country"))
Next Row

So if this works, could he do this, and then add the datatable to the
dataset, or even create the datatable inside the dataset?

Robin S.
-------------------------------------------------------------
I would
Instantiate a new Table object
Walk the Table(n).Columns collection and clone the desired columns
Add these selected columns to the new Table object's Columns collection
Add the newly configured table to the DataSet.Tables collection
hth

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
INETA Speaker
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
Visit www.hitchhikerguides.net to get more information on my latest book:
Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
and Hitchhiker's Guide to SQL Server 2005 Compact Edition (EBook)
-----------------------------------------------------------------------------------------------------------------------
 
Subject: Re: Create new table in DataSet
From: RobinS <[email protected]>
Newsgroups: microsoft.public.dotnet.framework.adonet

If he's using .Net 2.0, can't he use the .ToTable with a DataView to make a
new table of selected items?

'tblAllCustomers is a DataTable with all [Customers] records from Northwind
'Create a dataview for spanish customers
Dim dv as New DataView(tblAllCustomers)
dv.RowFilter = "Country = 'Spain'"

'create a new data table based on the DataView
Dim tblSpanishCustomers As DataTable = dv.ToTable("SpanishCustomers")
Console.WriteLine("TableName: {0}", tblSpanishCustomers.TableName)
Console.WriteLine("Rows:")
For each row as DataRow in tblSpanishCustomers.Rows
Console.WriteLine(" {0}, {1}", row("City"), row("Country"))
Next Row

So if this works, could he do this, and then add the datatable to the
dataset, or even create the datatable inside the dataset?

Robin S.

I still think my question is being misunderstood.

I have a datatable with 10 columns, A, B, C, ... J. It is populated with
data. I want to create a new data table that contains only columns A, B,
C, D, and E along with the column name and all of the column data.

I don't see a Clone() method that I can use to create a new column
that's a duplicate of an existing one.
 
Peter A said:
I still think my question is being misunderstood.

I have a datatable with 10 columns, A, B, C, ... J. It is populated with
data. I want to create a new data table that contains only columns A, B,
C, D, and E along with the column name and all of the column data.

I don't see a Clone() method that I can use to create a new column
that's a duplicate of an existing one.

A dataview won't work, as dataviews do not let you select a subset of
columns.
I think Bills second answer should do you. I'm not sure what he means by
clone, here's some code that worked for me.
I created a form with two datagrids - gridOrig and gridCopy.
sqlData is just my own class I have to perform common database functions.
I add 3 columns, call them Code, Executive and Salesman.
Then I use columns 0,2,4 from the original table to fill the columns.
It would probably be better to use the DataColumnCollection, but this should
set you on the right track.

Const ORIG As String = "Original"
Const COPY As String = "Copy"
Dim dsTest As New DataSet
Dim sqlData As New clsData
Dim stSQL as string = "SELECT * FROM TestSales"

dsTest.Tables.Add(ORIG)
If sqlData.FillTable(stSQL, dsTest.Tables(ORIG)) Then
Me.gridOrig.DataSource = dsTest.Tables(ORIG)

With dsTest
.Tables.Add(COPY)

.Tables(COPY).Columns.Add("Code")
.Tables(COPY).Columns.Add("Executive")
.Tables(COPY).Columns.Add("SalesMan")

For Each rowOrig As DataRow In .Tables(ORIG).Rows
.Tables(COPY).Rows.Add(rowOrig(0), rowOrig(2),
rowOrig(4))
Next

Me.gridCopy.DataSource = .Tables(COPY)
End With

End If


HTH
Vayse
 
Subject: Re: Create new table in DataSet
From: <"Vayse" <vvv>>
Newsgroups: microsoft.public.dotnet.framework.adonet



A dataview won't work, as dataviews do not let you select a subset of
columns.

<snipped>

I discovered a way to do what I needed. Use the DataTable.Copy method to
create a duplicate of the original table, and then use the Remove method
to remove those columns you do not want in the copy.
 
Back
Top