Search code examples
wpfxamlwpfdatagrid

Translating numeric value in DataTable for editing without marking as Modified


I have a sql table which I read into a DataTable. One of the columns is a decimal proportion, but I would like value to be displayed as a percentage for user editing (i.e. multiplied by 100). Is there an easy way to perform this translation in a DataGrid (or somewhere else?) so that I don't have to iterate over every record to multiply by 100, display, then divide all by 100 at the end?


Solution

  • Not sure if that'd do for you but you could try setting the StringFormat - like in this one...

    <TextBox Text="{Binding Path=Percentage, StringFormat={}{0:P2}}"/>
    

    Format decimal for percentage values?
    http://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.100).aspx

    Note: this one actually works :) - and e.g. it translates the 0.8555 into 85.55% (add percent at will)

    EDIT:
    For a two way editing actually - you'd need a Converter.

    You could use this one here Two way percentage formatted binding in WPF