ado.net dirty records in dataset

2019-08-17 18:05发布

I'll try to explain. I want to track dirty records in my dataset row that is bound to controls on window (wpf).

It works fine. Lets say i begin to edit some textbox that is bound to dataTable in that dataSet. 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.

Is there any method i need to call so dataset can recompute dirty records from binding fields, or some similar aproach. Thanks.

4条回答
做个烂人
2楼-- · 2019-08-17 18:38

You need to keep a copy of the original entity set to compare against, and make the "IsDirty" determination at the point you actually need to know whether it's dirty, not at the point the data is changed and therefore only might be dirty.

查看更多
祖国的老花朵
3楼-- · 2019-08-17 18:38

You can check the datarow's rowstate property and if Modified then compare the values in the Current and Original 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. 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.

查看更多
Animai°情兽
4楼-- · 2019-08-17 18:41

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++;
            }
        }
查看更多
倾城 Initia
5楼-- · 2019-08-17 18:48
登录 后发表回答