Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
772 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

837 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