Search code examples
c#wpfentity-frameworkwpfdatagridobservablecollection

Finding the items changed in Observable Collection


I have a class from a EF db context which I have displayed in a datagrid based on an ObservableCollection. The user can edit the the grid and this all displays fine.

However I now need to send the data back to the database. I do not want to send all the items in the collection to my save method, so can I find only the items that have been have change in the collection?


Solution

  • just as an idea (not professing this to be an ideal solution) i have run into a similar issue, looked around for potential solutions and none of those were exactly what i wanted.

    i had to pass a collection to WPF DataGrid and it seemed to complain about using List, hence i turned to ObservableCollection

    i did not want to work directly with the EF context for multiple reasons primarily because i wanted to grab items and pass them to intermediate transaction factory to be processed (business logic).

    so decided to stick with ObservableCollection and instead make slight modification to the ViewModel since this i was free to do it.

    my model ended up to look like this:

    internal class databaseItemModel
    {
        int _id; 
        string _description;
        decimal _price; 
        decimal _quantity;
        decimal _cost;
        bool _modified;
    
        public databaseItemModel()
        {
            _modified = false;
        }
    
        public int id { get { return _id; } }
        public bool modified { get { return _modified; } }
        public string description { get { return _description; } set { _description = value; _modified = true; } }
        public decimal price { get { return _price; } set { _price = value; _modified = true; } }
        public decimal quantity { get { return _quantity; } set { _quantity = value; _modified = true; } }
        public decimal cost { get { return _cost; } set { _cost = value; _modified = true; } }
        public bool selected { get; set; }
    
        public void setId(int _idvalue) 
        {
            _id = _idvalue;
        }
        public decimal value
        {
            get { return price * quantity; }
        }
        public void setDescription(string _descr)
        {
            _description = _descr; 
        }
        public void setPrice(decimal _pr)
        {
            _price = _pr;
        }
        public void setQuantity(decimal _qty)
        {
            _quantity = _qty;
        }
        public void setCost(decimal _cst) 
        {
            _cost = _cst;
        }
    }
    

    Basically, the plain idea behind it is that i would use functions to populate data rather than using properties direct and then pass the item to ObservableCollection which then would become the source for the DataGrid.ItemsSource

    since DataGrid/ObservableCollection would work with properties - modified objects would be marked as modified and i would then be able to pick up the collection on exit and collect the modified items.

    hope this is helpful.