I have a function with 3 conditions:
DataRow rowstate == Added
!DataRow HasVersion(DataRowVersion.Original)
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?
You can use the . SetAdded() method to change the RowState, like this: da. Fill(dt); foreach (DataRow row in dt.
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.
AcceptChanges()
the RowState will be set to
"Unchanged"AcceptChanges()
on the DataRow, or DataTable the DataRowVersion will be set to "Original". You could say that original means all changes have been accepted.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();
}
}
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With