ado.net dirty records in dataset

为君一笑 提交于 2019-12-25 04:36:17

问题


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.


回答1:


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.




回答2:


DataRow.CancelEdit()
Or
DataRow.RejectChanges()
Or
DataSet.RejectChanges()

Might work in your situation.




回答3:


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.




回答4:


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++;
            }
        }


来源:https://stackoverflow.com/questions/2083193/ado-net-dirty-records-in-dataset

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!