Home All Groups Group Topic Archive Search About

Create new table in DataSet

Author
6 Mar 2007 4:05 PM
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.


--
Peter Aitken

Author
6 Mar 2007 5:31 PM
William (Bill) Vaughn
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)
-----------------------------------------------------------------------------------------------------------------------

Show quote
"Peter A" <pait***@CRAP.nc.rr.com> wrote in message
news:MPG.20575b49ae0ee560989691@msnews.microsoft.com...
>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.
>
>
> --
> Peter Aitken
Author
6 Mar 2007 9:54 PM
Peter A
In article <egLaUVBYHHA.1***@TK2MSFTNGP04.phx.gbl>,
billvaRemoveT***@nwlink.com says...
>
> 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.

--
Peter Aitken
Author
6 Mar 2007 11:44 PM
William (Bill) Vaughn
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)
-----------------------------------------------------------------------------------------------------------------------

Show quote
"Peter A" <pait***@CRAP.nc.rr.com> wrote in message news:MPG.2057ad2dd64ea41c989692@msnews.microsoft.com...
> In article <egLaUVBYHHA.1***@TK2MSFTNGP04.phx.gbl>,
> billvaRemoveT***@nwlink.com says...
>>
>> 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.
>
> --
> Peter Aitken
Author
7 Mar 2007 1:44 AM
RobinS
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.
-------------------------------------------------------------
"William (Bill) Vaughn" <billvaRemoveT***@nwlink.com> wrote in message
news:ewgqxlEYHHA.3848@TK2MSFTNGP02.phx.gbl...
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)
-----------------------------------------------------------------------------------------------------------------------

Show quote
"Peter A" <pait***@CRAP.nc.rr.com> wrote in message
news:MPG.2057ad2dd64ea41c989692@msnews.microsoft.com...
> In article <egLaUVBYHHA.1***@TK2MSFTNGP04.phx.gbl>,
> billvaRemoveT***@nwlink.com says...
>>
>> 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.
>
> --
> Peter Aitken
Author
7 Mar 2007 4:13 PM
Peter A
In article <iN-dnX7MB7SHhHPYnZ2dnUVZ_sSmn***@comcast.com>,
RobinS@NoSpam.yah.none says...
Show quote
> Subject: Re: Create new table in DataSet
> From: RobinS <RobinS@NoSpam.yah.none>
> 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 Aitken
Author
8 Mar 2007 10:50 AM
Vayse
Show quote
"Peter A" <pait***@CRAP.nc.rr.com> wrote in message
news:MPG.2058aea455e1de26989693@msnews.microsoft.com...
> 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 Aitken

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
Author
8 Mar 2007 4:43 PM
Peter A
Show quote
In article <up69x8WYHHA.***@TK2MSFTNGP06.phx.gbl>, "Vayse" <vvv> says...
> Subject: Re: Create new table in DataSet
> From:  <"Vayse" <vvv>>
> Newsgroups: microsoft.public.dotnet.framework.adonet
>
> "Peter A" <pait***@CRAP.nc.rr.com> wrote in message
> news:MPG.2058aea455e1de26989693@msnews.microsoft.com...
> > 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 Aitken
>
> 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.

--
Peter Aitken

AddThis Social Bookmark Button