Click here to Skip to main content
15,881,248 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.1K   17.5K   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 can I change tha table that is related with a datagrid control? Pin
29-May-02 4:37
suss29-May-02 4:37 
GeneralMS Data Grid and the Data Environment Pin
michael watt16-May-02 13:22
michael watt16-May-02 13:22 
QuestionMSDataGrid and Delphi? Pin
12-Mar-02 6:59
suss12-Mar-02 6:59 
AnswerRe: MSDataGrid and Delphi? Pin
Douglas Troy12-Mar-02 7:19
Douglas Troy12-Mar-02 7:19 
GeneralRe: MSDataGrid and Delphi? Pin
15-Mar-02 4:32
suss15-Mar-02 4:32 
GeneralUnicde and grid Pin
11-Mar-02 6:05
suss11-Mar-02 6:05 
GeneralRe: Unicde and grid Pin
12-Mar-02 6:56
suss12-Mar-02 6:56 
Questionhow can I make search function with datagrid output Pin
15-Feb-02 2:17
suss15-Feb-02 2:17 
GeneralAddNew, Edit,Delete-DataGrid rows Pin
harirk5-Feb-02 1:03
harirk5-Feb-02 1:03 
GeneralRe: AddNew, Edit,Delete-DataGrid rows Pin
Mazdak5-Feb-02 2:09
Mazdak5-Feb-02 2:09 
GeneralRe: AddNew, Edit,Delete-DataGrid rows Pin
Carlos Antollini5-Feb-02 3:01
Carlos Antollini5-Feb-02 3:01 
Generalreformating the column Pin
Ivan Yap5-Dec-01 18:09
Ivan Yap5-Dec-01 18:09 
GeneralDataGrid and DAO ! Pin
Hadi Rezaee28-Nov-01 6:43
Hadi Rezaee28-Nov-01 6:43 
GeneralRe: DataGrid and DAO ! Pin
Rashid Thadha28-Nov-01 11:08
Rashid Thadha28-Nov-01 11:08 
GeneralRe: DataGrid and DAO ! Pin
David7911-Apr-02 7:56
David7911-Apr-02 7:56 
GeneralRe: DataGrid and DAO ! Pin
quanchong22-Nov-02 2:36
quanchong22-Nov-02 2:36 
GeneralMSDataGrid: Error setting dividing lines Pin
gylu24-Nov-01 15:28
gylu24-Nov-01 15:28 
GeneralRe: MSDataGrid: Error setting dividing lines Pin
korgan22-May-03 3:45
korgan22-May-03 3:45 
Generalcompile error with import dlls Pin
gylu17-Nov-01 4:39
gylu17-Nov-01 4:39 
GeneralRe: compile error with import dlls Pin
Rashid Thadha19-Nov-01 10:33
Rashid Thadha19-Nov-01 10:33 
GeneralRe: compile error with import dlls Pin
gylu24-Nov-01 15:18
gylu24-Nov-01 15:18 
GeneralAllowAddNew Pin
18-Oct-01 3:27
suss18-Oct-01 3:27 
GeneralRe: AllowAddNew Pin
Rashid Thadha20-Oct-01 12:03
Rashid Thadha20-Oct-01 12:03 
GeneralRe: AllowAddNew Pin
Shehzad Anjum16-Nov-02 20:55
sussShehzad Anjum16-Nov-02 20:55 
GeneralDataGrid Fails in MDAC2.6 Pin
16-Oct-01 16:50
suss16-Oct-01 16:50 

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.