[Expand]General Information
[Collapse]WinForms Controls
  .NET Core Support
 [Expand]What's Installed
 [Expand]Build an Application
 [Expand]Controls and Libraries
 [Collapse]Common Features
  [Expand]Data Binding Common Concepts
  [Expand]Data Source Wizard
  [Expand]Application Appearance and Skin Colors
  [Expand]Filtering UI Context
   Find Panel Syntax
  [Expand]Graphics Performance and High DPI
  [Expand]Scaffolding Wizard
  [Collapse]Formatting Values
    How to: Format Date-Time Values Using the LongDate Pattern
    How to: Format Date-Time Values Using a Custom Format Pattern
    How to: Format a Numeric Value according to its Sign
    How to: Add Custom Text to a Formatted String
    How to: Create a Custom Formatter to Change the Case of String Values
    How to: Create a Custom Formatter to Represent Decimal Values in Binary
    Formatting Approaches
    Members that Support the Formatting Mechanism
    Format Specifiers
    Composite Formatting
    Custom Formatting
   HTML-inspired Text Formatting
  [Expand]Tooltip Management
  [Expand]Save and Restore Layouts
   Clipboard - Copy and Paste Operations. Data Formatting
   Version Compatibility: Default Property Values
  Get More Help
 [Expand]API Reference
[Expand]ASP.NET Controls and MVC Extensions
[Expand]ASP.NET Bootstrap Controls
[Expand]ASP.NET Core Bootstrap Controls
[Expand]WPF Controls
[Expand]Xamarin Controls
[Expand]Windows 10 App Controls
[Expand]Office File API
[Expand]Report and Dashboard Server
[Expand]eXpressApp Framework
[Expand]eXpress Persistent Objects
[Expand]CodeRush Classic
[Expand]Cross-Platform Core Libraries
[Expand]Tools and Utilities
 End-User Documentation
View this topic on docs.devexpress.com (Learn more)

Custom Formatting

The Custom Formatting feature allows you to:

  • format values that are not numeric nor date/time;
  • implement complex formatting of numeric or date/time values.

This topic explains how to implement a custom formatter.

Expanded Custom Formatting Concepts

Implementing a custom formatter means creating a method that will return formatted strings, taking the original value and the format string as the parameters. Thus, it is up to you to produce the formatting logic, but this does give you unlimited capabilities. To enable custom formatting, set the FormatInfo.FormatType property to FormatType.Custom and the FormatInfo.Format property to your custom formatter object.

To create a custom formatter object, you will need to follow the steps below.

  • Create a class implementing the IFormatProvider interface. The interface declares a single GetFormat method that must return a reference to the object implementing the ICustomFormatter interface. Note that it is common practice to provide a single class that implements both the IFormatProvider and ICustomFormatter interfaces. In this case, the GetFormat method must simply return a reference to the current class instance.
  • Implement the Format method of the ICustomFormatter interface. This method takes the format string, the value and the format provider as parameters and must return the formatted string. The formatting logic must be implemented in this method.
  • Initialize the FormatInfo.Format property with an object that implements the IFormatProvider interface. See the Members that Support the Formatting Mechanism topic for information on how to access this property.

The outline of the code that needs to be written to provide a custom formatter is given below.

The following two sub-sections provide examples on creating custom formatter classes: a simple upper/lower case example followed by decimal numbers displayed in binary.


Only use custom formatters when necessary, as performance is not as good as standard formatting.

Expanded Example - Changing the Case of String Values

This sample creates a custom formatter that modifies the case of an editor's text. The editor's display text will obey the specified format string (the FormatInfo.FormatString property value). This string is passed to the Format method as the format parameter. If this parameter value is 'u', the display value is converted to upper case, if the format string is 'l', it is converted to lower case.

The following image shows the text editors' appearance before and after sample code execution.

See also: RepositoryItemTextEdit.CharacterCasing.

Expanded Example - Display Decimal Values in Binary

This example demonstrates a way of creating a custom formatter object to display decimal values in binary, but only if the format string is supplied as "B".

The result is shown in the image below.

Is this topic helpful?​​​​​​​