Unable to Copy datacolumn from one data table to another

后端 未结 6 968
独厮守ぢ
独厮守ぢ 2021-01-17 22:28

How can I copy 1 data column from 1 data table to a new datatable. When I try to do it, I get the error Column \'XXX\' already belongs to another DataTable.?



        
6条回答
  •  情书的邮戳
    2021-01-17 23:05

    The problem is caused by the c# can not reuse the object instance created and uses it on multiples DataTables. For this it is necessary to create a new object DataCollumn for each loop iteration.

    foreach (DataTable table in DATASET.Tables)
    {
        DataColumn yourDataCollumn = new DataColumn("Name of DataCollumn", typeof(Your data type));
        // your logic here
    }
    

    Hope it's help...

提交回复
热议问题