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: