John
John

Reputation: 10969

Make a new DataTable with the same columns as another DataTable

I want to create a new DataTable that has the same columns as another DataTable.

Currently, I do the following:

DataTable myTable = new DataTable();
myTable = table.Copy();
myTable.Clear();

Then, I import rows into myTable as needed.

Is there a more efficient way of doing this? Right now if table is large, then there is a lot of unnecessary copying of rows going on.

Thanks.

Upvotes: 19

Views: 47429

Answers (2)

Tony
Tony

Reputation: 684

Use the Clone method - it creates a copy of the schema (columns) only.
See DataTable.Clone

Upvotes: 10

Stecya
Stecya

Reputation: 23276

Try to use

myTable = table.Clone()

Upvotes: 38

Related Questions