Data Formatting and Presentation

Data Formatting and Presentation

In compliance with the principle of separating the data layer from the presentation layer, NetGrid has a powerful data binding system that fully implements all relevant capabilities. Data can be represented with arbitrary classes, lists, Dictionaries, etc. The grid provides multiple methods of connecting to this data via a binding list or in unbound mode.

This tutorial shows presentation of formatted business layer data and back transformation of this data to values, for example when a user edits a cell via TextBox or other controls.

It is very important to understand how data differs from its presentation. Let's review an example to make it more clear. Let's take some abstract product. Quantity and price of this product can be represented with simple types: int and double. Class containing this data has corresponding fields: Price & Quantiry.

It is better to provide this data in a form convenient for user, i.e. if the price is not set (price value equals zero), it is better not to display anything in cells. If price and quantity values are high, it would be good to display thousand separators or display data in a special way, e.g. adding suffixes for thousands, millions, etc.

For example, value 1234567 can be displayed as 1.23 M

However, if a user decides to edit quantity and types 15 K, this value should be transformed to 15000 in Quote object.

Now let's see how is data displayed in the grid. For this purpose the grid provides a simple IFormat interface that enables data formatting and reverse operations that the programmer can use for full customization of text presentation of business data.

There are several ways of setting an arbitrary format:

Declarative definition of format for data objects. Format is declared via FormatAttribute for data object properties. If data of different types is used in the grid, this approach enables different ways of formatting values in the same column. Besides, when the same data objects are used in different grids, they are displayed in the same way in the entire application.

There are some specialized attributes that simplify declarative format definition: DoubleFormatAttribute, EmptyFormatAttribute etc. It is also possible to create custom attributes for declaring formats with parameters.

Format can be set directly in grid column and values in cells will be formatted in the same way disregarding formats set via FormatAttribute

Cell value formatting

Values can be transformed to text strings displayed in the grid as follows:

The grid checks whether format is set for column corresponding to cells.

If no format is found, the grid checks whether declarative format exists for data object field.

If neither format is found, the grid searches for default format corresponding to data type. Basically this format calls object.ToString() method or invokes the ConvertTo() of appropriate TypeConverter.

Formats can be two-sided. It means that data entered by user in cell editors can be transformed from text to unformatted values and transmitted to data objects. For this purpose it is sufficient to implement CanParse and Parse methods in a class implementing IFormat.

TypeConverter - a specific method of declarative format setting

An actual project may consist of multiple assemblies some of which may represent application business logic while others may represent graphical controls displaying this logic. To comply with the principle of business logic being independent from presentation, in such case it would be good to remove dependency on assemblies with graphical components in business logic assemblies, including Dapfor.Net.dll assembly.

On the other hand, sometimes it would be convenient to use declarative data formatting set with FormatAttribute attributes.

Microsoft component model can be used to avoid adding dependency on Dapfor.Net.dll to application business logic. Specifically, it would be good to use TypeConverter that enables transformation of data of one type to another and back. In most cases these converters are set for data types:

Views: 140
Author: Regular Articles
Net Grid provides broad functionality of displaying various datagrid elements, such as cells, rows, headers and columns. The main objective of Dapfor is to develop high-performance software, specifically grids. Popular MVVM programming model that extensively uses databinding, data gridview to separate data from presentation.
Tell a friend
Average rating:
(0 votes)

Hezbollah buries militant Qantar, says Israel will be held accountable

Lebanon's Hezbollah group said on Monday that Israel would be held accountable for killing prominent militant Samir Qantar in an air strike in Syria, and accorded him an elaborate funeral of... Read More

Russia says black box from warplane downed by Turkey unreadable

Investigators in Moscow said on Monday they were unable to retrieve information from the damaged black box of a Russian warplane shot down by Turkey last month, data the Kremlin hoped would support... Read More

Exclusive: U.S. glossed over Oman's human rights record during Iran talks

By Jason Szep, Matt Spetalnick and Yara Bayoumy WASHINGTON/MUSCAT (Reuters) - As the United States negotiated this year’s nuclear pact with Iran, the State Department quietly agreed to spare the... Read More