Format Cell Values

  • 5 minutes to read

This topic describes approaches to formatting cell values.

Approach

Affects the Column Type

Affects Sorting / Filtering / Grouping

Supports Printing / Exporting

Works in Edit Mode

GridControl / GridColumn

CustomColumnDisplayText

no

yes *

yes (WYSIWYG mode only)

no

Unbound Columns

yes

yes *

yes

yes

Columns with Binding

yes

yes *

yes

yes

EditSettings

DisplayFormat

no

yes *

yes

no

DisplayTextConverter

no

yes *

yes

no

Masked Input

no

yes *

yes

yes

Cell Templates

DisplayFormatString

no

no

no

no

DisplayTextConverter

no

no

no

no

Masked Input

no

no

no

yes

* Except Server Mode and Virtual Sources

GridControl / GridColumn

The GridControl / GridColumn support the following ways to format cell values:

CustomColumnDisplayText

The GridControl.CustomColumnDisplayText and TreeListView.CustomColumnDisplayText events allow you to customize text within any cell.

<dxg:GridColumn FieldName="Value" /> 
void grid_CustomColumnDisplayText(object sender, DevExpress.Xpf.Grid.CustomColumnDisplayTextEventArgs e) {
    if (e.Column.FieldName == "Value")
        e.DisplayText = string.Format("{0:n2}", e.Value);
} 
  • Affects the column type: no
  • Affects sorting/filtering/grouping: yes (except Server Mode and Virtual Sources)
  • Supports printing/exporting: yes (WYSIWYG mode only)
  • Works in edit mode: no

Unbound Columns

The GridControl allows you to display Unbound Columns:

<dxg:GridColumn FieldName="ValueUnbound" UnboundType="String" /> 
private void grid_CustomUnboundColumnData(object sender, DevExpress.Xpf.Grid.GridColumnDataEventArgs e) {
    e.Value = string.Format("{0:n2}", e.GetListSourceFieldValue("Value"));
} 
  • Affects the column type: yes
  • Affects sorting/filtering/grouping: yes (except Server Mode and Virtual Sources)
  • Supports printing/exporting: yes
  • Works in edit mode: yes

Columns with Binding

You can use the ColumnBase.Binding property to associate a grid column with a data source's property. Refer to Columns with Binding for more information.

<dxg:GridColumn Binding="{Binding Value, Converter={StaticResource myConverter} }" />
  • Affects the column type: yes
  • Affects sorting/filtering/grouping: yes (except Server Mode and Virtual Sources)
  • Supports printing/exporting: yes
  • Works in edit mode: yes

EditSettings

You can use in-place data editors to edit cell values. Each editor has a helper class (the BaseEditSettings descendant) responsible for the editor's functionality. You can use the column's ColumnBase.EditSettings property to specify an in-place editor's settings.

DisplayFormat

The BaseEditSettings.DisplayFormat property specifies the pattern used to format the editor's display value.

For example, set the DisplayFormat property to c2 to display currency values. If the editor's value is 99.9, its formatted equivalent is $99.90.

<dxg:GridColumn EditSettings="{dxe:TextSettings DisplayFormat=c2}" FieldName="Value" /> 

Refer to the Format Specifiers topic for more information on the available DisplayFormat values.

  • Affects the column type: no
  • Affects sorting/filtering/grouping: yes (except Server Mode and Virtual Sources)
  • Supports printing/exporting: yes
  • Works in edit mode: no

DisplayTextConverter

The BaseEditSettings.DisplayTextConverter property specifies a converter that provides the editor's display value.

<dxg:GridColumn EditSettings="{dxe:TextSettings DisplayTextConverter={...}}" FieldName="Value" /> 
  • Affects the column type: no
  • Affects sorting/filtering/grouping: yes (except Server Mode and Virtual Sources)
  • Supports printing/exporting: yes
  • Works in edit mode: no

Masked Input

You can define an editor's mask settings using the properties with the Mask prefix (for example, the TextEditSettings.MaskType, TextEditSettings.Mask, TextEditSettings.MaskCulture, etc.).

Masks are used in edit mode. Set the TextEditSettings.MaskUseAsDisplayFormat property to true to use the specified mask in display mode. If this property is false, the BaseEditSettings.DisplayFormat format specifies an editor's display text.

Refer to Masked Input for more information.

<dxg:GridColumn EditSettings="{dxe:TextSettings Mask=c, MaskUseAsDisplayFormat=True, MaskType=Numeric}" FieldName="Value" /> 
  • Affects the column type: no
  • Affects sorting/filtering/grouping: yes (except Server Mode and Virtual Sources)
  • Supports printing/exporting: yes
  • Works in edit mode: yes

Cell Templates

The ColumnBase.CellTemplate / DataViewBase.CellTemplate properties specify Cell Templates that define the column cells' appearance.

TIP

You can use separate templates to present and edit data:

NOTE

Cell templates do not support Printing and Exporting. To print and export data with formatted cell values:

DisplayFormatString

The BaseEdit.DisplayFormatString property specifies the pattern used to format the editor's display value.

For example, set the DisplayFormatString property to c2 to display currency values. If the editor's value is 99.9, its formatted equivalent is $99.90.

<dxg:GridColumn FieldName="Value">
    <dxg:GridColumn.CellTemplate>
        <DataTemplate>
            <dxe:TextEdit Name="PART_Editor" DisplayFormatString="c2" />
        </DataTemplate>
    </dxg:GridColumn.CellTemplate>
</dxg:GridColumn>

Refer to the Format Specifiers topic for more information on available DisplayFormatString values.

  • Affects the column type: no
  • Affects sorting/filtering/grouping: no
  • Supports printing/exporting: no
  • Works in edit mode: no

DisplayTextConverter

The BaseEdit.DisplayTextConverter property specifies a converter that provides the editor's display value.

<dxg:GridColumn FieldName="Value">
    <dxg:GridColumn.CellTemplate>
        <DataTemplate>
            <dxe:TextEdit Name="PART_Editor" DisplayTextConverter="{...}" />
        </DataTemplate>
    </dxg:GridColumn.CellTemplate>
</dxg:GridColumn>
  • Affects the column type: no
  • Affects sorting/filtering/grouping: no
  • Supports printing/exporting: no
  • Works in edit mode: no

Masked Input

You can define an editor's mask settings using the properties with the Mask prefix (such as the TextEdit.MaskType, TextEdit.Mask, TextEdit.MaskCulture, etc.).

Masks are used in edit mode. Set the TextEdit.MaskUseAsDisplayFormat property to true to use the specified mask in display mode. If this property is false, the BaseEdit.DisplayFormatString format specifies an editor's display text.

Refer to Masked Input for more information.

<dxg:GridColumn FieldName="Value">
    <dxg:GridColumn.CellTemplate>
        <DataTemplate>
            <dxe:TextEdit Name="PART_Editor"
                        Mask="c"
                        MaskType="Numeric"
                        MaskUseAsDisplayFormat="True" />
        </DataTemplate>
    </dxg:GridColumn.CellTemplate>
</dxg:GridColumn>
  • Affects the column type: no
  • Affects sorting/filtering/grouping: no
  • Supports printing/exporting: no
  • Works in edit mode: yes
See Also