Formatting a Column as Currency Using Templates
In this topic you'll learn how to use templates to format a column using templates. Note that you can also more easily format the column's appearance using the Format property. For more information, see the Formatting a Column as Currency topic.
In the following steps you'll create a custom content template that uses an IValueConverter to format the appearance of a column as currency and assign the template to a column's ItemCellShowContentTemplate property. By following the steps outlined below, you'll set the UnitPrice column in a grid so that values in the column are formatted as currency. This topic assumes that you have completed steps 1 and 2 of the Grid for WPF Quick Start and have bound the C1DataGrid to the C1NWind.mdb database.
Complete the following steps:
1. Open your project in Visual Studio 2008, right-click the window, and select View Code to open the Code Editor.
2. Add the following import statements to the top of the Window1.xaml.cs (or Window1.xaml.vb) file:
Imports System.Globalization
• C#
using System.Globalization;
3. Add the following code to the project to create the IValueConverter that transform the current value of a column to currency:
Public Class CurrencyConverter
Implements IValueConverter
Public Function Convert(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
If (Not value Is Nothing) AndAlso ((Not Object.Equals(String.Empty, value))) Then
Try
' This will convert the string value provided to a double value before formatting.
Dim tempD As Double = System.Convert.ToDouble(value, CultureInfo.CurrentCulture)
Return String.Format(CultureInfo.CurrentCulture, "{0:C}", tempD)
' Catch exceptions.
Catch e1 As FormatException
Catch e2 As OverflowException
End Try
End If
' Return the string value.
Return String.Format(CultureInfo.CurrentCulture, "{0}", value)
End Function
Public Function ConvertBack(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack
Return Double.Parse(TryCast(value, String), NumberStyles.Currency, CultureInfo.CurrentCulture)
End Function
End Class
• C#
[ValueConversion(typeof(double), typeof(string))]
public class CurrencyConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if ((value != null) && (!object.Equals(string.Empty, value)))
{
try
{
// This will convert the string value provided to a double value before formatting.
double tempD = System.Convert.ToDouble(value, CultureInfo.CurrentCulture);
// Return the string value.
return string.Format(CultureInfo.CurrentCulture, "{0:C}", tempD);
}
// Catch exceptions.
catch (FormatException)
{
}
catch (OverflowException)
{
}
}
return string.Format(CultureInfo.CurrentCulture, "{0}", value);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return double.Parse(value as string, NumberStyles.Currency, CultureInfo.CurrentCulture);
}
}
4. Add the local namespace to your project by adding xmlns:local="clr-namespace:ProjectName" to the <Window> tag, where ProjectName is the name of your project, so that it looks similar to the following:
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ProjectName"
xmlns:my="clr-namespace:C1.WPF.C1DataGrid;assembly=C1.WPF.C1DataGrid" x:Class="Window1"
Height="240" Width="411" x:Name="Window">
5. Create a new ControlTemplate resource in your project by adding the following XAML just below the <Window> tag:
<Window.Resources>
<local:CurrencyConverter x:Key="convertCurrency"/>
< ControlTemplate x:Key="ItemCellShowContentTemplate_currency">
<TextBlock Text="{Binding Value, Converter={StaticResource convertCurrency}}"/>
</ControlTemplate >
</Window.Resources>
6. Now add the following XAML within the <c1grid:C1DataGrid> tag to define the columns and call the ItemCellShowContentTemplate in the UnitPrice column:
<c1grid:C1DataGrid Name="c1DataGrid1" ItemsSource="{Binding Path=ProductsDataSet.Products, ElementName=Window, Mode=Default}" AutoGenerateColumns="False">
<c1grid:C1DataGrid.Columns>
<c1grid:Column PropertyName="ProductName" Caption="Name" HeaderCellWidth="150"/>
<c1grid:Column PropertyName="UnitPrice" Caption="Price" HeaderCellWidth="60" ItemCellShowContentTemplate="{StaticResource ItemCellShowContentTemplate_currency}"/>
<c1grid:Column PropertyName="QuantityPerUnit" Caption="Unit Size" HeaderCellWidth="125"/>
<c1grid:Column PropertyName="UnitsInStock" Caption="In Stock" HeaderCellWidth="75"/>
<c1grid:Column PropertyName="UnitsOnOrder" Caption="On Order" HeaderCellWidth="80"/>
<c1grid:Column PropertyName="ReorderLevel" Caption="Reorder Level" HeaderCellWidth="100"/>
<c1grid:Column PropertyName="Discontinued" Caption="Discontinued" HeaderCellWidth="95"/>
</c1grid:C1DataGrid.Columns>
</c1grid:C1DataGrid>
Run the project and observe:
The grid displays items in the UnitPrice column as currency:
|