Chapter 5 Using Objective Grid
Cell Value
This section discusses the manipulation of grid values in a regular grid. The Objective Grid class methods presented in this section are the most commonly used subset of available methods. Other sections will discuss relevant methods for manipulating cell values as necessary. For more information about these or other methods, please consult the Objective Grid Class Reference.
Filling the Grid with Values
There are two common methods for putting values into a grid:
1. CGXGridCore::SetValueRange() — This is a good method to use when only the cell value needs to be changed.
2. CGXGridCore::SetStyleRange() — This is a good method to use when the cell attributes need to be changed in addition to the value. This usually occurs during (but is not limited to) initialization. The method accepts a style object. You can set the cell value in the style object using CGXStyle::SetValue().
If you are making changes to multiple cells, you should lock the updating mechanism in the grid object using CGXGridCore::LockUpdate(). This will prevent unnecessary redrawing and flickering during multiple updates.
 
// Lock the grid update mechanism
BOOL bOldLock = LockUpdate(TRUE);
 
// Fill cell values with SetValueRange
SetValueRange(CGXRange(nRow, 1), str); // string
SetValueRange(CGXRange(nRow, 2), d); // number
 
// Fill cell values with SetStyleRange
SetStyleRange(CGXRange(nRow, 3),
CGXStyle()
.SetValue(str)
);
 
// Enable grid updating
LockUpdate(bOldLock);
 
// Redraw the grid
Redraw();
How Objective Grid stores the cell values is very important. There are APIs supplied to set cell values based on numerous data types ranging from double and float to CString and LPCTSTR. Objective Grid always stores those values as strings. When data is passed as anything but a string, Objective Grid converts it using the appropriate precision and then stores it as a string.
Getting Values from the Grid
There are two methods for getting values from the grid:
*CGXGridCore::GetValueRowCol()
*CGXStyle::GetStyle()
Both methods return a CString representation of the cell's value. If you need the value as a different data type, you should convert it.
// String
CString strValue = GetValueRowCol(nRow, nCol);
 
// Number
double d = atof(GetValueRowCol(nRow, nCol));
 
// Via style
CString strStyleVal = _T("");
CGXStyle style;
GetStyleRowCol(nRow, nCol, &style);
 
if (style.GetIncludeValue())
{
strStyleVal = style.GetValue();
}
Changes in cell values made by the end user interactively are stored when the cell becomes inactive. This is important to know, because the above two methods return the stored cell value. Calling these methods on an active cell will not reflect a pending value change. With this in mind, there are two ways to get the cell value regardless of the cell's current state.
*Call CGXGridCore::TransferCurrentCell() before getting the cell value. This solution is appropriate if maintaining the current state of the cell is not necessary. TransferCurrentCell() stores and deactivates the current cell. Calling this method is very useful if you want to ensure that the current cell's contents are transferred to the grid before performing specific operations with grid data.
*Implement the following GetCellValue() method:
 
CString CMyGrid::GetCellValue(ROWCOL nRow, ROWCOL nCol)
{
if (IsCurrentCell(nRow, nCol) && IsActiveCurrentCell())
{
CString s;
CGXControl* pControl = GetControl(nRow, nCol);
pControl->GetValue(s);
return s;
}
else
return GetValueRowCol(nRow, nCol);
}
This method will return the current value for a given cell, regardless of the active state of the cell. It will not change the active state either. This method is useful when you need to preserve the current state of the cell.
Clearing Cell Values
To empty a cell or range of cells, call:
 
ClearCells(CGXRange(1, 1, nRows, nCols), FALSE);
Note that the second parameter is FALSE. This indicates that only the cell value should be cleared. Passing TRUE for this parameter will cause all cell style attributes to be cleared.