A very important feature in .Net Grid 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():
C# | Copy |
---|---|
column.Format = new StringFormat("### ### ### ###", string.Empty, " $"); //The value 12345 will be displayed in cells 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:
C# | 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.
Here is an example of using System.ComponentModel.TypeConverter:
C# | Copy |
---|---|
class HexTypeConverter : TypeConverter { public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) { return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); } public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) { return destinationType == typeof(string) || base.CanConvertTo(context, destinationType); } //Parses the hexdecimal string to the integer public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { if (value is string) { string text = (string) value; text = text.Replace("0x", ""); text = text.Trim(); int result = 0; if (string.IsNullOrEmpty(text) || int.TryParse(text, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out result)) { return result; } } return base.ConvertFrom(context, culture, value); } //Formats the value to the hexadecimal string public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { if (value != null && destinationType == typeof(string)) { return string.Format("0x{0:X}", value); } return base.ConvertTo(context, culture, value, destinationType); } } //This example shows how to declare the TypeConverter in a property - all grids will display hexadecimal values in cells class SomeClass { private int intValue; [TypeConverter(typeof(HexTypeConverter))] int SomeHexdecimalValue { get { return intValue; } set { intValue = value; } } } //grid.Rows.Add(new SomeClass()); |