我有一个包含 3 个条件的函数:
- 如果
DataRow rowstate == Added
- 如果
!DataRow HasVersion(DataRowVersion.Original)
- 如果
DataRow rowstate == 已修改
我的问题是第二个,任何人都可以照亮它吗? 它与第一个条件有何不同?
最佳答案
说明
- DataRow 在新添加到
Table 在调用
AcceptChanges()
之后,RowState 将设置为 “不变” - DataRow 的 DataRowVersion 为“Original”,当它包含
原始值。在 DataRow 或 DataTable 上调用
AcceptChanges()
时,DataRowVersion 将设置为“Original”。您可以说原始意味着所有更改都已被接受。 - DataRow 在编辑后具有“已修改”的 RowState。
示例程序
我创建了一个小示例程序,它显示了操作的变化,以阐明差异。
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();
}
}
进一步阅读
关于DataRowStates的msdn文档实际上很好解释。它提供了关于每个状态的简短解释以及一些示例代码。 DataRowVersions 也是一样的.您绝对应该看看这两篇文章。
数据行版本
引用自链接的 MSDN 文章:
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.
关于c# - RowState.Added 和 DataRowVersion.Original 之间有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48772333/