6

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?

Tobias Theel
  • 3,088
  • 2
  • 25
  • 45
Amit Toren
  • 353
  • 5
  • 13
  • So... you want someone to do your homework? Why don't you google what `Added` means, what `HasVersion` does and what `DataRowVersion.Original` returns? – Camilo Terevinto Feb 13 '18 at 17:28
  • I searched and didn't quite understand what HasVersion(DataRowVesrion.Original) means compared to the first condition. It seems to me they do the same thing, but it's too important to just assume that, so I came here. Didn't see any need to explain the whole story. – Amit Toren Feb 13 '18 at 17:32
  • 1
    I edited the question so you might understand what quesrion I'm asking – Amit Toren Feb 13 '18 at 17:42

2 Answers2

6

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.

Tobias Theel
  • 3,088
  • 2
  • 25
  • 45
0

Row state(Added, Deleted ..) Row Version(Original,Current,Proposed)

The update method using the Row Version to decided What changes to apply to the database. If we call the AcceptChanges method on the TableAdapter Before calling the Update Method: Current Version will be the Original Verion So you should not call The ACCEPTCHANGES method before calling Update Method

Bonny
  • 511
  • 5
  • 12