Objective Grid : PART I User’s Guide : Chapter 2 Design Overview : Browser Architecture
Browser Architecture
The Objective Grid browser classes let you easily browse any external data source by overriding virtual methods. Objective Grid already provides special implementations of the browser classes for displaying ODBC CRecordset and ADO results.
The key principle for browsing external data sources is that the grid object maintains one current record with the current cell. When the user edits data in the current record, all changes are stored in an internal buffer. When the user moves the current cell to another record, changes for the current record are written to the data source before the current cell is positioned to a new record. When the user clicks on the last row, a new record will be added to the data source (if the data source supports appending records).
The main features of Objective Grid for browsing data sources are:
Row headers show the state of the current record. An arrow indicates the current record as unmodified, while a pencil indicates that the current record has been modified.
Provides support for adding rows to the data source. When the user enters data in the last row (marked with an asterisk in the row header), a new record will be appended.
Provides Undo support for changes and the ability to cancel all pending changes in the current record.
Provides improved abstraction for loading cell data from the data source and storing all changed cell data into the data source when the user has moved to a new record.
Allows the programmer to validate user input at the record level. This means the user can input data into the record and when the current cell moves to a new record, all changes for the previous record will be validated.
Synchronizes data changes and current cell movement between different views on the same data.
Provides support for rearranging columns and rows. This lets you add support for dragging columns and sorting rows.
Provides support for maintaining a high-water mark for the last seen record. CGXAdoGrid and CGXODBCGrid use this high-water mark to determine the record count as the user navigates through the records.
CGXBrowserGrid - Interface
CGXBrowserGrid abstracts the basic functionality for browsing databases. Two important groups of methods of the CGXBrowserGrid class allow you to:
Modify cells in the grid independent of the specific data source
Bind the grid to the external data source with a set of overrides
Current Record
The following member functions allow you to modify the cells in the grid, independent of the data source you are browsing:
AddNew()
Prepares the grid for adding a new record. The grid appends a new line in the grid. Call Update() to complete the addition.
CancelRecord()
Cancels all pending changes in the current record and resets the edit- or append-mode. If an AddNew() is in progress, the pending row is removed from the grid.
Edit()
Prepares for changes to the current record. Call Update() to complete the edit.
UndoRecord()
Cancels all changes for the current record (by calling CancelRecord()) and empties the Undo and Redo list.
Update()
Completes an AddNew() or Edit() operation by saving the changed values to the data source.
Data Source Binding
The following member functions should be overridden to bind the browser grid to your external data source.
CanAppend()
Called to determine if the user should be able to append rows to the data source.
CanUpdate()
Called to determine if the user should be able to change data in the data source.
DeleteRows()
Called to delete rows from the data source.
MoveTo()
Called to position the recordset to the given row. You should return FALSE if the move failed.
OnAddedNewRecord()
Called from Update() when a new record has been added to the data source.
OnFlushCellValue()
Called from within CGXBrowserGrid::OnFlushRecord() for all values changed in the current record buffer. You should override this method to store changed values in your data source.
OnFlushRecord()
Writes “dirty” fields (those with unsaved changes) back to the data source and prepares the data source at record level before changes are written to it with OnFlushCellValue().
OnGetRecordCount()
Called to determine the number of records in the data source, if the last record has been accessed. If the record count is unknown, the method should return LONG_MAX.
OnLoadCellStyle()
Called to load a data value from the data source or apply additional formatting at run time. This method provides a better abstraction for loading data from data sources at the record level. OnLoadCellStyle() is called from GetStyleRowCol() when needed.
Requery()
Called to requery the data source. The default implementation resets the high-water mark for the last seen record.
ODBC Browser
As already mentioned, the MFC ODBC classes do not directly support the ability to determine database schema information at run time. Objective Grid provides support to allow the end user to specify and view the results of an SQL query at run time (where schema is unknown). Objective Grid also supports the MFC paradigm where the developer uses ClassWizard to create recordsets with a known schema.
CGXODBCGrid is a special derivative of the CGXBrowserGrid class for browsing ODBC data sources. CGXRecordWnd and CGXRecordView inherit from CGXODBCGrid and represent the ODBC browser as a view or dialog control, respectively. CGXODBCGrid is implemented only through overriding the methods named in the previous “Data Source Binding” section of the CGXBrowserGrid interface.
When initializing your grid, you should store a pointer to your recordset object inside of your grid. The recordset will be of type CRecordset (or a class derived from it).
Objective Grid’s CGXDynamicRecordset class derives from CRecordset and lets you easily construct a recordset object that can be browsed in the grid.
 
CGXDynamicRecordset m_QuerySet;
m_QuerySet.SetSqlQuery("SELECT * FROM STUDENT");
 
// Overriden OnInitialUpdate method
void CMyGridView::OnInitialUpdate()
{
SetRecordset(&m_QuerySet);
CGXRecordView::OnInitialUpdate();
}
See the Objective Grid Class Reference and the GxQuery sample for further information on ODBC classes. Also check out the tutorial for using the ODBC classes.
ADO Browser
CGXAdoGrid is a special derivative of the CGXBrowserGrid class for browsing any database through ADO. CGXAdoRecordWnd and CGXAdoRecordView inherit from CGXAdoGrid and represent the ADO browser as a view or dialog control. CGXAdoGrid is implemented only through overriding the methods named in the previous “Data Source Binding” section of the CGXBrowserGrid interface.
See the Objective Grid Class Reference and the AdoQuery sample for further information about ADO classes.