Skip to main content

DiffViewer

Wisej.NET 4.2.0+

A side-by-side comparison of two versions of a sales opportunity, with revert buttons and the unchanged-fields toggle.

A DiffViewer compares two versions of the same record — field by field, not line by line — and shows what changed: the field name, the old value struck through, an arrow, and the new value. A header counts the changes and a footer carries a caption, such as who made the change and when, and a button that shows or hides the fields that did not change.

Use it wherever users need to see an edit rather than the result of one: the history tab of a customer record, an approval step that has to confirm what is being approved, a "review your changes" step before saving, a conflict between the user's copy and the one saved in the meantime. Changed fields can offer a revert button, which raises an event and leaves the decision — and the records — to your code.

info

For a complete list of properties, methods, and events, see the API documentation.

Features​

Records and Fields​

OldValue and NewValue take the two records. Each can be any object with readable properties, an IDictionary with string keys, or a Dictionary<string, object> — the two do not even have to be of the same type, since fields are matched by name (exactly, case-sensitively). Values are compared with object.Equals, without conversion and without looking inside nested objects. A field that one record has and the other does not counts as changed, and is shown as "(missing)" rather than as a null.

With Fields left empty, the viewer compares every browsable property or key of both records, in alphabetical order. Add DiffField objects to choose the fields, their order and their presentation: Name is the property or key, Text the label shown for it, and Format a composite format such as {0:C0} or {0:d}, applied with the current culture. IgnoreFields lists names to leave out in either case — internal ids, timestamps, row versions.

The viewer does not watch the records. After changing either one in place, call RefreshComparison(); assigning a new record refreshes on its own.

Layout​

Mode is SideBySide (the default), which lines the old and new values up in two columns, or Inline, which places them together under each field label and suits a narrow panel. The control's Text is the heading, next to the change count; Caption is the footer line.

Unchanged Fields​

ShowUnchanged (on by default) shows the fields that are equal in both records, marked as unchanged, which gives the changes their context. The footer has a "Show unchanged" / "Hide unchanged" button that flips the property for the user. In a long record, start with it off.

Revert Requests​

With AllowRevert set to true, each changed field gets a revert button, unless its DiffField sets AllowRevert to false. Clicking it raises RevertClick. Neither record is modified: apply the old value yourself, if the change should really be undone, and refresh.

FieldClick is raised when a field label is clicked, for example to jump to the field in the editor. Both events carry a DiffFieldEventArgs with the Field, the raw OldValue and NewValue, HasOldValue and HasNewValue to tell a missing field from a null one, and IsChanged.

Localized Text​

The fixed captions — "(missing)", "(null)", "unchanged", "Revert", "N changes", "Show unchanged (N)" and "Hide unchanged (N)" — are translated for the current UI culture. MissingText, NullText, UnchangedText, RevertText, ChangesText, ShowText and HideText override them; the last three are composite formats whose {0} is the count.

How To​

Show the History of a Record​

Choose and format the fields, and let the caption say who changed them.

this.diffViewer1.Text = "Opportunity OPP-2291";
this.diffViewer1.Caption = $"Edited by {entry.UserName} · {entry.Timestamp:g}";
this.diffViewer1.ShowUnchanged = false;

this.diffViewer1.Fields.Add(new DiffField { Name = "Stage" });
this.diffViewer1.Fields.Add(new DiffField { Name = "Amount", Format = "{0:C0}" });
this.diffViewer1.Fields.Add(new DiffField { Name = "CloseDate", Text = "Close date", Format = "{0:d}" });
this.diffViewer1.Fields.Add(new DiffField { Name = "Owner" });

this.diffViewer1.OldValue = entry.Before;
this.diffViewer1.NewValue = entry.After;

Review Changes Before Saving​

Compare the loaded record with the edited copy, ignoring the bookkeeping fields, and offer a revert per field.

var original = customerService.Load(customerId);
var edited = this.customerBindingSource.Current as Customer;

this.diffViewer1.IgnoreFields = new[] { "Id", "RowVersion", "ModifiedOn" };
this.diffViewer1.AllowRevert = true;
this.diffViewer1.OldValue = original;
this.diffViewer1.NewValue = edited;

Apply a Revert​

RevertClick only asks. With dictionary records, writing the old value back is one line; then refresh the comparison.

this.diffViewer1.RevertClick += (sender, e) =>
{
var draft = (Dictionary<string, object>)this.diffViewer1.NewValue;

if (e.HasOldValue)
draft[e.Field.Name] = e.OldValue;
else
draft.Remove(e.Field.Name);

this.diffViewer1.RefreshComparison();
};

Advanced​

JavaScript Widget​

ItemDescription
Class name"wisej.web.DiffViewer"
Theme appearance"diffviewer", see Themes. The parts are themed as components: "header", "title", "count", "row", "field", "old", "new", "value", "arrow", "muted", "revert", "footer" and "toggle".