DataGrid calculate difference between values in two databound cells

Posted by justMe on Stack Overflow See other posts from Stack Overflow or by justMe
Published on 2011-02-14T20:32:04Z Indexed on 2011/02/14 23:25 UTC
Read the original article Hit count: 301

Filed under:
|
|
|
|

Hello!

In my small application I have a DataGrid (see screenshot) that's bound to a list of Measurement objects. A Measurement is just a data container with two properties: Date and CounterGas (float). Each Measurement object represents my gas consumption at a specific date.

enter image description here

The list of measurements is bound to the DataGrid as follows:

    <DataGrid ItemsSource="{Binding Path=Measurements}" AutoGenerateColumns="False">
        <DataGrid.Columns>
             <DataGridTextColumn Header="Date" Binding="{Binding Path=Date, StringFormat={}{0:dd.MM.yyyy}}" />
             <DataGridTextColumn Header="Counter Gas" Binding="{Binding Path=ValueGas, StringFormat={}{0:F3}}" />
        </DataGrid.Columns>
    </DataGrid>

Well, and now my question :) I'd like to have another column right next to the column "Counter Gas" which shows the difference between the actual counter value and the last counter value.

E.g. this additional column should calculate the difference between the value of of Feb. 13th and Feb. 6th => 199.789 - 187.115 = 15.674

What is the best way to achieve this? I'd like to avoid any calculation in the Measurement class which should just hold the data. I'd rather more like the DataGrid to handle the calculation. So is there a way to add another column that just calculates the difference between to values? Maybe using some kind of converter and extreme binding? ;D

P.S.: Maybe someone with a better reputation could embed the screenshot. Thanks :)

© Stack Overflow or respective owner

Related posts about wpf

Related posts about datagrid