Home All Groups Group Topic Archive Search About

Copying objects (DataView)

Author
14 Dec 2005 4:27 PM
Andreiwid
Hi!

I've declared a DataView in the form's declaration section as global variable:

Dim dtvGlobal As New System.Data.DataView

In some event I need to make an identical copy (structure + data) of the
global dataview and then manipulate the copy without changing the source
dataview:

Dim dtvLocal As New System.Data.DataView
dtvLocal = dtvGlobal
dtvLocal.Sort = "BarCode"

But when I try dtv.Sort the dtvGlobal is affected too.

Please where I am making mistake?

Author
14 Dec 2005 5:37 PM
mtgcsharpguy @ DotNetFun.com
THe problem is that you're copying the reference to dtvGlobal, not dtvGlobal
itself. Therefore, a change to dtvLocal affects dtvGlobal as they're both the
same object in memory.

Do this instead:

Dim dtvLocal As New System.Data.DataView
dtvLocal.Table = dtvGlobal.Table
dtvLocal.Sort = "BarCode"

Now you have two different copies of the DataView, but which use the same
DataTable.

Show quote
"Andreiwid" wrote:

> Hi!
>
> I've declared a DataView in the form's declaration section as global variable:
>
> Dim dtvGlobal As New System.Data.DataView
>
> In some event I need to make an identical copy (structure + data) of the
> global dataview and then manipulate the copy without changing the source
> dataview:
>
> Dim dtvLocal As New System.Data.DataView
> dtvLocal = dtvGlobal
> dtvLocal.Sort = "BarCode"
>
> But when I try dtv.Sort the dtvGlobal is affected too.
>
> Please where I am making mistake?
Author
14 Dec 2005 5:40 PM
Andreiwid
ok thanks!

just a tiny correction:

dtvLocal = dtvGlobal.Table.Copy.DefaultView

Show quote
"mtgcsharpguy @ DotNetFun.com" wrote:

> THe problem is that you're copying the reference to dtvGlobal, not dtvGlobal
> itself. Therefore, a change to dtvLocal affects dtvGlobal as they're both the
> same object in memory.
>
> Do this instead:
>
> Dim dtvLocal As New System.Data.DataView
> dtvLocal.Table = dtvGlobal.Table
> dtvLocal.Sort = "BarCode"
>
> Now you have two different copies of the DataView, but which use the same
> DataTable.
>
> "Andreiwid" wrote:
>
> > Hi!
> >
> > I've declared a DataView in the form's declaration section as global variable:
> >
> > Dim dtvGlobal As New System.Data.DataView
> >
> > In some event I need to make an identical copy (structure + data) of the
> > global dataview and then manipulate the copy without changing the source
> > dataview:
> >
> > Dim dtvLocal As New System.Data.DataView
> > dtvLocal = dtvGlobal
> > dtvLocal.Sort = "BarCode"
> >
> > But when I try dtv.Sort the dtvGlobal is affected too.
> >
> > Please where I am making mistake?

AddThis Social Bookmark Button