473,396 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Datatable to a new DataTable

I am trying to create a new file from a datatable (not sql server table).
I am doing it in this way at the moment:

Dim dataTable2 As DataTable = dataTable.Clone
drs = dataTable.Select("", "SortField,F5,F6")
For Each dr In drs
stemp = dr(1)
dr(1) = stemp.Substring(0, 2) & "/" & stemp.Substring(2, 2) & "/" &
stemp.Substring(4, 2)
dataTable2.ImportRow(dr)
Next

This works fine. But I need to be able to reorder the columns and drop
some. The problem with the above method is that it will create the new
datatable fine but will leave the columns the same.

It thought about using a DataView, but the problem is that when you use rows
they are DataRowViews not DataRows which is what ImportRow needs.

Is there a way to easily copy the DataTable to a new DataTable with a new
sort as I do above as well as new columns?

Thanks,

Tom
Apr 12 '07 #1
1 1170
Hi,

You can use the overloaded DataView.ToTable(xxx,xxx,xxx)

http://msdn2.microsoft.com/en-us/lib...aa(VS.80).aspx

I hope this helps,

Cor

"tshad" <t@home.comschreef in bericht
news:eC**************@TK2MSFTNGP05.phx.gbl...
>I am trying to create a new file from a datatable (not sql server table). I
am doing it in this way at the moment:

Dim dataTable2 As DataTable = dataTable.Clone
drs = dataTable.Select("", "SortField,F5,F6")
For Each dr In drs
stemp = dr(1)
dr(1) = stemp.Substring(0, 2) & "/" & stemp.Substring(2, 2) & "/" &
stemp.Substring(4, 2)
dataTable2.ImportRow(dr)
Next

This works fine. But I need to be able to reorder the columns and drop
some. The problem with the above method is that it will create the new
datatable fine but will leave the columns the same.

It thought about using a DataView, but the problem is that when you use
rows they are DataRowViews not DataRows which is what ImportRow needs.

Is there a way to easily copy the DataTable to a new DataTable with a new
sort as I do above as well as new columns?

Thanks,

Tom

Apr 13 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: randy | last post by:
Hello all, I have a DataTable which I am building column by column and adding rows after each new column. The DataTable columns match the columns in my database table. I'm building the...
5
by: Stefan Turalski \(stic\) | last post by:
Hi, I'm wondering if there is a way to send a method parametrs by ref when DataTabel is a type of this value ? I done some sort of select over DataTable columns, just by removing them froma...
1
by: TaeHo Yoo | last post by:
Hi all, After sorting and grouping data using a dataview, then how to transfer the changed datatable in the dataview to a datatable in C#? Cheers
0
by: Chris Ericoli | last post by:
Hi, I am working with an 'in session' ado dataset with an asp.net application. My dataset is comprised of two tables, one of which maintains a few calculated datacolumns. For some reason these...
7
by: Raymond Lewallen | last post by:
Which would be the proper way or the reason for using any of the following or combinations of the following? These are the 3 ways I've figured I can do what I want to do, I just don't know which...
3
by: Gene Hubert | last post by:
I'm using DataTable.ImportRow to move data from one datatable to another... Dim dt, dtTarget As DataTable Dim dr As DataRow dt = DirectCast(Me.DataSource, DataTable) dtTarget = dt.Clone...
3
by: Jon | last post by:
I'm learning about datatables. When using the example provided by MS in the ..NET Framework Class Library for DATATABLE (see below) I get an error on line 3 that says "Type expected". Is something...
6
by: Pete Wittig | last post by:
Hi, I have a DataTable and I want to get a subset of the rows within it. I use the Select method to get my subset and the results are in a DataRow. I want to put those Rows back into a...
0
by: Maart_newbie | last post by:
Hi all, I've got a question about returning the value of a pk-column to a DataTable after inserting a row (via a data-adapter) using MySql5. Here is the SQL and code concerned: ...
5
by: Frank Hauptlorenz | last post by:
Hello, I recognized some days ago, that returning a DataTable blocks my WCF-Service. Is this a known bug? If I add this table to a new DataSet() and return this, it works. Thank you, Frank
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.