简体   繁体   English

ado.net数据集中的脏记录

[英]ado.net dirty records in dataset

I'll try to explain. 我会尽力解释。 I want to track dirty records in my dataset row that is bound to controls on window (wpf). 我想跟踪绑定到窗口(wpf)控件上的数据集行中的脏记录。

It works fine. 工作正常。 Lets say i begin to edit some textbox that is bound to dataTable in that dataSet. 可以说我开始编辑绑定到该dataSet中的dataTable的一些文本框。 After i add one character to textbox, dataset is marked dirty. 将一个字符添加到文本框后,数据集被标记为脏。

But if I delete that character again ( restore original value ), dataset is still dirty. 但是,如果我再次删除该字符(恢复原始值),则数据集仍然很脏。 I want after i restore original value to dataSet become not dirty, because in reallity it isn't dirty any more. 我想要在将原始值恢复为dataSet之后变得不脏,因为实际上它不再脏了。

Is there any method i need to call so dataset can recompute dirty records from binding fields, or some similar aproach. 我是否需要调用任何方法,以便数据集可以从绑定字段或某些类似方法中重新计算脏记录。 Thanks. 谢谢。

您需要保留原始实体集的副本以进行比较,并在实际需要知道是否脏的时候进行“ IsDirty”确定,而不是在数据更改时才做出“ IsDirty”确定,因此仅可能是脏的。

You can check the datarow's rowstate property and if Modified then compare the values in the Current and Original DataRowVersions. 您可以检查数据行的rowstate属性,如果已Modified则可以比较CurrentOriginal DataRowVersions中的值。 If your second change makes the value the same as the original then you could call RejectChanges , but that would reject ALL changes on that row. 如果第二次更改使值与原始值相同,则可以调用RejectChanges ,但这将拒绝该行上的所有更改。 You will have to manually track each field since the dataset only keeps per-row or per-table changes and any change is a change, even if you set the same value. 您将必须手动跟踪每个字段,因为数据集仅保留每行或每表更改,并且即使设置了相同的值,任何更改都是更改。

Well, got something working, just wanted to share. 好吧,有什么工作了,只想分享。 So far so good. 到现在为止还挺好。 Thanks everyone for answers, helped me alot. 谢谢大家的回答,对我有很大帮助。 Next step is building this functionality into custom control :). 下一步是将此功能构建到自定义控件中:)。

        private bool dirty = false;
        private int currentRowIndex;

        void Products_RowChanged(object sender, System.Data.DataRowChangeEventArgs e)
        {
           currentRowIndex=ProductsViewSource.View.CurrentPosition;
            int i=0;
            foreach (object o in originalProducts[currentRowIndex].ItemArray)
            {
                if (o.Equals(restouranDataSet.Products[currentRowIndex].ItemArray[i]))
                    dirty = false;
                else
                {
                    dirty = true;
                    return;
                }
                i++;
            }
        }

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

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