How do you format numeric values ?

Jan 26, 2009 at 8:08 PM
How do you format a numeric column to display , and decimals?

Jan 26, 2009 at 9:54 PM

This is what I have found:




<toolkit:DataGridTextColumn Header="Date" Binding="{Binding Path=Date,



Converter={StaticResource formatter}, ConverterParameter=' \{0:dd-MMM-yyyy\}'}"/>




<toolkit:DataGridTextColumn Header="Amount" Binding="{Binding Path=Amount,



Converter={StaticResource formatter}, ConverterParameter='\{0:C\}'}"/>

For a decimal replace 0:C with 0:F? where ? is the number of decimal places requried, P for percentage etc.








<local:FormattingConverter x:Key="formatter"/>







public class FormattingConverter : IValueConverter




public object Convert(object value, Type targetType,



object parameter, CultureInfo culture)




string formatString = string.Empty;



if (parameter != null)



formatString = parameter

as string;




if (value is DateTime && formatString == null)



formatString =





if (!string.IsNullOrEmpty(formatString))




return string.Format(culture, formatString, value);








return value.ToString();





public object ConvertBack(object value, Type targetType,



object parameter, System.Globalization.CultureInfo culture)




// not implemented yet



return null;




Jan 27, 2009 at 3:52 PM
In .Net 3.5 SP1 you can use the StringFormat in the binding, now.

Here are a couple of columns in one of my datagrids:
                <toolkit:DataGridTextColumn Binding="{Binding Reading.ReadingDate, StringFormat={}{0:d}}" Header="Read Date" />
                <toolkit:DataGridTextColumn Binding="{Binding Reading.ReadingValue, StringFormat={}{0:0}}" Header="Reading(kgal)"/>

You can get more details here: