Refreshing UI Data Without Losing Edits – Quick Start

Many applications need to refresh data from the server while the user is actively editing. POCO DataSet supports this scenario by using IObservableDataSet.DoRefreshMergePreservingLocalChanges merge: refreshed server values are applied to clean rows, while local edits remain untouched.

When to use merge which preserves local changes

What merge, which preserves local changes, guarantees

Core workflow

  1. Keep a UI-bound observabl edataset in memory.
  2. Request a refreshed dataset snapshot from the server.
  3. Merge the refreshed snapshot into the current dataset by using IObservableDataSet.DoRefreshMergePreservingLocalChanges method.
  4. UI updates for clean rows; dirty rows remain dirty and unchanged.

Minimal working example

Assuming observable data set is used for UI binding, for example:

// 1. Create observable dataset
IObservableDataSet boundObservableDataSet = new ObservableDataSet();

// 2. Add Department table to the observable dataset
IObservableDataTable departmentObservableDataTable = boundObservableDataSet.AddNewTable("Department");
departmentObservableDataTable.AddColumn("Id", DataTypeNames.INT32);
departmentObservableDataTable.AddColumn("Name", DataTypeNames.STRING);

// 3. Add a row to the Department table (data row state is Added)
IObservableDataRow departmentObservableDataRow = departmentObservableDataTable.AddNewRow();
departmentObservableDataRow["Id"] = 1;
departmentObservableDataRow["Name"] = "Sales";

// 4. Add another row to the Department table, the values of which user hadn't cahnged (data row state is Unchanged)
IObservableDataRow departmentObservableDataRow = departmentObservableDataTable.AddNewRow();
departmentObservableDataRow["Id"] = 2;
departmentObservableDataRow["Name"] = "Emergency";
departmentObservableDataRow.AcceptChanges();
// The following line is a pseudo-code example of how UI can be bound to the observable data set.
// The actual binding code depends on the UI framework and is not part of POCO DataSet.
somePage.DataContext = boundObservableDataSet;

Assuming user clicked on Refresh button asking to refresh bound data. The following code imitates the server response.

// 1. Create refreshed dataset
IDataSet refreshedDataSet = DataSetFactory.CreateDataSet();

// 2. Add Department table to the refreshed dataset
IDataTable departmentRefreshedDataTable = refreshedDataSet.AddNewTable("Department");
departmentRefreshedDataTable.AddColumn("Id", DataTypeNames.INT32);
departmentRefreshedDataTable.AddColumn("Name", DataTypeNames.STRING);

// 3. Add two rows to the Department table which imitate changed data in the database
IDataRow departmentRefreshedDataRow = departmentRefreshedDataTable.AddNewRow();
departmentRefreshedDataRow["Id"] = 1;
departmentRefreshedDataRow["Name"] = "Sales and Marketing";

departmentRefreshedDataRow = departmentRefreshedDataTable.AddNewRow();
departmentRefreshedDataRow["Id"] = 2;
departmentRefreshedDataRow["Name"] = "Emergency Department";

// 4. Mark all rows as Unchanged to indicate they are clean server values
refreshedDataSet.AcceptChanges();

Call IObservableDataSet.DoRefreshMergePreservingLocalChanges method to merge refreshed snapshot into the current dataset while preserving local changes:

IObservableMergeOptions observableMergeOptions = new ObservableMergeOptions();
boundObservableDataSet.DoRefreshMergePreservingLocalChanges(refreshedDataSet, MergeMode.Refresh);

// Observe that the name of the Sales department is not overwridden because the observable row state is Added
string nameOfSalesDepartment = (string)currentDepartment.Rows[0]["Name"];

// Observe that the name of the Emergency department is updated to "Emergency Department" because the observable row state is Unchanged
string nameOfEmergencyDepartment = (string)currentDepartment.Rows[1]["Name"];

 

Table of Content POCO DataSet Quick Start

 


Business Process Programming in .Net
© 2004–2026 Laskarzhevsky Software Inc.
Unless otherwise noted, the content of this website is licensed under the Creative Commons Attribution 4.0 International License (CC BY 4.0).
Code examples are provided under the MIT License.
You are free to share and adapt the material provided that appropriate credit is given and any modifications are clearly indicated.
The information provided on this website is for educational purposes only.
The author and publisher make no warranties regarding the completeness or suitability of the information and are not responsible for any damages resulting from its use.