Wednesday, 9 July 2014

Difference between Copying and Cloning the DataTable



 Copying and Cloning the DataTable: 


DataTable.Clone() - Copies only the structure of the source table with the constraints, the data is not copied from the source table.

DataTable.Copy() - Copies the structure and data from the source table.

For example, we may want to assign a DataTable object to a GridView control to allow a user to edit the data, but you also may want to provide a cancel button that aborts all changes on the Web page.

A simple way to implement this functionality is to create a copy of your DataTable object and use the copy for editing. If the user clicks the cancel button, the DataTable copy is thrown out.

If the user decides to keep the changes, you can replace the original DataTable object with the edited copy.

To create a copy of a DataTable object, use the Copy method on the DataTable, which copies the DataTable object schema and data.

DataTable copy = employee.Copy( );  
  

You often require a copy of the DataTable schema without the data. You can accomplish this by invoking the Clone method on the DataTable. Use this method when an empty copy of the DataTable is required and to which DataRow objects will be added at a later time.

DataTable clone = employee.Clone( ); 

1 comment: