Click here to Skip to main content
Licence 
First Posted 1 Sep 2001
Views 301,965
Bookmarked 79 times

Using MS DataGrid control with ADO

By | 2 Sep 2001 | Article
Use the MS DataGrid control in your C++ app with ADO
 
Part of The SQL Zone sponsored by
See Also

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

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

Conclusion

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

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

About the Author

Rashid Thadha

Web Developer

United Kingdom United Kingdom

Member

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
 


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 3 Pinmemberbuyong21:11 10 Nov '11  
Generaldao.ocx dbgrid.ocx PinmemberMember 280963710:18 1 Sep '10  
Questionadd column number in DataGrid control Pinmemberauliac5:58 26 Jan '10  
GeneralWTL/ATL Pinmembermanosza21:04 10 Jan '09  
QuestionMicrosoft DataGrid Control, Version 6 (OLEDB) PinmemberMichael B Pliam13:28 2 Apr '08  
AnswerRe: Microsoft DataGrid Control, Version 6 (OLEDB) PinmemberMartaFdez1:47 9 Dec '09  
GeneralRe: Microsoft DataGrid Control, Version 6 (OLEDB) PinmemberMichael B Pliam14:02 11 Dec '09  
QuestionHow can you print this grid???? Pinmemberjani0122:13 20 Nov '07  
QuestionProblem with CDataGrid Pinmembereryreyeryeryeryeyryye4:16 16 Apr '07  
GeneralProblem with visual C++ 7.1 / 2003 Pinmembereryreyeryeryeryeyryye0:03 16 Apr '07  
QuestionSample to work with MS Access ? Pinmemberbujal16:16 21 Mar '07  
AnswerRe: Sample to work with MS Access ? Pinmembereryreyeryeryeryeyryye23:45 15 Apr '07  
GeneralUpdate recordset via grid. PinmembermishkaU5:06 22 Jan '06  
GeneralData Grid PinsussAnonymous22:39 28 Aug '05  
QuestionHow to delete one row? PinsussAnonymous2:56 2 Aug '05  
GeneralChange the color of one cell PinmemberAlundra025:23 15 Apr '05  
How can i cange the color of one ore more cell in the data grid .
for exemple in one table i have there's one for storing foalt data so what i want is:
when i retrieve the data from the table i''ll test the values if it's above for exapmle 11.12 i want to view this value in the datagrind in yellow
how can i do it??????Confused | :confused:
 
Bakhocha@hotmail.com (My email)
QuestionCan i add a row to datagrid? Pinmemberzimli22:44 30 Dec '04  
Generala stupid question: why doesn't confirm dialog come out :( PinmemberDick.lee Solucky18:24 23 Nov '04  
Generalask question in a right way... PinmemberDick.lee Solucky18:19 24 Nov '04  
Generalsomething more... PinmemberDick.lee Solucky19:15 24 Nov '04  
Generala stupid question again PinmemberDick.lee Solucky20:56 24 Nov '04  
QuestionCan i click one of each field record in the datagrid and then the record apear in the text box? PinsussAnonymous16:02 23 Nov '04  
GeneralUnable to bind the recordset to the DataGrid PinsussBrandon Jensen20:03 5 Nov '04  
Generali need to update records using datagrid Pinmemberjaisriram21:53 17 Sep '04  
QuestionHow to bind datasource to DataList Pinmembersoccomo15:10 23 Jul '04  

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.

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