|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Copying objects (DataView)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? 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? 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? |
|||||||||||||||||||||||