Classes
Class | Description | |
---|---|---|
DecimalFormat |
Formats and parses Decimal values.
| |
DecimalFormatAttribute |
An attribute that declares DecimalFormat for a property of data object.
| |
DoubleFormat |
Formats and parses Double values.
| |
DoubleFormatAttribute |
An attribute that declares DoubleFormat for a property of data object.
| |
EmptyFormat |
Formats all values to empty strings to prevent displaying text in cells.
| |
FormatAttribute |
Enables use of standard string patterns to format values in cells.
| |
FormatBaseAttribute |
Base class for all format attributes.
| |
HexFormat |
Formats and parses hexadecimal values
| |
StringFormat |
Formats values using standard string patters.
|
Interfaces
Interface | Description | |
---|---|---|
IFormat |
Objects implementing this interface format and parse values in cells.
|
Remarks
A very important feature in .Net Grid is its ability to work directly with application business logic. Business logic is a set of classes that may have certain properties returning specific values, i.g. prices, quantities, dates, etc. Generally these values are represented by primitive types, such as System.Int32, System.Double, System.Decimal etc. To show this data in grid cells, it's sufficient to convert the necessary values into the System.String type by calling Object.ToString() or String.Format("{0}", value). However, this approach is not flexible and doesn't support parsing strings to objects. To fill in for this, the .Net Grid provides a very powerful system of formats to convert values into strings and vice-versa. These formats are fully customizable. For instance, the grid can display empty strings instead of "0" when a value equals 0 or add a separator between thousands or some prefix or suffix like "$". These formats can also parse strings back into values. For application programming it's better to have a set of format classes, where data presentation is centralized.
In programming, formats can be defined in the following places:
- As an attribute of a class property. For example: FormatAttribute, DoubleFormatAttribute, EmptyFormatAttribute, etc.
- In a column: Column.Format = 'your format';
- Directly in a cell (this method requires a lot of memory): Cell.Format = 'your format';
The .Net Grid looks for IFofmat object to format values or parse strings in the following order:
- In a cell
- In a column returned by the Cell.Column property
- In a IDataField object, returned by the Cell.DataField property.
- If the format is still not found, the grid uses default format for the specified object type.
Some formats greatly simplify application development. For example, StringFormat, enables use of standard patterns for formatting values through String.Format():
Copy | |
---|---|
column.Format = new StringFormat("### ### ### ###", string.Empty, " $"); The value 12345 is displayed as "12 345 $" |
You can also write your own formats for more complex formatting and parsing. Below you will find an example of a hexadecimal format:
Copy | |
---|---|
class HexFormat : IFormat { //Formats numeric value into a hexadecimal string public string Format(IDataField dataField) { return string.Format("0x{0:X}", dataField.Value); } //Verifies whether the string can be parsed public bool CanParse(string text, IDataField dataField) { text = text.Replace("0x", ""); text = text.Trim(); int value; return string.IsNullOrEmpty(text) || int.TryParse(text, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out value); } //Parses the string public void Parse(string text, IDataField dataField) { text = text.Replace("0x", ""); text = text.Trim(); int value = 0; if (string.IsNullOrEmpty(text)) { dataField.Value = value; } else if (int.TryParse(text, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out value)) { dataField.Value = value; } } } //This code shows how to set format into a column column.Format = new HexFormat(); //... grid.Rows.Add(new object[]{123}}); //This example shows how to declare the format in a property - all grids will display hexadecimal values in cells class SomeClass { private int intValue; [Format(typeof(HexFormat))] public int SomeHexdecimalValue { get { return intValue; } set { intValue = value; } } } //grid.Rows.Add(new SomeClass()); |
.NET Framework has similar system of type conversion based on the System.ComponentModel.TypeConverter class that enables conversion of values to strings and vice versa. This conversion system is more complete, but a little bit cumbersome in the context of formating and data parsing. Althrough converters are bulky, such approach enables development of a business logic independently from data presentation. In a perfect case all these mechanisms can be completely based on Microsoft's component model and therefore have no physical dependencies on libraries of other vendors and on the Dapfor libraries as well.