how to copy only the columns in a DataTable to another DataTable?
DataTable.Clone() should do the trick.
DataTable newTable = originalTable.Clone();
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With