[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1920
  • Last Modified:

Copy Datatable

I have a currentdatatable and want to save it as originaldatatable, but all the changes made to my currentdatable is refleceted in the origionaldatatable.

dtOriginalDataTable = dsEmptyDatasetSP.Tables(0).Copy

I realized this didn't work...So I am tring to update it as follows:

dtOriginalDataTable = dsEmptyDatasetSP.Tables(0).Clone
Dim DtRow As DataRow
For Each DtRow In dsEmptyDatasetSP.Tables(0).Rows
            dtOriginalDataTable .ImportRow(DtRow)
Next

But this also reflected the changes made in dsEmptyDatasetSP.Tables(0) to my new origianalDatatable...

How can I have a datatable that won't change
0
Midnight2005
Asked:
Midnight2005
  • 4
  • 3
1 Solution
 
Midnight2005Author Commented:
Or can you say if it's possible or not?
0
 
SanclerCommented:
Both .Copy and .ImportRows copies/imports EVERYTHING, including the RowStates.  If you are in VB.NET 2005 you can use either and then cycle through the rows using the (new) DataRow.SetAdded method, which will change all the rowstates to New.

If you are in an earlier version of VB.NET you will need to clone the table and then copy each row's .ItemArray from the original table into a new row in the new table.  On these lines (this is from memory, as I'm not on my own machine at the moment)

dtOriginalDataTable = dsEmptyDatasetSP.Tables(0).Clone
Dim DtRow As DataRow
For Each DtRow In dsEmptyDatasetSP.Tables(0).Rows
            dim NewDtRow as DataRow = dtOriginalDataTable.NewRow
            NewDtRow.ItemArray = DtRow.ItemArray
            dtOriginalDataTable.Rows.Add(NewDtRow)
Next

Roger

0
 
Midnight2005Author Commented:
I am using VB 2005

So with your direction, I now have:

        DataGridView1.DataSource = dsEmptyDatasetSP.Tables(0)
        dtOriginalDataTable = dsEmptyDatasetSP.Tables(0).Copy
        Dim dtrow As DataRow
        For Each dtrow In dtOriginalDataTable .Rows
            dtrow.SetAdded()
        Next

'Then I have added some values to datagrid

But I am looking at dtOriginalDataTable, but I still see the changes that I just added new values to datagrid...
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
SanclerCommented:
I obviously misunderstood your question.  Your reference to "saving" the original table made me think you wished to save it to a database and your attempt to do so was being thwarted by the fact that the rowstates were Unchanged.

But now I cannot understand what the problem is.  I have just tested both

        newTable = oldTable.Copy
        dgv1.DataSource = newTable
        dgv2.DataSource = oldTable

and

        newTable = oldTable.Clone
        For Each dr As DataRow In oldTable.Rows
            newTable.ImportRow(dr)
        Next
        dgv1.DataSource = newTable
        dgv2.DataSource = oldTable

and have in both cases been able to make changes in both datagrids - dgv1 and dgv2 - independently of each other.

Roger
0
 
Midnight2005Author Commented:
You're totally right, I see that I wasn't even able explain or make any sense...
I applogize.
I am afraid nobody is going to read this anymore, but let me try to explain again:


I start with a datatable and I want to get a copy and put aside to see what I am starting with. So:

dtOriginalDataTable = dsEmptyDatasetSP.Tables(0).Copy

Then I bind the datagrid to dsEmptyDatasetSP.Tables(0)
On the datagrid, user enters some values, all of them are of course getting reflected to dsEmptyDatasetSP.tables(0)

Then when I want see or compare what I started with, I realized that dtOriginalDataTable looks like same as dsEmptyDatasetSP.tables(0) !!!

So whatever happends to other datatable, it looks like the records are saved to other datatable, too...Which I don't want this to happen...

How can I get a copy of a datatable that won't be changed no matter what happens to the other data table.
If it is still not very clear, please let me know, I appreciate the help.
0
 
SanclerCommented:
Don't apologise.  There's something strange going on here.

With a new form - you don't need any controls on it - just copy and paste this code into it and run it

    Dim dsEmptyDatasetSP As New DataSet
    Dim dtOriginalDataTable As DataTable

    Private Function makeTable() As DataTable

        Dim result As New DataTable
        For i As Integer = 0 To 1
            Dim dc As New DataColumn
            dc.ColumnName = "Col" & i.ToString
            dc.DataType = GetType(Integer)
            result.Columns.Add(dc)
        Next
        For i As Integer = 0 To 5
            Dim dr As DataRow = result.NewRow
            dr(0) = i
            result.Rows.Add(dr)
        Next
        Return result

    End Function

    Private Sub CopyDataTable_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim msg As String
        dsEmptyDatasetSP.Tables.Add(makeTable) 'make a table for (0) in the dataset
        dtOriginalDataTable = dsEmptyDatasetSP.Tables(0).Copy 'copy that table
        For Each dr As DataRow In dsEmptyDatasetSP.Tables(0).Rows 'amend the table in the dataset
            dr(0) = dr(0) + 10
        Next
        For i As Integer = 0 To 5 'show results for the two tables
            msg &= "dtOriginalDataTable = " & dtOriginalDataTable.Rows(i).Item(0) & vbTab
            msg &= "dsEmptyDatasetSP.Tables(0) = " & dsEmptyDatasetSP.Tables(0).Rows(i).Item(0) & vbCrLf
        Next
        MsgBox(msg)

    End Sub

The first block of code is just to make a new table with dummy data in it.  The second block of code - which runs on form load - gets that table and puts it in a dataset.  Then it copies that table from that dataset to another datatable.  Then it amends just the table in the dataset.  Then it prints out the values from both the tables.  For me, the table which was copied (I used the same names as you do for this purpose, so it's dtOriginalDataTable) still contains the old values whereas the old version, in the dataset, which was altered - that is dsEmptyDatasetSP.Tables(0) - contains the new values.

The example is as simple as possible deliberately to illustrate that - at least at this simple level - what you say you are seeing just doesn't happen (for me, anyway).  So I must assume that there is something else in your code (which obviously won't be as simple as this) which is causing the effect.  But I don't know what it is.  

How, when you "want see or compare what I started with", do you do it?  Is the line/s which copies the table being called for a second time (obviously it would not be intentional) - after the alterations have been made and before you compare the tables?  Are you actually "saving" either or both of the datatables to a database and is how you are doing that producing the effect?  Or what?

I'd like to help but, unless/until I can identify why "the records are saved to other datatable, too" - when so far as I can see it shouldn't happen with the code you've shown and the description you've given - I'm afraid I can't.  If nothing I've said above gives you sufficient of a clue to identify the problem, it might be as well if you posted so much of your code as you think might be relevant to the issue.

Roger
0
 
Midnight2005Author Commented:
Thanks very much for all the help Roger.

When I ran your application, I saw there had to be something.

I appreciate your perfect example...Cannot be better.
Thanks for all the help again and have a great day.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now