开发者

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).

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 s开发者_如何学编程ome similar aproach. Thanks.


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.


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

Might work in your situation.


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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜