Adding a DataColumn to a DataTable

I want to move data from dataColumnto a specific column in dataTable. I am not sure how to indicate which column inside mine dataTableI want to add dataColumn.

        foreach (DataColumn col in dt.Columns)
        {
            dt1.Columns.Add(col);
        }

I get an exception Column 'X' already belongs to another DataTable.

+5
source share
4 answers

You need to copy the type properties ColumnNameand create a new one DataColumns:

foreach (DataColumn col in dt.Columns)
{
    dt1.Columns.Add(col.ColumnName, col.DataType);
}

There is a reason for ArgumentExceptionwhen you add DataColumnthat already belongs to another DataTable. It would be very dangerous to assume that since a DataTablecontains a link in its columns, and each column contains a DataTable link to it. If you add a column to another table, your code will hit sooner or later.

DataRows :

foreach (DataRow row in t1.Rows)
{
    var r = t2.Rows.Add();
    foreach (DataColumn col in t2.Columns)
    {
        r[col.ColumnName] = row[col.ColumnName];
    }
}
+14

DataColumn , , DataColumn Add, Object. . :

public static class DataColumnExtensions
{
    public static DataColumn CopyTo(this DataColumn column, DataTable table)
    {
        DataColumn newColumn = new DataColumn(column.ColumnName, column.DataType, column.Expression, column.ColumnMapping);
        newColumn.AllowDBNull = column.AllowDBNull;
        newColumn.AutoIncrement = column.AutoIncrement;
        newColumn.AutoIncrementSeed = column.AutoIncrementSeed;
        newColumn.AutoIncrementStep = column.AutoIncrementStep;
        newColumn.Caption = column.Caption;
        newColumn.DateTimeMode = column.DateTimeMode;
        newColumn.DefaultValue = column.DefaultValue;
        newColumn.MaxLength = column.MaxLength;
        newColumn.ReadOnly = column.ReadOnly;
        newColumn.Unique = column.Unique;

        table.Columns.Add(newColumn);

        return newColumn;
    }

    public static DataColumn CopyColumnTo(this DataTable sourceTable, string columnName, DataTable destinationTable)
    {
        if (sourceTable.Columns.Contains(columnName))
        {
            return sourceTable.Columns[columnName].CopyTo(destinationTable);
        }
        else
        {
            throw new ArgumentException("The specified column does not exist", "columnName");
        }
    }
}

public class MyClass
{
    public static void Main()
    {
        DataTable tableA = new DataTable("TableA");
        tableA.Columns.Add("Column1", typeof(int));
        tableA.Columns.Add("Column2", typeof(string));

        DataTable tableB = new DataTable("TableB");

        foreach (DataColumn column in tableA.Columns)
        {
            column.CopyTo(tableB);
        }
    }
}

, , , .. tableA.CopyColumnTo("Column1", tableB);.

, :

foreach (DataRow row in tableA.Rows)
{
    tableB.Rows.Add(row.ItemArray);
}

, Tim Schmelter, , . , :

foreach (DataRow souceRow in sourceTable.Rows)
{
    DataRow destinationRow = destinationTable.Rows.Add();

    foreach (DataColumn destinationColumn in destinationTable.Columns)
    {
        string columnName = destinationColumn.ColumnName;

        if (sourceTable.Columns.Contains(columnName))
        {
            destinationRow[columnName] = sourceRow[columnName];
        }
    }
 } 
+3

, DataTable ArgumentException .

. : http://msdn.microsoft.com/en-us/library/55b10992.aspx

you can create a new column, the same as the one you already added to the original table, and add it to the new table.

0
source

Table data is organized by row, then by column. You cannot (I know) add a data column in one shot. You will need to add the column definition to the second table and add the data to it separately.

Since your source code is looped over all columns, you might be better off copying the source datatable with DataTable.Copy()and delete what you don't want.

0
source

All Articles