CGXStyle::SetValueType() allows you to specify the value type in a cell. Possible types are:
GX_VT_STRING
GX_VT_NUMERIC
GX_VT_EXPRESSION
GX_VT_ERROR
CGXStyle::GetValueType() returns the value type of the cell.
SetValueType() does not affect the way values are stored in CGXStyle. All values are stored as a string. SetValueType() only offers additional information about the value that is stored in the cell. For example, when you apply numeric formatting to a cell, it will only affect the cell if its value type is GX_VT_NUMERIC.
The value type information is very important when you pass style objects to the formula engine. The formula engine parses values to determine if they should be stored as strings or as numbers. It is very important for the formula engine to distinguish between numbers and strings.
// Sets the value type to GX_VT_NUMERIC SetValueRange(CGXRange(nRow, nCol), 122.22); // Sets the value type to GX_VT_STRING SetValueRange(CGXRange(nRow, nCol), "122.22"); // Sets the value type to GX_VT_STRING SetStyleRange(CGXRange(nRow, nCol), CGXStyle().SetValue ("122.22")); // Sets the value type to GX_VT_NUMERIC SetStyleRange(CGXRange(nRow, nCol), CGXStyle().SetValue (929.2)); // SetExpressionRowCol parses the string and sets the // value type to GX_VT_NUMERIC SetExpressionRowCol (nRow, nCol, "1222.22"); // SetExpressionRowCol parses the string and sets the // value type to GX_VT_STRING SetExpressionRowCol (nRow, nCol, "ABDGDDG1222.22"); |
The following methods pertain to number formatting:
CGXStyle::SetFormat()
CGXStyle::SetPlaces()
These methods let you format number values for static cells and specify the precision of the value (the number of significant digits).
Objective Grid lets you format number values for inactive cells and specify the precision when appropriate. The formatted value is only displayed for static (inactive) cells. If you start editing a cell, the original value is displayed.
For example, if you store the value 122.2345 in a cell and then set the format to be fixed with two decimals, 122.23 is displayed in the cell. When you click the cell make the cell active, the cell value changes to 122.2345. Once you leave the cell the cell is made inactive again, the display text reverts to 122.23.
When you apply numeric formatting to a cell, it only affects the cell if the value type is GX_VT_NUMERIC.
Objective Grid supports the following formats. For certain format types you can also specify the number of places (referred to as 'N' in Table 8).
Format ID | Description | Definition |
GX_FMT_FLOAT | Scientific | Displays the number in scientific notation (exponent form) with N significant digits. |
GX_FMT_FIXED | Fixed | Displays the number using a fixed number of decimal places, specified by N. |
GX_FMT_FIXED_PARENS | Fixed | Displays the number using a fixed number of decimal places, specified by N. Uses parentheses to indicate negative numbers. |
GX_FMT_NUMLOCALE | Locale | Displays number using locale from regional settings ( , instead of . for example). |
GX_FMT_GEN | General | Displays the number in fixed format or scientific notation, whichever fits. Trailing zeros are not displayed. |
GX_FMT_DOLLARS | Dollars | Displays the number with a leading dollar sign ($) and with comma delimiters, as in $1,000,000. Negative values are displayed in parentheses. |
GX_FMT_COMMA | Comma | Displays the number with comma delimiters, as in 1,000,000. Negative values are displayed in parentheses. |
GX_FMT_HEX | Hex | Display the number in hexadecimal notation. For example, the value 31 is displayed as 1F. |
GX_FMT_LOGIC | Logic | Displays the value 0 as 0, the value 1 as 1, and all other values as ?. |
GX_FMT_DAY_MONTH_YEAR | DD-MMM-YY | Displays the integer portion of a date/time value as a Gregorian date, in the format 01-Aug-99. |
GX_FMT_DAY_MONTH | DD-MMM | Displays the integer portion of a date/time value in the format 01-Aug. |
GX_FMT_MONTH_YEAR | MMM-YY | Displays the integer portion of a date/time value in the format Aug-99. |
GX_FMT_DATE | MM/DD/YY | Displays the integer portion of a date/time value in the format 08/01/99. |
GX_FMT_HIDDEN | Hidden | The cell contents are not displayed. |
GX_FMT_TIME | HH:MM:SS | Displays the fractional portion of a date/time value in the format 06:15:30. |
GX_FMT_PERCENT | Percent | Displays the number as a percentage, multiplying it by 100. For example, the value 0.1 is displayed as 10.00%. |
GX_FMT_TEXT | Text | For cells that contain formulas, the formula itself is displayed rather than the computed value of the cell. |
GX_FMT_INTL_DATE | DD.MM.YYYY | Displays the integer portion of a date/time value in the format 01.08.1999. |
GX_FMT_ISO8061_DATE | YYYY-MM-DD | Displays the integer portion of a date/time value in the ISO 8061 date format 1999-08-01. |
GX_FMT_DATETIME | MM/DD/YY HH:MM | Displays the date and time according to the system settings. |
GX_FMT_USER1 | User1 | If you override GXFormatText you might use these format codes: |
GX_FMT_USER2 | User2 | (GX_FMT_USER1, GX_FMT_USER2 |
GX_FMT_USER3 | User3 | GX_FMT_USER3, GX_FMT_USER4) |
GX_FMT_USER4 | User4 | for your own currency formatting (e.g. DM, Lira, ...). |
Use CGXStyle::SetFormat() to apply number formatting to cells.
SetExpressionRowCol(nRow, 1, _T("General")); SetExpressionRowCol(nRow+1, 1, _T("7777.77")); SetStyleRange(CGXRange(nRow+1, 1), CGXStyle().SetFormat(GX_FMT_GEN).SetPlaces(15)); SetExpressionRowCol(nRow, 2, _T("Fixed")); SetExpressionRowCol(nRow+1, 2, _T("7777.77")); SetStyleRange(CGXRange(nRow+1, 2), CGXStyle().SetFormat(GX_FMT_FIXED).SetPlaces(4)); SetExpressionRowCol(nRow, 3, _T("Scientific")); SetExpressionRowCol(nRow+1, 3, _T("7777.77")); SetStyleRange(CGXRange(nRow+1, 3), CGXStyle().SetFormat(GX_FMT_FLOAT).SetPlaces(4)); SetExpressionRowCol(nRow, 4, _T("Dollars")); SetExpressionRowCol(nRow+1, 4, _T("7777.77")); SetStyleRange(CGXRange(nRow+1, 4), CGXStyle().SetFormat(GX_FMT_DOLLARS).SetPlaces(2)); SetExpressionRowCol(nRow, 5, _T("Comma")); SetExpressionRowCol(nRow+1, 5, _T("7777.77")); SetStyleRange(CGXRange(nRow+1, 5), CGXStyle().SetFormat(GX_FMT_COMMA).SetPlaces(2)); SetExpressionRowCol(nRow, 6, _T("Percent")); SetExpressionRowCol(nRow+1, 6, _T(".77")); SetStyleRange(CGXRange(nRow+1, 6), CGXStyle().SetFormat(GX_FMT_PERCENT).SetPlaces(2)); |
This applies only to non formula grids.
By default, Objective Grid does not use regional settings when you choose to format numbers. So, it cannot understand that 1.345,13 is a number, and format it properly, even if your system's regional settings are set so that period is the thousands separator and comma is the decimal separator. To get Objective Grid to use your regional settings, add a call to CGXGridCore:: EnableLocaleNumbers() in your grid's Initialize() or OnInitialUpdate() call. After that, any cell using a CGXEditControl that has been formatted using one of the codes listed below will use the numeric locale settings. In addition, with this setting enabled, Objective Grid will recognize values typed into numeric formatted cells as being numerical values.
No matter what the locale settings, the actual numerical values stored in the grid's data object use a period as the decimal separator and do not contain any thousands separator. This convention makes the grid's serialization locale independent. Numbers serialized from a grid using Italian settings can be read into a grid using English settings without losing the numerical values.
Also, for historical reasons, Objective Grid catches system changes through handling the WM_WININICHANGE message. If your code implements a handler for the newer WM_SETTINGCHANGE message, make sure you return a FALSE in your message handler so the grid's handler will also be called.
To customize this behavior, derive your control from CGXEditControl and override one of these three virtual CGXEditControl methods.
virtual BOOL StripSeparatorFromValue(CString& sValue, const CGXStyle* pStyle, double& d); // called from Store & ConvertControlTextToValue (handles Paste) // to strip group separator from the value before it is stored virtual BOOL PutDecInValue(CString& sValue, const CGXStyle* pStyle); //called from Init to put correct dec sep in windowtext virtual BOOL AcceptCommasInNumbers(const CGXStyle* pStyle); // returns whether strings with thousands separator should be // treated as a number. Default behavior checks for one of these // number formats: // GX_FMT_FIXED // GX_FMT_COMMA // GX_FMT_DOLLARS // GX_FMT_FLOAT // GX_FMT_GEN // GX_FMT_FIXED_PARENS // GX_FMT_NUMLOCALE |
By default in a non-formula grid, the code below will display 1234.5555 as 1,234.56 no matter what your regional settings are. But if you add a call to EnableLocaleNumbers() in your initialization code, the formatting will reflect your regional settings. For example, using Italian locale settings would display the value 1234.5555 as 1.234,56.
SetStyleRange(CGXRange(1,1,1,2), CGXStyle() .SetFormat(GX_FMT_COMMA) .SetValueType(GX_VT_NUMERIC) .SetPlaces(2) .SetValue((double) 1234.5555) ); |
Copyright © Rogue Wave Software, Inc. All Rights Reserved.
The Rogue Wave name and logo, and Stingray, are registered trademarks of Rogue Wave Software. All other trademarks are the property of their respective owners.
Provide feedback to Rogue Wave about its documentation.