Copy One Column From One DataTable To Another DataTable

Posted on 2006-11-19
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:


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.

Question by:lgropper
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
LVL 10

Expert Comment

ID: 17976072
You could try cloning the column:

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

Author Comment

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)

LVL 34

Accepted Solution

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)

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

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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.

LVL 34

Expert Comment

ID: 17979308

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.

LVL 96

Expert Comment

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


Author Comment

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

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

636 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