[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 789
  • Last Modified:

How do I create an entity or observable collection the relies on previous "row's" data for calculated columns?

I'm having an extremely difficult problem (to me), where I have some data that relies on previous "row's" data to calculate correctly.  Usually calculations are simpler "row math" like _quantity * _cost = _total, but I need a way to have an ObservableCollection that has more complicated "multiple-row math" like  _previousRowQuantity + _currentRowQuantity * _currentRowCost where the _previousRowQuantity is more like _previousRowQuantity - _previousRowOtherValue.

Here is a simplified example class that is similar to my real class.  Usually I would just wrap this in ObservableCollection<TestModel> if I need to bind to in in WPF, but I'm not sure how to frame this problem and structure the functions and classes if I need one change in a "cell" of data to recalculate values in the entire ObservableCollection and have that reflected in the control that is binding to it (simpler OnPropertyChanged event notifications are not working).

    /// <summary>
    /// Represents a row
    /// </summary>
    public class TestModel : EntityBase
    {
        #region Private Members
        private string _key;
        private double _rollingQuantity;
        private double _calculatedValue;
        #endregion

        #region Public Properties
        public string Key
        {
            get { return _key; }
            set
            {
                _key = value;
                this.OnPropertyChanged("Key");
            }
        }
        public double RollingQuantity
        {
            get { return _rollingQuantity}
            set
            {
                _rollingQuantity = value;
                this.OnPropertyChanged("RollingQuantity");
            }
        }

        public double CalculatedValue
        {
            get { return _calculatedValue}
            set
            {
		//Needs to take the the previous "row's" value into account
		//Formula: _calculatedValue = previousRowCalculatedValue + currentRowRollingQuantity
		//If the row is the first row, then the formula should just be _calculatedValue = currentRowRollingQuantity
                _calculatedValue = value;
                this.OnPropertyChanged("CalculatedValue");
            }
        }
        #endregion 

    }

Open in new window

0
endrec
Asked:
endrec
  • 2
1 Solution
 
adler77Commented:
What if when you create your class you add forward and backwards references to the previous and next rows, sort of like a linked list? Then, if the data changes somewhere, it can use the forward reference to notify that instance that the current row has changed and it should recalculate, and then the backwards reference gives that instance the prior instance on which to derive its own calculations from? An instance could also use a collection of forward references in case you have several equally dependent rows tied to the current row.

Make sense? Would something like this work for your implementation?
0
 
endrecAuthor Commented:
I think so but that seems mighty complicated.
0
 
Gautham JanardhanCommented:
cool.. that was something diff.. try this code.
    public class CostItem : INotifyPropertyChanged
    {
        public Func<CostItem,int> GetPreviousItemCost { get; set; }
        public Action<CostItem,string> MyPropertyChanged { get; set; }

        private int _itemCost;
        public int ItemCost
        {
            get
            {
                return _itemCost;
            }
            set
            {
                _itemCost = value;
                OnPropertyChanged("ItemCost");
                if (MyPropertyChanged != null)
                {
                    MyPropertyChanged(this,"ItemCost");
                }
            }
        }

        private string _itemName;
        public string ItemName
        {
            get
            {
                return _itemName;
            }
            set
            {
                _itemName = value;
                OnPropertyChanged("ItemName");
            }
        }

        public int CalculatedCost
        {
            get
            {
                if (GetPreviousItemCost != null)
                {
                    return GetPreviousItemCost(this) * ItemCost;
                }
                else
                {
                    return 0;
                }
            }
        }


        #region INotifyPropertyChanged Members
        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }

        #endregion
        }
    }

    public class CostList : ObservableCollection<CostItem>
    {
        protected override void SetItem(int index, CostItem item)
        {
            base.SetItem(index, item);
        }

        public int GetPreviousItemCost(CostItem value)
        {
            int i = this.IndexOf(value);
            if (i > 0)
            {
                return this[i - 1].ItemCost;
            }
            else
            {
                return 1;
            }
        }

        public void CollectionPropertyChanged(CostItem value, string prop)
        {
            int i = this.IndexOf(value);
            if (i >= 0 && i != this.Count - 1)
            {
                this[i + 1].OnPropertyChanged("CalculatedCost");
            }
        }

        protected override void OnCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            foreach (var item in e.NewItems)
            {
                (item as CostItem).MyPropertyChanged = CollectionPropertyChanged;
                (item as CostItem).GetPreviousItemCost = GetPreviousItemCost;
            }
        }
    }

Open in new window

0
 
Gautham JanardhanCommented:
you can bind CostList  to the itemsource of the items control you are using instead of ObservableCollection...
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now