Customizing numeric format

Article ID: 727
Last updated: 11 Jun, 2018
Article ID: 727
Last updated: 11 Jun, 2018
Revision: 3
Views: 2200
Posted: 07 Sep, 2001
by Meltreger B.
Updated: 11 Jun, 2018
by Meltreger B.

Problem


Numeric values in scientific notation can be seriously misinterpreted if the exponent is not visible because the cell is too narrow.
 


Cause



Action


Download NumFormat sample.

This sample implements a custom edit control that adjusts the number of digits displayed to make the string fit the cell.  If the cell is too narrow to display a meaningful value, ellipses are displayed to indicate that the value has been truncated.
 
The FAQ/KB article entitled ''Is there a way to specify the format of cells'' outlines two options for changing how values are displayed.
1.  Override GetStyleRowCol() and StoreStyleRowCol(), or
2.  Create a custom edit control overriding GetControlText() and SetValue().

I recommend the custom edit control method for our purposes.  It has the following advantages:
1.  You can also override ValidateString() to disallow any non-numeric characters.
2.  You can easily select which cells are given the special format.
3.  GetStyleRowCol() and StoreStyleRowCol() do not work with formatted cells -- SetFormat() -- because GXFormatText() applies the formatting after GetStyleRowCol().

Our custom control, CNumEditControl, is derived from CGXEditControl.  It has overrides of GetControlText() and SetValue() to implement our custom formatting.  It also has an override of Draw().  This override is an exact copy of CGXEditControl::Draw().  It is required so that CGXStatic::Draw() will call CGXDrawingAndFormatting::DrawStatic() with the proper CGXControl* pControl parameter -- i.e. pControl is a CNumEditControl and not a CGXEditControl.  CGXDrawingAndFormatting::DrawStatic() calls our GetControlText().

In addition, ValidateString() is overridden to accept only numeric characters.  And OnGridCtlColor() is overridden to change the cell's colors when active.

To use this custom numeric edit control in your app, add a string resource ID of IDS_CTRL_NUMEDIT to your project.


In the grid setup -- OnInitialUpdate() -- register the custom control.


   //  Use custom numeric edit control
   RegisterControl(IDS_CTRL_NUMEDIT, new CNumEditControl(this, IDS_CTRL_NUMEDIT));

Then use this control to format the cells that you want.  For example,

    SetStyleRange( CGXRange(7,2),
       CGXStyle()
          .SetControl(IDS_CTRL_NUMEDIT)  // use custom numeric edit control
          .SetValueType(GX_VT_NUMERIC)
	  .SetFormat(GX_FMT_FLOAT)  // scientific notation
	  .SetPlaces(10)      // digits to display
	  .SetValue(0.12345678912345e-12)
	);

I also tried the Set/StoreStyleRowCol() method, but I ran into difficulties.  First, I wanted to check the cell's ValueType and Format to see if I should modify the string.  But the style passed to StoreStyleRowCol() does not have this information.  I had to call ComposeStyleRowCol() to get it.  Also, the ValueType seemed to change from GX_VT_NUMERIC to GX_VT_STRING when I edited a value so that the decimal point was removed from the display. So I would only suggest the Set/StoreStyleRowCol() method if you can identify the cells to modify by position (row and/or column) and the cells are not formatted.

Mike Shipman
May 24, 2001


This article was:   Helpful | Not helpful
Report an issue
Article ID: 727
Last updated: 11 Jun, 2018
Revision: 3
Views: 2200
Posted: 07 Sep, 2001 by Meltreger B.
Updated: 11 Jun, 2018 by Meltreger B.

Others in this category