How create distinct copy of dataTable

datatable

#1

I have a DataTable, let’s name it DT1 with some data
I create a new one (Assign : DT2 = new DataTable() )
I copy the data of DT1 in DT2 (Assign : DT2 = DT1)
My problem is when I remove some columns in DT2, there are also removed in DT1.
It seems my datatables are linked together, how could I create a separate copy of DT1?


#2

@alice the reason this happens is because the DataTable type is passed by reference instead of value.

You should be able to use Assign: DT2 = DT1.Copy to work around this

https://msdn.microsoft.com/en-us/library/system.data.datatable.copy(v=vs.110).aspx

https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/procedures/passing-arguments-by-value-and-by-reference


#3

@JMP Thank you, it works!