This is a follow on article from the previous article that showed you how to use the MS DataGrid with ADO. See article.
This article will show you how to use the MS DataGrid with OLE DB consumer templates.
This is a simple implementation that shows you how to use the MS DataGrid control with OLE DB consumers. The sample has been tested with both MS Access and SQL Server, in theory you should be able to use it against all data sources that support OLE DB.
You can find information on the
DataGrid control and its properties at MSDN.
MDAC v2.1 or higher (This can be obtained from Microsoft). This contains the OLE DB drivers that are required to hook up to the database or the data source.
A good knowledge of OLE DB consumers is also required to understand the binding process to the
Using OLE DB consumers
In order to use the OLE DB consumer templates you will need to include the following header file, this should be done in your stdafx.h file.
Adding the DataGrid control to the project
You need to add the DataGrid control to your project, this is done in the usual way via:
Project->Add to project->Components and Controls
Select the Registered ActiveX Control folder from the dialog and find Microsoft DataGrid control, Version 6 (OLE DB)
Then press the insert button, the following dialog will appear:
You only need the
CDataGrid class for this sample, therefore check only this class.
Go to the Resource editor, you should see the MS DataGrid control added to your collection of controls that you can use. The
CDataGrid class will have been generated for you in DataGrid.h and DataGrid.CPP, these files will also have been added to your project.
Bind the DataGrid to a Dialog or Formview
Use the Class Wizard in the normal way to bind the control to a Dialog or Formview. Class wizard will bind the
CDataGrid class with the control.
void CDataGridView::DoDataExchange(CDataExchange* pDX)
DDX_Control(pDX, IDC_DATAGRID1, m_ctlDataGrid);
Bind the DataGrid to OLE DB Template Command
There is actually a pinch of ADO that is required when binding the
Recordset to the MS DataGrid. The OLE DB
Command needs to be converted into an ADO
Recordset before it is bound to the MS DataGrid.
The conversion from an OLE DB
Command to an ADO
Recordset can be done through ADO
RecordsetConstruction (See code below for an example).
You have to bind the
DataGrid at run-time using an ADO
Recordset. The following code gives you the example used.
void CDataGridView::UpdateGridDetails(const CString& sTableName)
CMainFrame* pMainFrame = reinterpret_cast<CMAINFRAME*>(AfxGetMainWnd());
CCommand<CDYNAMICACCESSOR, CRowset> dbCommand;
DBPROPVAL_UP_CHANGE | DBPROPVAL_UP_INSERT |
sCommand.Format("SELECT * FROM [%s]", sTableName);
HRESULT hr = dbCommand.Create(pMainFrame->m_session,
hr = dbCommand.Open(&propset, NULL, true);
hr = spRs.CreateInstance(__uuidof(Recordset));
hr = spRs->QueryInterface(__uuidof(ADORecordsetConstruction),
m_ctlDataGrid.SetRefDataSource( (LPUNKNOWN) spRs);
SetRefDataSource property is used to bind the ADO
Recordset generated to the control. Please note that the type of
Recordset cursor generated determines what can and can not be done in the grid. Example, if a forward only cursor is used, then the grid will not allow you to add, edit or delete records via the grid.
Note - The client cursor location needs to be set in order for it to work with MS Access (This is not required in SQL Server).