How to copy / clone DataTable

I have a DataTable that will be modified by the user. I want to determine what has changed, save the record in the database.

The best way I see this done is to copy this DataTable before making any changes and check what was changed when the user saves the form.

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

How would you handle this?

+3
source share
3 answers

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

DataSet.GetChanges, , . .

, :

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.        
}

, DataSet :

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.        
}

, DataSet, :

row[columnIndex, DataRowVersion.Original]
+3

IMO, serialize, deserialize DataTable. XML, DataTable DataSet.

0

Consider using the DataTable.GetChanges (DataRowState) method . This method returns a new data table with rows that have changed.

Example:

DataTable changeTable = table.GetChanges(DataRowState.Modified);

Using this table, you can compare rows one by one with another DataTable.

0
source

All Articles