How to create new DataTable with column structure from other DataTable?

Kamil

As in title - the question is:

How to create new DataTable with column structure from other DataTable?

I need empty DataTable to use .Rows.Add() method in it.

Code:

DataTable dtFirst = new DataTable();
dtFirst.Columns.Add("column1");
dtFirst.Columns.Add("column2");
dtFirst.Columns.Add("column3");

FillDataTableFirst(); // before I create second DataTable - dtFirst is filled

// here I need dtSecond DataTable with same column structure
// i cant just copy first data table, because it contains data

DataTable dtSecond = ???;
Bridge

You are looking for the DataTable.Clone() method (available since framework version 1.1).

From the documentation:

Clone creates a new DataTable with the same structure as the original DataTable, but does not copy any data (the new DataTable will not contain any DataRows). To copy both the structure and data into a new DataTable, use Copy.

In your example:

DataTable dtFirst = new DataTable();
dtFirst.Columns.Add("column1");
dtFirst.Columns.Add("column2");
dtFirst.Columns.Add("column3");

FillDataTableFirst(); // before I create second DataTable - dtFirst is filled

DataTable dtSecond = dtFirst.Clone();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to create new DataTable with column structure from other DataTable?

From Dev

How to remove column from dataTable

From Dev

How to make new datatable using other tables

From Dev

How to make Jquery DataTable Column as HyperLink or ActionLink with other column value?

From Dev

How to add new column based on existing column in a datatable in VB?

From Java

Serverside datatable combining column data and data from other tables

From Dev

How to fill one column using list and other by datatable in datagridview?

From Dev

Create a new regular table from visible and filtered rows of DataTable

From Dev

Create a new regular table from visible and filtered rows of DataTable

From Dev

Create New Object From DataTable using field Mapping

From Dev

Create dataTable column definition from uploaded csv data

From Dev

Remove the first column from datatable

From Dev

Remove the first column from datatable

From Dev

Create Excel Sheet From a DataTable

From Dev

How to redraw DataTable with new data

From Dev

Summarize datatable by group based on nrows of other column

From Dev

How to get a subset DataTable from another DataTable filtered by few column values?

From Dev

How to get a subset DataTable from another DataTable filtered by few column values?

From Java

How to check if a column exists in a datatable

From Dev

How to get name of datatable column?

From Dev

How to save DataTable Column Reorder?

From Dev

How to get values in a column of a DataTable?

From Dev

How to get sum of column in datatable

From Dev

How to get list of one column values from DataTable?

From Dev

How to get a specific column value from a DataTable in c#

From Dev

How to get one specific column from datatable into model in MVC Razor

From Dev

DataTable jquery - How to remove sort icon from first column?

From Dev

How to get list of one column values from DataTable?

From Dev

DataTable jquery - How to remove sort icon from first column?

Related Related

  1. 1

    How to create new DataTable with column structure from other DataTable?

  2. 2

    How to remove column from dataTable

  3. 3

    How to make new datatable using other tables

  4. 4

    How to make Jquery DataTable Column as HyperLink or ActionLink with other column value?

  5. 5

    How to add new column based on existing column in a datatable in VB?

  6. 6

    Serverside datatable combining column data and data from other tables

  7. 7

    How to fill one column using list and other by datatable in datagridview?

  8. 8

    Create a new regular table from visible and filtered rows of DataTable

  9. 9

    Create a new regular table from visible and filtered rows of DataTable

  10. 10

    Create New Object From DataTable using field Mapping

  11. 11

    Create dataTable column definition from uploaded csv data

  12. 12

    Remove the first column from datatable

  13. 13

    Remove the first column from datatable

  14. 14

    Create Excel Sheet From a DataTable

  15. 15

    How to redraw DataTable with new data

  16. 16

    Summarize datatable by group based on nrows of other column

  17. 17

    How to get a subset DataTable from another DataTable filtered by few column values?

  18. 18

    How to get a subset DataTable from another DataTable filtered by few column values?

  19. 19

    How to check if a column exists in a datatable

  20. 20

    How to get name of datatable column?

  21. 21

    How to save DataTable Column Reorder?

  22. 22

    How to get values in a column of a DataTable?

  23. 23

    How to get sum of column in datatable

  24. 24

    How to get list of one column values from DataTable?

  25. 25

    How to get a specific column value from a DataTable in c#

  26. 26

    How to get one specific column from datatable into model in MVC Razor

  27. 27

    DataTable jquery - How to remove sort icon from first column?

  28. 28

    How to get list of one column values from DataTable?

  29. 29

    DataTable jquery - How to remove sort icon from first column?

HotTag

Archive