CHAPTER 22 ADO.NET PART II: THE DISCONNECTED LAYER
Understanding the DataRowVersion Property
Beyond maintaining the current state of a row with the RowState property, a DataRow object maintains
three possible versions of the data it contains using the DataRowVersion property. When a DataRow object
is first constructed, it contains only a single copy of data, represented as the current version. However, as
you programmatically manipulate a DataRow object (using various method calls), additional versions of
the data spring to life. Specifically, you can set the DataRowVersion to any value of the related
DataRowVersion enumeration (see Table 22-6).
Table 22-6. Values of the DataRowVersion Enumeration
Value
Meaning in Life
Current
This represents the current value of a row, even after changes have been made.
Default
This is the default version of DataRowState. For a DataRowState value of Added,
Modified, or Deleted, the default version is Current. For a DataRowState value of
Detached, the version is Proposed.
Original
This represents the value first inserted into a DataRow or the value the last time
AcceptChanges() was called.
Proposed
This is the value of a row currently being edited due to a call to BeginEdit().
As suggested in Table 22-6, the value of the DataRowVersion property is dependent on the value of
the DataRowState property in many cases. As mentioned previously, the DataRowVersion property will be
changed behind the scenes when you invoke various methods on the DataRow (or, in some cases, the
DataTable) object. Here is a breakdown of the methods that can affect the value of a row’s
DataRowVersion property:
•
If you call the DataRow.BeginEdit() method and change the row’s value, the
Current and Proposed values become available.
•
If you call the DataRow.CancelEdit() method, the Proposed value is deleted.
•
After you call DataRow.EndEdit(), the Proposed value becomes the Current value.
•
After you call the DataRow.AcceptChanges() method, the Original value becomes
identical to the Current value. The same transformation occurs when you call
DataTable.AcceptChanges().
•
After you call DataRow.RejectChanges(), the Proposed value is discarded, and the
version becomes Current.
Yes, this is a bit convoluted, not least because a DataRow might or might not have all versions at any
given time (you’ll receive runtime exceptions if you attempt to obtain a row version that is not currently
tracked). Regardless of the complexity, given that the DataRow maintains three copies of data, it becomes
simple to build a front end that allows an end user to alter values, change his or her mind and roll back
values, or commit values permanently. You’ll see various examples of manipulating these methods over
the remainder of this chapter.
870