Copying objects (DataView)

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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.
 
ok thanks!

just a tiny correction:

dtvLocal = dtvGlobal.Table.Copy.DefaultView
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top