Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 721319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:51:36+00:00 2026-05-14T05:51:36+00:00

I have two DataTables. First is DataTable NameAddressPhones = new DataTable(); with Three columns

  • 0

I have two DataTables.
First is

DataTable NameAddressPhones = new DataTable(); 

with Three columns Name, Address and PhoneNo.But I only want two columns Name and Address data so I want to copy those columns (with data) to the new DataTable.

DataTable NameAddress = new DataTable(); 

For that I do

            foreach (DataRow sourcerow in NameAddressPhones.Rows)
            {
                DataRow destRow = NameAddress.NewRow();
                foreach (string colname in columns)
                {
                    destRow[colname] = sourcerow[colname];
                }
                NameAddress.Rows.Add(destRow);
            }

I clear the NameAddressPhones(first) DataTable every time there are new records inserted in the table. And every time there will be the same number of columns but the column names will be different like Nm instead of Name, Add instead of Address.Now the problem is the second DataTable already has column names Name and Address and now I want to copy the columns data of Nm and Add to the second DataTable but the column names are different than the column names of the second DataTable. So even if there are different column names I want to copy Nm column data of first DataTable to the column Name of second DataTable and column Add data of first DataTable to column Address of second DataTable.

In short how can we copy column data from one DataTable to another even if there are different column names of both DataTables like Nm is the column name of first DataTable and Name is the column name of second DataTable then the data of the column Nm should be copied to the column Name.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T05:51:36+00:00Added an answer on May 14, 2026 at 5:51 am

    Here’s the simplest way:

    foreach (DataRow sourcerow in NameAdressPhones.Rows)
    {
        DataRow destRow = NameAdress.NewRow();
        destRow["Name"] = sourcerow["Nm"];
        destRow["Address"] = sourcerow["Add"];
        NameAdress.Rows.Add(destRow);
    }
    

    Automation is great when it’s available. When it’s not, you have to map source columns to destination columns in some manner.

    If the columns are in the same order in both tables, you could just reference the values by ordinal instead of column name, but that’s such a bad idea I’m not even going to post any code for it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two datatable , first contain DataTable dtMaterialStatement = new DataTable(); dtMaterialStatement.Columns.Add(new DataColumn(MaterialNo,
I have two datatables. Each has three columns, the two of them have the
I have two DataTables. I want to do inner join them into a new
I have two DataTable [System.Data] .One contains three rows id,name,date and another contains id
I have two datatable, while adding rows in second datatable, I want to first
I have two datatables. 1st table-----> DataTable _dtMain = new COrder().GetDetails(); 2nd table-----> DataTable
I have two datatables. One is to display names. Another contains three <h:selectOneMenu> .
I have a datatable with two columns, Column 1 = EmpID Column 2 =
I have a DataTable which I want to check if values in three of
I have a datatable I'm using which has 5 columns ( http://datatables.net/ ) The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.