tags:

views:

11

answers:

0

This is quite a tricky problem to explain, please bear with me!

I have 2 NumericUpDown controls called ItemsPerPerson and TotalItems (as grid columns, but this may not be relevant). There is a variable NumberOfPeople (lets keep it at a constant 10 for simplicity's sake).

TotalItems = ItemsPerPerson * NumberOfPeople (this is defined in the class Get accessor like this:

    public int TotalItems 
    {
        get { return itemsPerPerson * numberOfPeople ; }
    }

I am using 2 way databinding on these 2 textboxes, so that when you update ItemsPerPerson then TotalItems automatically updates - this works well. However, now I want it to work backwards - when you adjust TotalItems I want ItemsPerPerson to adjust accordingly (ie. TotalItems / NumberOfPeople).

To do this, I use the NumericUpDown_ValueChanged Event on TotalItems and do the calculation and set the ItemsPerPerson to the correct value (newTotalItems / NumberOfPeople) .

The problem is that this then triggers the event again as the total depends on the ItemsPerPerson which raises it's DataChanged event - it then goes into an infinite loop and all sorts of nasty things happen.

It's a bit hard to explain, but I think my question is - How do you bind 2 values in a datagrid that are dependent on each other and are both editable?