Formatting a Column as Currency
You can easily customize the way a column is formatted using the Format property. If you'd like to format the column as currency using templates instead, you can see the Formatting a Column as Currency Using Templates topic for details. The following steps assume that you've bound the grid to the Products table of the C1NWind.mdb database (see Binding Grid for WPF to a Database for more information). In this example, you'll format the UnitPrice column as currency at design time, in XAML, or in code.
Note: In the example below, the longer Format="{}{0:C}" format string was used for context. You can also use the briefer form, Format="C", with the same results.
At Design Time
To format the UnitPrice column as currency at design time, complete the following steps:
1. Click once on the C1DataGrid control to select it.
2. Navigate to the Properties window and click the ellipsis button next to the Columns collection. This will open the Column Collection Editor which will enable you to create column definitions. Note that these steps assume that no columns have been previously defined.
3. In the Column Collection Editor, click the Add another item button to create a new column definition. A new column will appear in the left-side Items pane.
4. In the right-side Properties pane, locate the PropertyName property in the Behavior tab and set it to "UnitPrice".
5. Scroll down to the Format property in the Miscellaneous tab, and set it to "{0:C}".
In XAML
To format the UnitPrice column as currency at design time add Format="{}{0:C}" to the <c1grid:C1DataGrid.Columns> tag so that it appears similar to the following:
<c1grid:C1DataGrid Name="c1DataGrid1" ItemsSource="{Binding Path=ProductsDataSet.Products, ElementName=Window, Mode=Default}" />
<c1grid:C1DataGrid.Columns>
<c1grid:Column PropertyName="UnitPrice" Caption="Price" HeaderCellWidth="60" Format="{}{0:C}"/>
</c1grid:C1DataGrid.Columns>
</c1grid:C1DataGrid>
In Code
To format the UnitPrice column as currency, add the following code to your project:
' Add a new column
Dim unitPrice As New Column
' Add the column to the grid and set properties
C1DataGrid1.Columns.Add(unitPrice)
unitPrice.PropertyName = "UnitPrice"
unitPrice.Caption = "Price"
' Format the column as currency
unitPrice.Format = "{0:C}"
• C#
// Add a new column
Column unitPrice = new Column();
// Add the column to the grid and set properties
c1DataGrid1.Columns.Add(unitPrice);
unitPrice.PropertyName = "UnitPrice";
unitPrice.Caption = "Price";
// Format the column as currency
unitPrice.Format = "{0:C}";
Run your project and observe:
The UnitPrice column will appear as currency:
|