Click here to Skip to main content
12,819,727 members (37,413 online)
Click here to Skip to main content
Add your own
alternative version


103 bookmarked
Posted 14 Feb 2000

Using the Grid Control in a Doc/View framework

, 29 Aug 2000 CPOL
Rate this:
Please Sign up or sign in to vote.
A simple tutorial that demonstrates how to use the grid control in a doc/view application.

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:
        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:
        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
            m_pGrid->Create(rect, this, 100);
            m_pGrid->SetRowCount(50);     // fill it up with stuff
            // ... 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:
        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.


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

You may also be interested in...


Comments and Discussions

GeneralCool Code Pin
DPLNeural21-Aug-14 16:25
memberDPLNeural21-Aug-14 16:25 
QuestionThe file has 0 bytes Pin
azolotnitsky1-Apr-12 4:19
memberazolotnitsky1-Apr-12 4:19 
Attached zip is empty - it has 0 bytes. Can you reupload the demo project?
AnswerRe: The file has 0 bytes Pin
Chris Maunder2-Apr-12 9:17
adminChris Maunder2-Apr-12 9:17 
QuestionHow to use in CFormView derived class ? Pin
Manmohan2925-Aug-11 1:09
memberManmohan2925-Aug-11 1:09 
GeneralCompile error at m_pGrid = new CGridCtrl; Pin
maetiop19-May-09 2:32
membermaetiop19-May-09 2:32 
GeneralRe: Compile error at m_pGrid = new CGridCtrl; Pin
maetiop19-May-09 2:45
membermaetiop19-May-09 2:45 
GeneralGridViewDemo VS 2008 a lot of errors when run this demo Pin
guru8826-Oct-08 11:35
memberguru8826-Oct-08 11:35 
GeneralRe: GridViewDemo VS 2008 a lot of errors when run this demo Pin
Brent Rauscher6-Apr-09 13:02
memberBrent Rauscher6-Apr-09 13:02 
GeneralRe: GridViewDemo VS 2008 a lot of errors when run this demo Pin
Chris Maunder6-Apr-09 13:03
adminChris Maunder6-Apr-09 13:03 
GeneralRe: GridViewDemo VS 2008 a lot of errors when run this demo Pin
Mohd Amanullah Zafar6-May-11 4:11
memberMohd Amanullah Zafar6-May-11 4:11 
GeneralThe download zip is corrupt Pin
andrewtruckle18-May-08 3:50
memberandrewtruckle18-May-08 3:50 
GeneralRe: The download zip is corrupt Pin
Chris Maunder18-May-08 10:34
adminChris Maunder18-May-08 10:34 
GeneralRe: The download zip is corrupt Pin
andrewtruckle18-May-08 10:37
memberandrewtruckle18-May-08 10:37 
Questionis there anyway to use without dialog-based and document/view? Pin
roroo21-Jun-07 8:30
memberroroo21-Jun-07 8:30 
QuestionTransparent Grid Control Pin
Parinda23-May-07 0:42
memberParinda23-May-07 0:42 
GeneralAfter adding a Custom Control, the Dialog does not display anymore Pin
AndyKiser20-Feb-07 0:48
memberAndyKiser20-Feb-07 0:48 
GeneralRe: After adding a Custom Control, the Dialog does not display anymore Pin
ronasando95-Apr-07 21:47
memberronasando95-Apr-07 21:47 
GeneralRe: After adding a Custom Control, the Dialog does not display anymore Pin
Member 833826021-Oct-11 2:18
memberMember 833826021-Oct-11 2:18 
GeneralCMyView::OnContextMenu(.....) won't be called. Pin
LeeeNN3-Jun-06 10:23
memberLeeeNN3-Jun-06 10:23 
QuestionCan't set the Item Pin
rosalite28-Apr-06 20:00
memberrosalite28-Apr-06 20:00 
Questionstarting the horizontal scrollbar control at a specified position to the left Pin
vkrishnamurthy23-Apr-06 21:25
membervkrishnamurthy23-Apr-06 21:25 
QuestionHow to load file? Pin
vanthe17-Feb-06 6:41
membervanthe17-Feb-06 6:41 
QuestionHow can use events when Text changes using it in a comboBox in view? Pin
Nam Sung-ho5-Jan-06 3:55
memberNam Sung-ho5-Jan-06 3:55 
GeneralHave a critical error when m_pGrid->Create(... Pin
Grey0077912-Dec-05 0:33
memberGrey0077912-Dec-05 0:33 
GeneralGetting Message to the CView Pin
Ron Gilbert14-Jun-05 10:48
memberRon Gilbert14-Jun-05 10:48 
GeneralRe: Getting Message to the CView Pin
NoName2316-Jun-05 5:41
memberNoName2316-Jun-05 5:41 
GeneralScrollbar not visible Pin
Muralidharan K.K26-Mar-05 0:47
memberMuralidharan K.K26-Mar-05 0:47 
GeneralOK, new problems. Pin
richiebabes1-Jun-04 7:07
memberrichiebabes1-Jun-04 7:07 
GeneralRe: OK, new problems. Pin
eldar_200123-Mar-05 9:59
membereldar_200123-Mar-05 9:59 
GeneralRe: OK, new problems. Pin
cc.caprani25-Aug-11 12:58
membercc.caprani25-Aug-11 12:58 
GeneralHelp - can't get it to work! Pin
richiebabes1-Jun-04 6:31
memberrichiebabes1-Jun-04 6:31 
GeneralRe: Help - can't get it to work! Pin
wilson_wanf5525-Jul-07 1:36
memberwilson_wanf5525-Jul-07 1:36 
GeneralDoes not work Pin
richiebabes31-May-04 11:07
memberrichiebabes31-May-04 11:07 
GeneralGood works. Pin
eric feng26-May-04 8:06
membereric feng26-May-04 8:06 
QuestionHowto capture messages for GridCtrl? Pin
ra_8210-May-04 3:40
memberra_8210-May-04 3:40 
GeneralQuery regarding the addition of Scroll bar to a Grid Contol. Pin
nandineeswari8-May-04 6:19
membernandineeswari8-May-04 6:19 
Generalmfc42u.lib File not found Pin
firehead10-Feb-04 0:21
sussfirehead10-Feb-04 0:21 
GeneralRe: mfc42u.lib File not found Pin
speedi12319-Feb-04 14:01
memberspeedi12319-Feb-04 14:01 
GeneralRe: mfc42u.lib File not found Pin
gaz6630-Mar-04 2:23
membergaz6630-Mar-04 2:23 
GeneralRe: mfc42u.lib File not found Pin
dyjin26-Sep-05 1:24
memberdyjin26-Sep-05 1:24 
GeneralRe: mfc42u.lib File not found Pin
s780822-Feb-04 21:20
members780822-Feb-04 21:20 
GeneralI Can't Run It Pin
firehead9-Feb-04 23:39
sussfirehead9-Feb-04 23:39 
QuestionHow can use events when using it in a view Pin
fill2-Oct-03 7:01
memberfill2-Oct-03 7:01 
AnswerRe: How can use events when using it in a view Pin
Cozumel992-Dec-03 4:33
memberCozumel992-Dec-03 4:33 
GeneralMessages with no dialog ID not passed to CView in later versions of Visual Studio Pin
darkhalf50117-Aug-12 16:42
memberdarkhalf50117-Aug-12 16:42 
QuestionHow to end edit before exit Pin
Phil Sherrod8-Sep-03 11:08
memberPhil Sherrod8-Sep-03 11:08 
GeneralProblem Editing Cell in virtual mode Pin
Dominik00778-Sep-03 5:58
memberDominik00778-Sep-03 5:58 
QuestionChris, Thanks. one thing.. Can I hide Row header? Pin
desafinado1-Sep-03 0:54
memberdesafinado1-Sep-03 0:54 
Generalm_pGridCtrl undeclared identifier Pin
mfcuser20-Aug-03 8:14
membermfcuser20-Aug-03 8:14 
GeneralRe: m_pGridCtrl undeclared identifier Pin
Chris Maunder20-Aug-03 12:35
adminChris Maunder20-Aug-03 12:35 

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.

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