Solved

Copy One Column From One DataTable To Another DataTable

Posted on 2006-11-19
7
583 Views
Last Modified: 2010-04-23
I have an application that needs to take column out of a DataTable and insert it into another DataTable.

I have tried:

dtAllTheData.Columns.Add(dtFirst.Columns(3))

but I get an error saying that that column is already part of a dataset.

How do I do this?  I need to create a Single Table that has columns from a series of other tables.

0
Comment
Question by:lgropper
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 10

Expert Comment

by:Kinger247
ID: 17976072
You could try cloning the column:

Dim DataColumn As DataColumn = dataset.Tables(0).Clone.Columns(0)
0
 

Author Comment

by:lgropper
ID: 17976285
That doesn't work, neither does


Dim MyCol as DataColumn = dataset.Tables(0).Copy.Columns(0)


gives the same Error saying that "" already belongs to dataset.Tables(0)


0
 
LVL 34

Accepted Solution

by:
Sancler earned 500 total points
ID: 17976383
You could overcome the "belongs to another table" problem on these lines

        Dim dc As DataColumn = dt1.Columns(0)
        dt1.Columns.Remove(dc)
        dt2.Columns.Add(dc)

but it won't produce what (I think) you want.  I imagine that what you actually want is not only the column, but also the data in it.  But the data belongs to a datatable's rows, not to its columns.  So all the above would do would be to put an empty column (with the same properties as the original one) in the second datatable - and, incidentally, get rid of the column and the data in each row for that column in the original datatable.

What you are going to have to do, I think, is create a new column in the second datatable and set its properties the same as those for the column you want to copy.  And then cycle through the rows in the original datatable, creating a new row for the second datatable, and setting the value for that as equal to the equivalent value from the original datatable.  On these lines

       Dim dt2 As New DataTable
       Dim dc As New DataColumn
       With dc
           .ColumnName = dt1.Columns(oldCol).ColumnName
           .DataType = dt1.Columns(oldCol).DataType
           'etcetera for each property you want transferred
       End With
       dt2.Columns.Add(dc)

       For Each dr As DataRow in dt1.Rows
           Dim ndr As DataRow = dt2.NewRow
           ndr(0) = dr(oldCol)
           dt2.Rows.Add(ndr)
       Next

Roger
0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 96

Expert Comment

by:Bob Learned
ID: 17978984
You could create a copy of the DataTable, using the Copy method, and then drop the columns that you don't need.

Bob
0
 
LVL 34

Expert Comment

by:Sancler
ID: 17979308
Bob

Agreed - for one column only.  But the question ends "I need to create a Single Table that has columns from a series of other tables" and I don't think you could do that from more than one table.

Roger
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17986484
Thanks, Roger, I missed that one.

Bob
0
 

Author Comment

by:lgropper
ID: 18030881
whoops, that should have been Excellent!  thanks a lot!
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Receiving a string from a WebService Push 21 47
vb.net help 4 31
HTML - Color not displaying correctly in EMAIL. 6 38
get column names from table in vb.net 8 29
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question