Thursday 7 January 2010

The skitzofrenic DataRow Versions.

The DataRow contains several different versions (DataRowVersion) of the data it contains and several different RowStates, and of top of that it can be in edit mode or not. This can be a bit complicated grasp, in the following I will try to explain to myself (and any other reader) how it works.

The RowState is a description of the rows connection to a DataTable and whether the data is changed.

The different DataRowVersion contains the different versions of the data, and can either be: Current, Original, Proposed or a pointer to one of the three (it is usually denoted as the Default).

And finally a DataRow can be in Edit Mode or not. A DataRow enters and exits this mode when BeginEdit and EndEdit is invoked respectively.

The lifetime of a DataRow can pass through these RowStates:

  1. Detatched: The DataRow is created but not attached to any DataTable.
  2. Added: The RowState goes to Added when it is added to a DataTable.
  3. Unchanged: If the AcceptChanges method is invoked, the RowState becomes Unchanged.
  4. Modified: If the data is modified the RowState becomes Modified, this is however not true if the current RowState is Added.
  5. Deleted: When the Delete method is invoked.


Strangely enough the DataRowVersions doesnt really have anything to do with the RowState, but whether the DataRow is in Edit Mode or not(or well in RowState Deleted.

Before an Edit the default data in the row is found in Current version. In edit mode the default data will be in the Proposed version, while the current version contains the unedited version.
After an Edit the Original version contains the data from before the edit, while the Current version contains the hmm... current version (in other words the Proposed version in Edit mode).