Click here to Skip to main content
15,867,308 members
Articles / Desktop Programming / ATL
Article

Using MS DataGrid control with OLE DB consumer

Rate me:
Please Sign up or sign in to vote.
4.25/5 (8 votes)
22 Sep 20013 min read 189.3K   2.2K   37   20
Use the MS DataGrid control in your C++ app with OLE DB Consumer Templates

Sample Image - msdatagridoledb.gif

Introduction

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.

Requirements

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 DataGrid.

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.

#include "atldbcli.h"

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)

Registered Controls - msdatagridoledb2.gif

Then press the insert button, the following dialog will appear:

Insert Classes - msdatagridoledb3.gif

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.

CDataGrid m_ctlDataGrid;
void CDataGridView::DoDataExchange(CDataExchange* pDX)
{
    CFormView::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CDataGridView)
    DDX_Control(pDX, IDC_DATAGRID1, m_ctlDataGrid);
    //}}AFX_DATA_MAP
}

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());
    if (pMainFrame)
    {        
        CCommand<CDYNAMICACCESSOR, CRowset> dbCommand;

        try 
        {    
            Recordset20Ptr spRs;
            ADORecordsetConstructionPtr spADOsCt;

            CDBPropSet propset(DBPROPSET_ROWSET);            
            propset.AddProperty(DBPROP_CLIENTCURSOR, true);
            propset.AddProperty(DBPROP_IRowsetChange, true);
            propset.AddProperty(DBPROP_UPDATABILITY, 
               DBPROPVAL_UP_CHANGE | DBPROPVAL_UP_INSERT | 
               DBPROPVAL_UP_DELETE);

            CString sCommand;
            sCommand.Format("SELECT * FROM [%s]", sTableName);
            
            HRESULT hr = dbCommand.Create(pMainFrame->m_session, 
                                (LPCTSTR)sCommand);
            if(FAILED(hr))
                _com_issue_error(hr);
            
            hr = dbCommand.Open(&propset, NULL, true); 
            if(FAILED(hr)) 
               _com_issue_error(hr);
            
            hr = spRs.CreateInstance(__uuidof(Recordset));
            if(FAILED(hr))
               _com_issue_error(hr);
            
            hr = spRs->QueryInterface(__uuidof(ADORecordsetConstruction),
                (void**)&spADOsCt);

            if(FAILED(hr))
               _com_issue_error(hr);

            hr= spADOsCt->put_Rowset(dbCommand.m_spRowset); 
            if(FAILED(hr))
               _com_issue_error(hr);

            //Demonstrates,  how to populate DataGrid 
            //by assigning it a Recordset object. 
            m_ctlDataGrid.SetCaption(sTableName); 
            m_ctlDataGrid.SetRefDataSource(NULL); 
            m_ctlDataGrid.SetRefDataSource( (LPUNKNOWN) spRs);
            m_ctlDataGrid.Refresh();
        } 
        catch (_com_error&e)
        { 
           AfxMessageBox(GetErrorDescription(e));
        }
        
        UpdateData(FALSE);
    }
}

The 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).

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United Kingdom United Kingdom
Was made redundant in early 2003 after 10 years in computer programming, since then started my own business (selling computer books on the net)
www.pricecutbook.co.uk


Comments and Discussions

 
GeneralWTL Pin
manosza10-Jan-09 10:28
manosza10-Jan-09 10:28 
How can I use MSDataGrid in a WTL/ATL project
without using MFC ?

Thanks,
Manos.
GeneralAdd checkboxes to a column(all rows) Pin
Aravind Kumar K31-May-07 1:45
Aravind Kumar K31-May-07 1:45 
QuestionDatagrid Pin
kazim bhai17-Apr-07 2:36
kazim bhai17-Apr-07 2:36 
GeneralNot Returning Row Set Pin
MIAN KAMRAN13-Feb-07 0:00
MIAN KAMRAN13-Feb-07 0:00 
GeneralProject-&gt;Add to project-&gt;Components and Controls Pin
User 35582525-Aug-04 23:20
User 35582525-Aug-04 23:20 
Generaldatagrid control Pin
hasnain014313-Dec-03 19:45
hasnain014313-Dec-03 19:45 
GeneralRowset is not bookmarkable Pin
Member 6941769-Nov-03 23:41
Member 6941769-Nov-03 23:41 
GeneralIncluding Check Box In DataGrid Vb.Net Pin
José Paulo Jorge11-Apr-03 4:36
José Paulo Jorge11-Apr-03 4:36 
GeneralUnable to add new row Pin
Mike_Dowey6-Dec-01 7:31
Mike_Dowey6-Dec-01 7:31 
GeneralRe: Unable to add new row Pin
Rashid Thadha6-Dec-01 10:46
Rashid Thadha6-Dec-01 10:46 
GeneralRe: Unable to add new row Pin
Mike_Dowey7-Dec-01 1:42
Mike_Dowey7-Dec-01 1:42 
GeneralRe: Unable to add new row Pin
Rashid Thadha8-Dec-01 6:44
Rashid Thadha8-Dec-01 6:44 
GeneralRe: Unable to add new row Pin
Mike_Dowey8-Dec-01 7:08
Mike_Dowey8-Dec-01 7:08 
GeneralRe: Unable to add new row Pin
CrackWei2-Sep-05 0:01
CrackWei2-Sep-05 0:01 
GeneralBig record set Pin
10-Oct-01 6:11
suss10-Oct-01 6:11 
GeneralRe: Big record set Pin
Rashid Thadha10-Oct-01 10:45
Rashid Thadha10-Oct-01 10:45 
QuestionDocumentation for SetRefDataSource? Pin
Paul Selormey23-Sep-01 16:14
Paul Selormey23-Sep-01 16:14 
AnswerRe: Documentation for SetRefDataSource? Pin
Rashid Thadha24-Sep-01 2:20
Rashid Thadha24-Sep-01 2:20 
GeneralRe: Documentation for SetRefDataSource? Pin
Paul Selormey24-Sep-01 2:55
Paul Selormey24-Sep-01 2:55 
GeneralRe: Documentation for SetRefDataSource? Pin
Rashid Thadha24-Sep-01 5:28
Rashid Thadha24-Sep-01 5:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.