Click here to Skip to main content
Click here to Skip to main content

Using the Grid Control in a Doc/View framework

By , 29 Aug 2000
 

gridctrl example image

I have had many, MANY questions asking how to use my MFC grid control in a view instead of in a dialog, so hopefully this will help.

The easiest way as I see it is as follows:

  1. Add a member variable of type CGridCtrl* to your view class:
    CGridCtrl* m_pGrid;
  2. Initialise this to NULL in your view class' constructor:
    CMyView::CMyView
    {
        m_pGrid = NULL;
    }
    
  3. In the CView function OnInitialUpdate, create a new CGridCtrl object if the m_pGrid is not NULL, and then create the CGridCtrl window:
    CMyView::OnInitialUpdate
    {
        CView::OnInitialUpdate();
    
        if (m_pGrid == NULL)             // Have we already done this bit?
        {
            m_pGrid = new CGridCtrl;     // Create the Gridctrl object
            if (!m_pGrid ) return;
    
            CRect rect;                  // Create the Gridctrl window
            GetClientRect(rect);
            m_pGrid->Create(rect, this, 100);
    
            m_pGrid->SetRowCount(50);     // fill it up with stuff
            m_pGrid->SetColumnCount(10);
            
            // ... etc
        }
    }

    This allows the view to be reused (eg SDI situations).

  4. We want the grid to take up the whole of the view's client space, so add a handler to the WM_SIZE message for the view and edit the OnSize function thus:
    CMyView::OnSize(UINT nType, int cx, int cy) 
    {
        CView::OnSize(nType, cx, cy);
        
        if (m_pGrid->GetSafeHwnd())     // Have the grid object and window 
        {                               // been created yet?
            CRect rect;
            GetClientRect(rect);        // Get the size of the view's client
                                        // area
            m_pGrid->MoveWindow(rect);  // Resize the grid to take up that 
                                        // space.
        }
    }
    
  5. Remember to delete the object when you are done:
    CMyView::~CMyView
    {
        delete m_pGrid;
    }
    
  6. You may want to also add an OnCmdMsg overide to your view class and let the grid control have first go at the messages (this will allow commands such as ID_EDIT_COPY to be wired in automatically:
    BOOL CMyView::OnCmdMsg(UINT nID, int nCode, void* pExtra, 
                           AFX_CMDHANDLERINFO* pHandlerInfo) 
    {
        if (m_pGrid && IsWindow(m_pGrid->m_hWnd))
            if (m_pGrid->OnCmdMsg(nID, nCode, pExtra, pHandlerInfo))
                return TRUE;
    
        return CView::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
    }
    

If you want print preview, then check out Koay Kah Hoe's article Print Previewing without the Document/View Framework.

License

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

About the Author

Chris Maunder
Founder CodeProject
Canada Canada
Member
Chris is the Co-founder, Administrator, Architect, Chief Editor and Shameless Hack who wrote and runs The Code Project. He's been programming since 1988 while pretending to be, in various guises, an astrophysicist, mathematician, physicist, hydrologist, geomorphologist, defence intelligence researcher and then, when all that got a bit rough on the nerves, a web developer. He is a Microsoft Visual C++ MVP both globally and for Canada locally.
 
His programming experience includes C/C++, C#, SQL, MFC, ASP, ASP.NET, and far, far too much FORTRAN. He has worked on PocketPCs, AIX mainframes, Sun workstations, and a CRAY YMP C90 behemoth but finds notebooks take up less desk space.
 
He dodges, he weaves, and he never gets enough sleep. He is kind to small animals.
 
Chris was born and bred in Australia but splits his time between Toronto and Melbourne, depending on the weather. For relaxation he is into road cycling, snowboarding, rock climbing, and storm chasing.

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.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionThe file has 0 bytesmemberazolotnitsky1 Apr '12 - 3:19 
Attached zip is empty - it has 0 bytes. Can you reupload the demo project?
AnswerRe: The file has 0 bytesadminChris Maunder2 Apr '12 - 8:17 
I've updated the download to the latest zip. Sorry about that.
cheers,
Chris Maunder
 
The Code Project | Co-founder
Microsoft C++ MVP

QuestionHow to use in CFormView derived class ?memberManmohan2925 Aug '11 - 0:09 
I have two views ( CMainView and CMembersView ) in my SDI application. The code below resizes my MFC GridCtrl according to size of app's window when the view is initialized.
I get valid values for CRect grid_rect {top=0 bottom=952 left=0 right=1819} in CMainView. But in CMemberView I am having some undesired values.
 
This is working fine :-
 
void CMainView::OnInitialUpdate() // CMainView is my application's default view
{
CFormView::OnInitialUpdate();

initialized = TRUE;
CWnd *pGridCtrl = GetDlgItem(IDC_SALEPURCHASEGRIDCTRL);
CRect rect,grid_rect;

CWnd *pWnd = this->GetOwner();
pWnd->GetClientRect(rect);


pGridCtrl->GetWindowRect(grid_rect); // works fine here
grid_rect = rect;
grid_rect.left += 16;
grid_rect.top += 150;
grid_rect.bottom -= 300;
grid_rect.right -= 16;

pGridCtrl->MoveWindow(grid_rect); // GridCtrl is resized properly
.
.
.
 

This is not working properly:-
 
void CMembersView::OnInitialUpdate()
{

CFormView::OnInitialUpdate();

initialized = TRUE;
CWnd *pGridCtrl = this->GetDlgItem(IDC_MEMBERSGRID);
ASSERT(pGridCtrl); // Must exist
 
CRect rect,grid_rect;

CWnd *pWnd = this->GetOwner();
pWnd->GetClientRect(rect); // Also tried with &rect

pGridCtrl->GetWindowRect(grid_rect); // problem is here. no grid_rect coordinates
// grid_rect {top=-32639 bottom=-32483 left=-32709 right=-31884} CRect

grid_rect = rect;
grid_rect.left += 16;
grid_rect.top += 150;
grid_rect.bottom -= 300;
grid_rect.right -= 16;

pGridCtrl->MoveWindow(grid_rect); // Also tried with &grid_rect
.
.
.
 
Confused | :confused:
---------------------------------------------
" Future Lies in Present "Manmohan Bishnoi
 

GeneralCompile error at m_pGrid = new CGridCtrl;membermaetiop19 May '09 - 1:32 
I follow the instruction, but get compile error. Who could tell me how to resolve this problem?
CTestGridView::OnInitialUpdate
{
    CView::OnInitialUpdate();
 
    if (m_pGrid == NULL)             // Have we already done this bit?
    {
        m_pGrid = new CGridCtrl;     // Create the Gridctrl object
        if (!m_pGrid ) return;
 
        CRect rect;                  // Create the Gridctrl window
        GetClientRect(rect);
        m_pGrid->Create(rect, this, 100);
 
        ...
        ...        
        // ... etc
    }
}
 
Error 2 error C2512: 'CGridCtrl' : no appropriate default constructor available
 
Thanks for help!
GeneralRe: Compile error at m_pGrid = new CGridCtrl;membermaetiop19 May '09 - 1:45 
I change the declaration of CGridCtrl in the header file, then it bulids OK.
 
change from
 
class CGridCtrl;
 
to
 
#include "Gridctrl.h"
GeneralGridViewDemo VS 2008 a lot of errors when run this demomemberguru8826 Oct '08 - 10:35 
------ Build started: Project: GridViewDemo, Configuration: Debug Win32 ------
Compiling...
StdAfx.cpp
WINVER not defined. Defaulting to 0x0600 (Windows Vista)
Compiling...
GridCell.cpp
GridCellBase.cpp
GridCtrl.cpp
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(425) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(425) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(425) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(426) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(1952) : error C2065: 'pos' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(1952) : error C2065: 'pos' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(1956) : error C2065: 'pos' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(2214) : warning C4244: 'initializing' : conversion from 'ULONGLONG' to 'DWORD', possible loss of data
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(2618) : error C2065: 'col' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(2630) : error C2065: 'col' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(2633) : error C2065: 'col' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(2649) : error C2065: 'row' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(2661) : error C2065: 'row' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(2664) : error C2065: 'row' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(2716) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(2716) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(2720) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(2720) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(2720) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(2722) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(2729) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(2729) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(2756) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(2756) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(2762) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(2762) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(2762) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(2764) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(2768) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(2768) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(3192) : error C2065: 'j' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(3192) : error C2065: 'j' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(3192) : error C2065: 'j' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(3194) : error C2065: 'j' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(3194) : error C2065: 'j' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(3195) : error C2065: 'j' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(3196) : error C2065: 'j' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(3246) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(3246) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(3246) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(3249) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(3249) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(3250) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(3251) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(3412) : error C2065: 'col' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(3412) : error C2065: 'col' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(3412) : error C2065: 'col' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(3416) : error C2065: 'col' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(3416) : error C2065: 'col' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(3490) : error C2668: 'CUIntArray::InsertAt' : ambiguous call to overloaded function
c:\program files\microsoft visual studio 9.0\vc\atlmfc\include\afxcoll.h(336): could be 'void CUIntArray::InsertAt(INT_PTR,CUIntArray *)'
c:\program files\microsoft visual studio 9.0\vc\atlmfc\include\afxcoll.h(333): or 'void CUIntArray::InsertAt(INT_PTR,UINT,INT_PTR)'
while trying to match the argument list '(int, int)'
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(3565) : error C2668: 'CUIntArray::InsertAt' : ambiguous call to overloaded function
c:\program files\microsoft visual studio 9.0\vc\atlmfc\include\afxcoll.h(336): could be 'void CUIntArray::InsertAt(INT_PTR,CUIntArray *)'
c:\program files\microsoft visual studio 9.0\vc\atlmfc\include\afxcoll.h(333): or 'void CUIntArray::InsertAt(INT_PTR,UINT,INT_PTR)'
while trying to match the argument list '(int, int)'
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(4739) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(4739) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(4739) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(4740) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(4745) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(4745) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(4745) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(4746) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(4806) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(4806) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(4806) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(4807) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(4812) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(4812) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(4812) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(4813) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(6559) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(6559) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(6559) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(6563) : error C2065: 'i' : undeclared identifier
GridDropTarget.cpp
InPlaceEdit.cpp
TitleTip.cpp
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\titletip.cpp(314) : error C2360: initialization of 'pts' is skipped by 'case' label
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\titletip.cpp(275) : see declaration of 'pts'
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\titletip.cpp(315) : error C2360: initialization of 'pts' is skipped by 'case' label
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\titletip.cpp(275) : see declaration of 'pts'
ChildFrm.cpp
GridViewDemo.cpp
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_in_view\gridviewdemo.cpp(61) : warning C4996: 'CWinApp::Enable3dControls': CWinApp::Enable3dControls is no longer needed. You should remove this call.
c:\program files\microsoft visual studio 9.0\vc\atlmfc\include\afxwin.h(4818) : see declaration of 'CWinApp::Enable3dControls'
GridViewDemoDoc.cpp
GridViewDemoView.cpp
MainFrm.cpp
Generating Code...
Build log was saved at "file://C:\Documents and Settings\Guru\Pulpit\gridctrl_demo221\gridctrl_in_view\Debug\BuildLog.htm"
GridViewDemo - 72 error(s), 2 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
 
How to fix it? Please help...
GeneralRe: GridViewDemo VS 2008 a lot of errors when run this demomemberBrent Rauscher6 Apr '09 - 12:02 
I concur - the same error list at compile time from Visual Studio C++. This code is NOT designed for this compiler, but I cannot tell where it is supposed to work. If these incidental bugs are removed, the run-time is EVEN WORSE. This is a very pretty utility that I REALLY WANT, but it is wasting days of my time and yields nothing.
 
NOTICE: Please DO NOT USE this code!!!!!!!!!!!!!!! It does not work - stay very FAR AWAY....
GeneralRe: GridViewDemo VS 2008 a lot of errors when run this demoadminChris Maunder6 Apr '09 - 12:03 
The code works fine on the compiler it was built and tested on. I can only apologise that I've not had a chance to update it for newer compilers.
 
Please do a search on this site for 'Ultimate Grid' for a more up to date grid.
 
cheers,
Chris Maunder
 
The Code Project Co-founder
Microsoft C++ MVP

GeneralRe: GridViewDemo VS 2008 a lot of errors when run this demomemberMohd Amanullah Zafar6 May '11 - 3:11 
This is because of a VC++ properties for Loop.
go to Property Pages --> C/C++ --> Language --> Set No to "Force Conformance in For Loop Scope".
SHould Fix this Error.
GeneralThe download zip is corruptmemberandrewtruckle18 May '08 - 2:50 
Zip is corrupt

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 30 Aug 2000
Article Copyright 2000 by Chris Maunder
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid