简体   繁体   English

检测数据集/数据表中的行何时发生更改

[英]Detecting when a row has changed in a dataset/datatable

I'm trying to add some extra logging to my C# winforms application. 我正在尝试为我的C#winforms应用程序添加一些额外的日志记录。 I have a few data bound forms that all the database stuff is managed by a binding source and some typed datasets/adapters. 我有一些数据绑定表单,所有数据库的东西都由绑定源和一些类型化的数据集/适配器管理。

With this setup, it's kind of difficult to tell when something is changed, I'd have to manage each field and keep it's previous value. 通过这种设置,当某些内容发生变化时很难判断,我必须管理每个字段并保持其之前的值。 Is there a way I can hook into the dataset and tell when something is changed? 有没有办法可以挂钩数据集并告诉什么时候有什么变化? I know datarow's have a RowState enumeration, would that be a good place to start? 我知道datarow有一个RowState枚举,这是一个很好的起点吗? I looked into the binding source's DataMemberChanged event but it never fired so... 我查看了绑定源的DataMemberChanged事件,但它从未解雇过......

要获取更新的数据值的原始值,您可以执行以下操作:

<DataTableRow>[<DataColumn>, DataRowVersion.Original]

To know what has changed look at the DataSet.GetChanges method . 要了解更改的内容,请查看DataSet.GetChanges方法 The example shows how to get the changes and go through them. 该示例显示了如何获取更改并完成更改。 I also have an old example here that uses a DataTable and shows how to do a comparison after a merge. 在这里也有一个旧示例,它使用DataTable并显示如何在合并后进行比较。 It inspects the RowState and shows the changed values etc. It's near the bottom of the page and it's in VB since the OP was using that, not C#. 它检查RowState并显示更改的值等。它靠近页面的底部,它在VB中,因为OP使用它,而不是C#。 I'm headed out now so I can't provide an equivalent translation but it should be pretty straightforward to glean some useful techniques from. 我现在要出去了,所以我不能提供相同的翻译,但是收集一些有用的技术应该非常简单。

您可以使用RowChanged事件, RowChanging事件或DataTable引发的任何其他事件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM