How to copy/clone a DataTable

I have a DataTable that will be modified by the user. I want to identify what's been change an keep record of that in the database.

The best way I see of doing this is by copying that DataTable before any changes are made and verify what's been changed when the user saves the form.

The problem is that wether I use myDataTable.Clone() or myDataTable.Copy(), the data are always the same. So I guess they just create references between the tables.

How would you handle this?

Answers


Try the DataSet.HasChanges method. This will tell you if there have been any changes to the dataset such as deleted or added rows, modified rows, etc.

You can also call DataSet.GetChanges to see what has changed. This method will return a copy of the changes.

For example, you could say:

private void VerifyChanges(DataSet dataSet)
{
    if(!dataSet.HasChanges(DataRowState.Modified)) return;
    var changedDataSet = dataSet.GetChanges(DataRowState.Modified);

    //... do the tracking or whatever else you want here.        
}

Or if you don't want to get all the DataSet changes, and just want the changes from a specific table:

private void VerifyChanges(DataSet dataSet, string tableName)
{
    if(!dataSet.HasChanges(DataRowState.Modified)) return;
    var changedTable = dataSet
        .Tables[tableName]
        .GetChanges(DataRowState.Modified);

    //... do the tracking or whatever else you want here.        
}

Lastly, to pull the original values out of the modified DataSet you could code:

row[columnIndex, DataRowVersion.Original]

Need Your Help

SpanNearQuery does not work

c# lucene lucene.net

For some reason, I am unable to get the SpanNearQuery to work. I've tried debugging for the last 3 hours but in vain. Can someone tell me if I'm doing something stupid? Here's my attempt so far:

Grails datepicker

grails

Using the grails datepicker control, is there a way to have it save time precision, but only show the user up to the day? The user only cares about that day, but I need to add the time of day that...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.