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
767 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
Comment Utility
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
Comment Utility
I think so but that seems mighty complicated.
0
 
LVL 29

Accepted Solution

by:
Gautham Janardhan earned 500 total points
Comment Utility
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
Comment Utility
you can bind CostList  to the itemsource of the items control you are using instead of ObservableCollection...
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

763 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

6 Experts available now in Live!

Get 1:1 Help Now