How can we copy the column data of one DataTable to another, even if there are different column names between the DataTables?

I have two data tables. At first

DataTable NameAddressPhones = new DataTable(); 

with three columns Name, Address and PhoneNo . But I need only two columns Name and address , so I want to copy these columns (with data) into a new DataTable.

DataTable NameAddress = new DataTable(); 

For this 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);
            }

NameAddressPhones () DataTable , . , Nm , . , DataTable , Nm Add DataTable, DataTable. , , Nm DataTable DataTable DataTable DataTable.

, DataTable , , Nm - DataTable - DataTable, Nm .

+3
3

:

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

, . , - .

, , - .

+10

, :

destRow[0] = sourcerow[0]; // for column 0 = "Name" or "NM"
+1

, , , . , . , .

0

Source: https://habr.com/ru/post/1740157/


All Articles