Click here to Skip to main content
15,881,882 members
Articles / Desktop Programming / MFC

Using MS DataGrid Control with ADO

Rate me:
Please Sign up or sign in to vote.
4.91/5 (18 votes)
2 Sep 2001CPOL3 min read 486.2K   17.6K   88   82
Use the MS DataGrid control in your C++ app with ADO

Sample Image - msdatagrid.gif

Introduction

The Microsoft DataGrid is usually seen associated with VB when you start searching for information on how to implement the MS DataGrid in Visual C++. There is not a lot of information out there and therefore many Visual C++ programmers tend not to use this excellent and easy to use database control.

This is a simple implementation that shows you how to use the MS DataGrid control with ADO. 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 on 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 ADO (ActiveX Data Object) is also required to understand the binding process to the DataGrid.

Using ADO

In order to use the ADO COM object, you will need to import the following type libraries, this should be done in your stdafx.h file.

#import msado15.dll  //This contains the ADO Type Library.
#import Oledb32.dll  //this contains the Data Source Locator COM Interface

Please note that you may need to add the full path to these DLLs or you can add the path through your visual C++ environment via:

Tools->Options->Directories->Include Files

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 (OLEDB).

Registered Controls - msdatagrid2.gif

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

Insert Classes - msdatagrid3.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.

C++
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 ADO

You have to bind the DataGrid at run-time using an ADO Recordset. The following code gives you the example used.

C++
void CDataGridView::UpdateGridDetails(const CString& sTableName)
{
    CMainFrame* pMainFrame = reinterpret_cast<CMAINFRAME*>(AfxGetMainWnd());
    if (pMainFrame)
    {
        m_pRS = NULL; 
        m_pRS.CreateInstance(__uuidof(Recordset));
        try  
        {
            m_pRS->CursorLocation= adUseClient;
            m_pRS->Open((LPCSTR)sTableName,(LPCSTR)pMainFrame->
                m_ptrConnection->GetConnectionString(), 
                adOpenKeyset,adLockOptimistic, adCmdTable);
        }
	    catch (_com_error &e)
        {
            AfxMessageBox(GetErrorDescription(e));
        }
	    
    	//Demonstrates, how to populate DataGrid 
    	//by assigning it a Recordset object.
	    m_ctlDataGrid.SetCaption(sTableName);
        m_ctlDataGrid.SetRefDataSource(NULL);
        m_ctlDataGrid.SetRefDataSource( (LPUNKNOWN) m_pRS );
        m_ctlDataGrid.Refresh();
	    
        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 cannot be done in the grid. For 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 Microsoft Access (this is not required in SQL Server).

Conclusion

The MS DataGrid control is straightforward to use as long as you know ADO.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


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

 
QuestionHow to bind datasource to DataList Pin
soccomo23-Jul-04 15:10
soccomo23-Jul-04 15:10 
GeneralERROR:&quot;cannot initialise databinding&quot; in MSdatgrd.ocx Pin
greenolive_bing26-May-04 21:11
greenolive_bing26-May-04 21:11 
QuestionHow can i add a column ?? Pin
Member 99716812-May-04 12:28
Member 99716812-May-04 12:28 
GeneralMSDATAGRD not work in .NET Pin
java3051-Apr-04 1:32
java3051-Apr-04 1:32 
GeneralNot Loading Pin
slrao7-Dec-03 18:18
slrao7-Dec-03 18:18 
GeneralRe: Not Loading Pin
alim14-Feb-04 19:18
alim14-Feb-04 19:18 
GeneralRe: Not Loading Pin
daburke8-Mar-04 4:00
daburke8-Mar-04 4:00 
QuestionCan I use dataGrid to enter data from the screen Pin
Anonymous28-Nov-03 16:39
Anonymous28-Nov-03 16:39 
GeneralHand Cursor on Datagrid columnHeader and Caption Pin
Neeraj Jain18-Nov-03 12:15
Neeraj Jain18-Nov-03 12:15 
QuestionHow to make query using LIKE operator in MFC Pin
Anonymous25-Sep-03 21:07
Anonymous25-Sep-03 21:07 
AnswerRe: How to make query using LIKE operator in MFC Pin
fyrewolfe30-Sep-03 3:54
professionalfyrewolfe30-Sep-03 3:54 
QuestionHow to cancel insert? Pin
korgan23-Sep-03 22:22
korgan23-Sep-03 22:22 
GeneralOnClick Event in MSDATGRD.OCX Pin
DAVELSITER1118-Aug-03 0:36
DAVELSITER1118-Aug-03 0:36 
GeneralOnClick Event in MSDATGRD.OCX Pin
DAVELSITER1118-Aug-03 0:35
DAVELSITER1118-Aug-03 0:35 
GeneralERROR:&quot;cannot initialise databinding&quot; in MSdatgrd.ocx Pin
arunacalls24-Jul-03 0:24
arunacalls24-Jul-03 0:24 
GeneralRe: ERROR:&quot;cannot initialise databinding&quot; in MSdatgrd.ocx Pin
DAVELSITER1118-Aug-03 0:29
DAVELSITER1118-Aug-03 0:29 
QuestionDataGrid in NON-MFC world? Pin
danthu28-Mar-03 8:44
sussdanthu28-Mar-03 8:44 
AnswerRe: DataGrid in NON-MFC world? Pin
Rashid Thadha28-Mar-03 9:26
Rashid Thadha28-Mar-03 9:26 
GeneralAdoDataGrid sample in MSDN Pin
nomadik21-Feb-03 4:43
nomadik21-Feb-03 4:43 
Questionhighlight row from code? Pin
Orgen Kl25-Nov-02 21:30
Orgen Kl25-Nov-02 21:30 
AnswerRe: highlight row from code? Pin
lijp15-Jan-03 23:58
lijp15-Jan-03 23:58 
GeneralRe: highlight row from code? Pin
Anonymous22-Jan-03 19:14
Anonymous22-Jan-03 19:14 
QuestionDataGrid in Access? Pin
hookkick806-Oct-02 14:41
hookkick806-Oct-02 14:41 
AnswerRe: DataGrid in Access? Pin
KRS-SSD9-Sep-03 1:17
KRS-SSD9-Sep-03 1:17 
GeneralCalling DataGrid Control in Web Page Pin
15-Jun-02 16:57
suss15-Jun-02 16:57 

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.