Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to copy only the columns in a DataTable to another DataTable?

Tags:

c#

asp.net

how to copy only the columns in a DataTable to another DataTable?

like image 710
user354547 Avatar asked Jun 01 '10 12:06

user354547


2 Answers

DataTable.Clone() should do the trick.

DataTable newTable = originalTable.Clone(); 
like image 55
codingbadger Avatar answered Oct 15 '22 02:10

codingbadger


If only the columns are required then DataTable.Clone() can be used. With Clone function only the schema will be copied. But DataTable.Copy() copies both the structure and data

E.g.

DataTable dt = new DataTable(); dt.Columns.Add("Column Name"); dt.Rows.Add("Column Data"); DataTable dt1 = dt.Clone(); DataTable dt2 = dt.Copy(); 

dt1 will have only the one column but dt2 will have one column with one row.

like image 25
freak Avatar answered Oct 15 '22 01:10

freak