The Program
This section examines the relevant VirtGrid source code. The grid control in this sample displays and allows end users to edit data stored in the document's member object m_data.
The following are code listings and discussions of the relevant overrides in this sample.
Initialization and Column Count
void CVirtGridView::OnInitialUpdate()
{
CGXGridView::OnInitialUpdate(); // 1
SetColCount(GetDocument()->m_data.GetColCount()); // 2
SetStyleRange(CGXRange().SetCols(4), // 3
CGXStyle().SetControl(GX_IDS_CTRL_CHECKBOX));
SetStyleRange(CGXRange().SetCols(1),
CGXStyle().SetInterior(RGB(150,150,150)));
SetStyleRange(CGXRange().SetCols(2),
CGXStyle().SetTextColor(RGB(0,0,255)));
GetParam()->EnableMoveCols(FALSE); // 4
GetParam()->EnableMoveRows(FALSE);
m_nClipboardFlags = GX_DNDTEXT; // 5
}
Override OnInitialUpdate to initialize the virtual grid. This override can also be used to initialize other aspects of the grid object.
Row Count
ROWCOL CVirtGridView::GetRowCount()
{
return GetDocument()->m_data.GetRowCount(); // 1
}
Override GetRowCount() to furnish the number of data rows to the grid object. This differs from an explicit call to SetRowCount() in that no style objects are allocated for each individual cell. In a virtual grid, the data is stored externally to the grid object making individual style objects unnecessary. An explicit call to SetRowCount() would only waste memory and resources.
NOTE >> GetRowCount() and GetColCount() are used extensively to validate values within routines. In addition, because HitTest() is called repeatedly on every mouse move and because this method touches a lot of grid code, including GetRowCount(), it is not unusual for GetRowCount() to be called thousands of times in a short period of time. Because GetRowCount() and GetColCount() are called quite often, your overrides of these methods are not an appropriate place to do extensive calculation.
Making the Virtual Connection Between Grid Object and Data Source
BOOL CVirtGridView::GetStyleRowCol(ROWCOL nRow,
ROWCOL nCol,
CGXStyle & style,
GXModifyType mt,
int nType)
{
BOOL bRet = CGXGridView:: GetStyleRowCol(nRow, // 1
nCol, style, mt,
nType);
if(nType >= 0) // 2
{
if(nCol != 0) // 3
{
style.SetValue(GetDocument()->m_data. // 4
GetValueRowCol(nRow, nCol));
}
}
return bRet;
}
BOOL CVirtGridView::StoreStyleRowCol(ROWCOL nRow, //5
ROWCOL nCol,
const CGXStyle *pStyle,
GXModifyType mt,
int nType)
{
if(nType == -1) // 6
{
return CGXGridView::StoreStyleRowCol(nRow,
nCol,
pStyle,
mt,
nType);
}
else if(nType >= 0) // 7
{
if(nCol != 0 && pStyle->GetIncludeValue()) // 8
{
GetDocument()->m_data. // 9
StoreValueRowCol(nRow, nCol,
pStyle->GetValue(),
gxOverride);
return TRUE;
}
}
return FALSE;
}
Override GetStyleRowCol() to supply the data dynamically from the external data source. This virtual function is called every time a cell is redrawn on the DC. Getting the data from the data source from within this override will ensure that the grid is in sync with the data source.
VirtGrid Conclusion
The VirtGrid tutorial demonstrates the minimum requirements to virtually bind a grid object. Keep in mind that virtual binding simply means that a grid's data is stored externally to the grid object. The stored data is not limited to cell values. It is possible to store any aspect of a cell's style externally. For the sake of discussion, we demonstrated only cell value storage, but the technique is comparable for all segments of cell styles.