Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between RowState.Added and DataRowVersion.Original

I have a function with 3 conditions:

  1. If DataRow rowstate == Added
  2. If !DataRow HasVersion(DataRowVersion.Original)
  3. If DataRow rowstate == modified

My problem is with the second one, can anyone shine some light on it? How is it different than the first condition?

like image 471
Amit Toren Avatar asked Feb 13 '18 17:02

Amit Toren


People also ask

How do I change the RowState of a DataRow?

You can use the . SetAdded() method to change the RowState, like this: da. Fill(dt); foreach (DataRow row in dt.

What is Row state?

A row state indicates the status of a row; row versions maintain the values stored in a row as it is modified, including current, original, and default values.


1 Answers

Explanation

  1. A DataRow has the RowState "Added", when it is newly added to the Table After calling AcceptChanges() the RowState will be set to "Unchanged"
  2. A DataRow has the DataRowVersion "Original", when it contains it's original values. When calling AcceptChanges() on the DataRow, or DataTable the DataRowVersion will be set to "Original". You could say that original means all changes have been accepted.
  3. A DataRow has the RowState "Modified" after it has been edited.

Example Program

I have created a small example program, which shows the changes in action, to clarify the differences.

class Program {
    static void Main(string[] args) {
        var table = new DataTable("MyTable");
        table.Columns.Add(new DataColumn("MyColumn"));
        var row = table.NewRow();
        Console.WriteLine($"Rowstate: {row.RowState}"); //Prints Detached

        table.Rows.Add(row);
        Console.WriteLine($"Rowstate: {row.RowState}"); //Prints Added

        table.AcceptChanges();
        Console.WriteLine($"Rowstate: {row.RowState}"); //Prints Unchanged

        row.BeginEdit();
        row[0] = "NewValue";
        row.EndEdit();
        Console.WriteLine($"Rowstate: {row.RowState}"); //Prints Modified

        if (row.HasVersion(DataRowVersion.Current)) { // Does the row contain uncommited values?
            Console.WriteLine($"DataRowVersion: {DataRowVersion.Current}"); //Prints Current
        }

        table.AcceptChanges(); //Commit all DataRowChanges
        if (row.HasVersion(DataRowVersion.Original)) {
            Console.WriteLine($"DataRowVersion: {DataRowVersion.Original}"); //Prints Current
        }

        Console.ReadLine();
    }
}

Further Reading

The msdn documentation about DataRowStates is actually pretty well explained. It provides a short explanation about every single state aswell as some example code. Thats the same for DataRowVersions. You should definetly have a look at these 2 articles.

DataRowVersion

Quote from linked MSDN article:

After calling the DataRow object's BeginEdit method, if you change the value, the Current and Proposed values become available.

After calling the DataRow object's CancelEdit method, the Proposed value is deleted.

After calling the DataRow object's EndEdit method, the Proposed value becomes the Current value.

After calling the DataRow object's AcceptChanges method, the Original value becomes identical to the Current value.

After calling the DataTable object's AcceptChanges method, the Original value becomes identical to the Current value.

After calling the DataRow object's RejectChanges method, the Proposed value is discarded, and the version becomes Current.

like image 58
Tobias Theel Avatar answered Oct 07 '22 15:10

Tobias Theel