Click here to Skip to main content
11,410,174 members (61,530 online)
Click here to Skip to main content

Using MS DataGrid control with ADO

, 2 Sep 2001
Rate this:
Please Sign up or sign in to vote.
Use the MS DataGrid control in your C++ app with ADO

Sample Image - msdatagrid.gif


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

CDataGrid m_ctlDataGrid;
void CDataGridView::DoDataExchange(CDataExchange* pDX)
    DDX_Control(pDX, IDC_DATAGRID1, m_ctlDataGrid);

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.

void CDataGridView::UpdateGridDetails(const CString& sTableName)
    CMainFrame* pMainFrame = reinterpret_cast<CMAINFRAME*>(AfxGetMainWnd());
    if (pMainFrame)
        m_pRS = NULL; 
            m_pRS->CursorLocation= adUseClient;
                adOpenKeyset,adLockOptimistic, adCmdTable);
	    catch (_com_error &e)
    	//Demonstrates, how to populate DataGrid 
    	//by assigning it a Recordset object.
        m_ctlDataGrid.SetRefDataSource( (LPUNKNOWN) m_pRS );

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


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


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


About the Author

Rashid Thadha
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)

Comments and Discussions

Questionhow to add combobox to DataGrid in VC++ Pinmemberpangziququ323-Nov-12 20:37 
GeneralMy vote of 3 Pinmemberbuyong10-Nov-11 22:11 
Generaldao.ocx dbgrid.ocx PinmemberMember 28096371-Sep-10 11:18 
Questionadd column number in DataGrid control Pinmemberauliac26-Jan-10 6:58 
GeneralWTL/ATL Pinmembermanosza10-Jan-09 22:04 
QuestionMicrosoft DataGrid Control, Version 6 (OLEDB) PinmemberMichael B Pliam2-Apr-08 14:28 
AnswerRe: Microsoft DataGrid Control, Version 6 (OLEDB) PinmemberMartaFdez9-Dec-09 2:47 
GeneralRe: Microsoft DataGrid Control, Version 6 (OLEDB) PinmemberMichael B Pliam11-Dec-09 15:02 
QuestionHow can you print this grid???? Pinmemberjani0120-Nov-07 23:13 
QuestionProblem with CDataGrid Pinmembereryreyeryeryeryeyryye16-Apr-07 5:16 
GeneralProblem with visual C++ 7.1 / 2003 Pinmembereryreyeryeryeryeyryye16-Apr-07 1:03 
QuestionSample to work with MS Access ? Pinmemberbujal21-Mar-07 17:16 
AnswerRe: Sample to work with MS Access ? Pinmembereryreyeryeryeryeyryye16-Apr-07 0:45 
GeneralUpdate recordset via grid. PinmembermishkaU22-Jan-06 6:06 
GeneralData Grid PinsussAnonymous28-Aug-05 23:39 
QuestionHow to delete one row? PinsussAnonymous2-Aug-05 3:56 
GeneralChange the color of one cell PinmemberAlundra0215-Apr-05 6:23 
QuestionCan i add a row to datagrid? Pinmemberzimli30-Dec-04 23:44 
Generala stupid question: why doesn't confirm dialog come out :( PinmemberDick.lee Solucky23-Nov-04 19:24 
Generalask question in a right way... PinmemberDick.lee Solucky24-Nov-04 19:19 
Generalsomething more... PinmemberDick.lee Solucky24-Nov-04 20:15 
Generala stupid question again PinmemberDick.lee Solucky24-Nov-04 21:56 
QuestionCan i click one of each field record in the datagrid and then the record apear in the text box? PinsussAnonymous23-Nov-04 17:02 
GeneralUnable to bind the recordset to the DataGrid PinsussBrandon Jensen5-Nov-04 21:03 
Generali need to update records using datagrid Pinmemberjaisriram17-Sep-04 22:53 
QuestionHow to bind datasource to DataList Pinmembersoccomo23-Jul-04 16:10 
GeneralERROR:&quot;cannot initialise databinding&quot; in MSdatgrd.ocx Pinmembergreenolive_bing26-May-04 22:11 
QuestionHow can i add a column ?? Pinmembermarsu7312-May-04 13:28 
GeneralMSDATAGRD not work in .NET Pinmemberjava3051-Apr-04 2:32 
GeneralNot Loading Pinmemberslrao7-Dec-03 19:18 
GeneralRe: Not Loading Pinmemberalim14-Feb-04 20:18 
GeneralRe: Not Loading Pinmemberdaburke8-Mar-04 5:00 
QuestionCan I use dataGrid to enter data from the screen PinsussAnonymous28-Nov-03 17:39 
GeneralHand Cursor on Datagrid columnHeader and Caption PinmemberNeeraj Jain18-Nov-03 13:15 
QuestionHow to make query using LIKE operator in MFC PinsussAnonymous25-Sep-03 22:07 
AnswerRe: How to make query using LIKE operator in MFC Pinmemberfyrewolfe30-Sep-03 4:54 
QuestionHow to cancel insert? Pinmemberkorgan23-Sep-03 23:22 
GeneralOnClick Event in MSDATGRD.OCX PinmemberDAVELSITER1118-Aug-03 1:36 
GeneralOnClick Event in MSDATGRD.OCX PinmemberDAVELSITER1118-Aug-03 1:35 
GeneralERROR:&quot;cannot initialise databinding&quot; in MSdatgrd.ocx Pinmemberarunacalls24-Jul-03 1:24 
GeneralRe: ERROR:&quot;cannot initialise databinding&quot; in MSdatgrd.ocx PinmemberDAVELSITER1118-Aug-03 1:29 
QuestionDataGrid in NON-MFC world? Pinsussdanthu28-Mar-03 9:44 
AnswerRe: DataGrid in NON-MFC world? PinmemberRashid Thadha28-Mar-03 10:26 
GeneralAdoDataGrid sample in MSDN Pinmembernomadik21-Feb-03 5:43 
Questionhighlight row from code? PinmemberJuergen Klingler25-Nov-02 22:30 
AnswerRe: highlight row from code? Pinmemberlijp16-Jan-03 0:58 
GeneralRe: highlight row from code? PinsussAnonymous22-Jan-03 20:14 
QuestionDataGrid in Access? PinsussHookkick806-Oct-02 15:41 
AnswerRe: DataGrid in Access? PinmemberKRS-SSD9-Sep-03 2:17 
GeneralCalling DataGrid Control in Web Page PinmemberMohammad Afzal15-Jun-02 17:57 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150414.5 | Last Updated 3 Sep 2001
Article Copyright 2001 by Rashid Thadha
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid