Solved

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

Posted on 2010-09-16
4
770 Views
Last Modified: 2013-12-16
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
Comment
Question by:endrec
  • 2
4 Comments
 
LVL 8

Expert Comment

by:adler77
ID: 33697498
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
 

Author Comment

by:endrec
ID: 33697523
I think so but that seems mighty complicated.
0
 
LVL 29

Accepted Solution

by:
Gautham Janardhan earned 500 total points
ID: 33700106
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
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 33700118
you can bind CostList  to the itemsource of the items control you are using instead of ObservableCollection...
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now