c# - RowState.Added 和 DataRowVersion.Original 之间有什么区别

标签 c# .net datatable datarow rowstate

我有一个包含 3 个条件的函数:

  1. 如果DataRow rowstate == Added
  2. 如果 !DataRow HasVersion(DataRowVersion.Original)
  3. 如果 DataRow rowstate == 已修改

我的问题是第二个,任何人都可以照亮它吗? 它与第一个条件有何不同?

最佳答案

说明

  1. DataRow 在新添加到 Table 在调用 AcceptChanges() 之后,RowState 将设置为 “不变
  2. DataRow 的 DataRowVersion 为“Original”,当它包含 原始值。在 DataRow 或 DataTable 上调用 AcceptChanges() 时,DataRowVersion 将设置为“Original”。您可以说原始意味着所有更改都已被接受。
  3. 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/

相关文章:

c# - Linq OrderBy 没有 100% 的时间正确排序

.net - 命名空间的 WSDL 仍在使用 "tempuri.org"

.net - 如何为 Amazon Simple Email Service 启用 TLS 1.1/1.2

javascript - 使用ajax服务器端数据表动态创建不同的按钮

c# - 无法将类型为 'System.DBNull' 的对象转换为类型 'System.String'

jsf - 在 h :dataTable 中使用 java.util.Map

c# - 如何随机化数组项,然后按所需百分比裁剪数组

c# - 如何检测新启动的浏览器进程是否加载页面失败?

c# - System.Timers.Timer在Azure辅助角色中徘徊

c# - EF entity.attach 不起作用