views:

37

answers:

1

Here's my current problem:

I have a data grid with 4 columns in it: Year One by Index, Year One by Percentage, Year Two+ by Index, and Year Two+ by Percentage. I want my data grid to make these columns mutually exclusive with its counterpart column.

So for example, if I type in a number for Year One by Percentage I shouldn't be allowed to enter anything for Year One by Index and vice versa. Same goes with the Year Two+ columns with each other.

I would be happy enough if instead of locking the column it would erase the other column's value (i.e. in the above example, instead of not being able to edit Year One by Index, make it so that if you did it it would erase the value from Year One by Percentage).

Any ideas?

EDIT: Here's what I've tried so far: I tried adjusting the "AllowEdit" field of the neighboring column when something gets change (this didn't do anything), and I tried clearing the value in the other column (also failed).

+1  A: 

If you are using some sort of MVVM pattern then in your viewmodel that represents the data for a row, you could do something like this:

public const string YearOneByIndexPropertyName = "YearOneByIndex";
public int YearOneByIndex
{
    get
    {
        return _yearOneByIndex;
    }

    set
    {
        if (_yearOneByIndex  == value)
        {
            return;
        }

        _yearOneByIndex = value;
        _yearOneByPercentage = 0

        RaisePropertyChanged(YearOneByIndexPropertyName);
        RaisePropertyChanged(YearOneByPercentagePropertyName);
    }
}

public const string YearOneByPercentagePropertyName = "YearOneByPercentage";
public int YearOneByPercentage
{
    get
    {
        return _yearOneByPercentage;
    }

    set
    {
        if (_yearOneByPercentage == value)
        {
            return;
        }

        _yearOneByPercentage = value;
        _yearOneByIndex = 0;

        RaisePropertyChanged(YearOneByIndexPropertyName);
        RaisePropertyChanged(YearOneByPercentagePropertyName);
    }
}

Edit: You could also add some boolean properties to bind to the IsReadOnly properties of each column and use the same technique to set the alternate column's one to true.

Edit: With some testing I have found that you must set datagrid column's binding UpdateSourceTrigger to PropertyChanged. Otherwise the setter code described above will not run until the user either presses the enter key or selects a different row.

PS: I'm using MVVM-Light which is where the RaisePropertyChanged comes from.

toomeke