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?
Asked
Active
Viewed 96 times
0

Conrad
- 2,197
- 28
- 53
1 Answers
0
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

Community
- 1
- 1

NSGaga-mostly-inactive
- 14,052
- 3
- 41
- 51
-
that's a culture specific - see this one http://stackoverflow.com/a/6567122/417747 - but I think that's easier then to make a converter for your percentage type. This answers your original question - if you need more, close this one and open another if you'd need more help. – NSGaga-mostly-inactive Apr 05 '13 at 21:25
-
Actually, it doesn't solve my problem - if I use this formatter, the TextBox still treats the value as a decimal proportion. *I.e.* if I type in **33** (user wants to input 33%) and tab out of the box, the resulting value in the box is **3300 %** which would be incorrect. – Conrad Apr 08 '13 at 14:14
-
then you need a `Converter` - from decimal/double to string and back. – NSGaga-mostly-inactive Apr 08 '13 at 15:47
-
Actual you mentioned 'displayed', this is ok for displaying - but editing is a bit different. – NSGaga-mostly-inactive Apr 08 '13 at 15:48
-
The Converter is exactly what I was looking for - thanks for the pointer. – Conrad Apr 08 '13 at 21:18