Well, it's pretty simple if you understand the difference between
reference types and value types.

When you assign a variable dt1 as equivalent to another variable dt2
which holds a reference to a DataTable, both variables then *point* to
the same memory location. Change one and the other will be changed
too, because they both point to the same object on the heap.

This is why the Copy method works, because it *copies* the object on
the heap and then assigns the new variable to point to the copy of the
original object.

On Feb 11, 9:58 pm, Nacho108 <[email protected]> wrote:
> Hi,
>
> I have a beginner's doubt, maybe somebody can give a quick help.
> I'm assigning a datatable to a datagrid and then I'm trying to copy a
> datatable into another with the following method:
>
> datatable_1 = datatable_2;
> datagridview1.source=datatable_1;
>
> Then when I try to modify datatable_2 in a background thread, a
> misterioous exception jumps ...
>
> Then trying another options I tried the following code and it works
> fine while I'm modifying datatable_2 in another thread:
>
> datatable_1 = datatable_2.copy();
> datagridview1.source=datatable_1;
>
> Can somebody explain to me why? Maybe is a very stupid question, but
> I'm just starting with this.
> Isn't it possible to assign one datatable into another?
>
> Thanks in advance
> Nacho

Reply via email to