Data formatting.
This topic contains the following sections.
Data formatting means transformation of values returned by data objects (usually by their properties) to strings for displaying in grid cells. Data formatting should not be confused with templates that can be used to change data presentation in cells. Formatting means simple transformation of unformatted values to text. The easiest way to transform value to text is to call Object.ToString()()()() method. However it is not always convenient as it can't be to display the same value differently in different parts of application and poorly implements the principle of separating data from presentation. The most general method is to use converters.
Type converters
MVVM data model implies separation of data from presentation with data binding. In this model data can be presented in different ways in different controls. Data binding enables binding business logic with graphical controls on data layer. When controls are initialized, the data layer provides values that are later transformed to required types and displayed on the presentation layer. User may change values with corresponding controls (combo box, text box, etc). New value entered by user is transformed to the required type and transmitted to the data layer via data binding. Type conversion is done via classes that implement IValueConverter interface. An example of implementing a data converter that transforms business logic values to strings is provided below.
C# | Copy |
---|---|
internal class IntToStringConverter : IValueConverter { #region IValueConverter Members // Converts int-type value to string public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value is int) { return string.Format(culture, "{0}", value); } return null; } // Parses string to int-type value public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { string s = value as string; if(!string.IsNullOrEmpty(s)) { return int.Parse(s, culture); } return Binding.DoNothing; } #endregion } |
IValueConverter may actually convert business logic to any data types. We will be most interested in conversion to String and back. The grid uses this conversion method to display rows in grid cells.
Usage of type converters
Programmer has the following ways to set type converters.
- Declarative method with attributes.
- Setting type converters in grid columns.
Declarative method is often use for unified data presentation in the application. Converters are set with attributes for data object properties. There is a set of predefined attributes that can be used to set the required presentation.
C# | Copy |
---|---|
class MyDataObject { //Specific converter to present double values as strings with 2 digits after dot [DoubleConverter(2)] public double SomeDoubleValue { get; set; } //Generic converter to format value with the specified format [Converter("yyyy-MM-dd HH:mm:ss")] public DateTime SomeDateValue { get; set; } } |
If converters provided by Dapfor's framework are not sufficient, programmers may create their own converters and attributes to use in declarative formatting. An example of implementing an attribute for declarative setting of the above shown IntToStringConverter:
C# | Copy |
---|---|
public class IntToStringConverterAttribute : ConverterBaseAttribute { private readonly IntToStringConverter _converter; public IntToStringConverterAttribute() { _converter = new IntToStringConverter(); } public override IValueConverter Converter { get { return _converter; } } } |
As we can see, IntToStringConverterAttribute create an instance of IntToStringConverter and returns it in a virtual property. IntToStringConverterAttribute class can also be enhanced by builders with parameters for sending them to IntToStringConverter during initialization. An example of using the attribute created above is shown below:
C# | Copy |
---|---|
class MyDataObject { //... [IntToStringConverter] public int SomeIntValue { get; set; } } |
Declarative formatting has one important feature – if a grid displays data of different types in the same column, and if no converters are used in this column, data in grid cells may be displayed differently depending on converters set in data object properties.
Another way to use converters is to set them directly in grid columns. This method is used in cases when usage of declarative formatting is not desirable or when it is necessary to display data in different grids differently. The below example demonstrates usage of converters in grid columns.
C# | Copy |
---|---|
GridControl grid = ...; grid.Headers[0]["SomeDoubleValue"].ValueConverter = new DoubleConverter(2); |