I would like to copy DataColumn from tableA to table B:
As I think it should be like this:
TableA in perdefined.
DataColumn[] dataColum = new
DataColumn[TableA .Columns.Count];TableA .Columns.CopyTo(dataColum, 0);
DataTable TableB = new DataTable();
TableB .Columns.AddRange(dataColum);
//
=> but it raised an error at the line error, ID is belong to another table (in tableB)…
As I dont like another way:
DataTable TableB = TableA.Copy();
TableB.Clear(); // => getting
structure of table A, not nice this
way
Any ideas? Thanks in advance!
You can use DataTable.Clone method that copies only structure (i.e all columns & constraints) unlike Copy that copies data also.
In case you need to copy a particular column then you need to use code such as
TableB.Columns.Add(columnToAdd.ColumnName, columnToAdd.DataType)