Why changes reflect on both tables after one Datatable is assigned to another using the 'equals' operator?
As Datatable works as a reference type, data of both the tables changes if changes are made in one. This is the expected behavior.
Datatable works as a reference type, hence when the operation DataTable2 = DataTable1 is performed, the changes made to one table reflects on the other.
To resolve this problem, use the DataTable's Copy() or Clone() method to create a new DataTable with the same structure.
Example:
-
DataTable dataTable2=dataTable1.Copy()
-
DataTable dataTable2 = dataTable1.Clone() for (int i = 0; i < dataTable1.Rows.Count; i++) { dataTable2.ImportRow(dataTable1.Rows[i]); }
To verify this, write a sample VB code and check the behavior. For more information, please refer to the attached documentation.