Click here to Skip to main content
Click here to Skip to main content
Go to top

Using the Grid Control in a Doc/View framework

, 29 Aug 2000
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:
    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)

Share

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.
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
GeneralCool Code PinmemberDPLNeural21-Aug-14 15:25 
QuestionThe file has 0 bytes Pinmemberazolotnitsky1-Apr-12 3:19 
AnswerRe: The file has 0 bytes PinadminChris Maunder2-Apr-12 8:17 
QuestionHow to use in CFormView derived class ? PinmemberManmohan2925-Aug-11 0:09 
GeneralCompile error at m_pGrid = new CGridCtrl; Pinmembermaetiop19-May-09 1:32 
GeneralRe: Compile error at m_pGrid = new CGridCtrl; Pinmembermaetiop19-May-09 1:45 
GeneralGridViewDemo VS 2008 a lot of errors when run this demo Pinmemberguru8826-Oct-08 10:35 
GeneralRe: GridViewDemo VS 2008 a lot of errors when run this demo PinmemberBrent Rauscher6-Apr-09 12:02 
GeneralRe: GridViewDemo VS 2008 a lot of errors when run this demo PinadminChris Maunder6-Apr-09 12:03 
GeneralRe: GridViewDemo VS 2008 a lot of errors when run this demo PinmemberMohd 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 corrupt Pinmemberandrewtruckle18-May-08 2:50 
GeneralRe: The download zip is corrupt PinadminChris Maunder18-May-08 9:34 
GeneralRe: The download zip is corrupt Pinmemberandrewtruckle18-May-08 9:37 
Questionis there anyway to use without dialog-based and document/view? Pinmemberroroo21-Jun-07 7:30 
QuestionTransparent Grid Control PinmemberParinda22-May-07 23:42 
GeneralAfter adding a Custom Control, the Dialog does not display anymore PinmemberAndyKiser19-Feb-07 23:48 
GeneralRe: After adding a Custom Control, the Dialog does not display anymore Pinmemberronasando95-Apr-07 20:47 
GeneralRe: After adding a Custom Control, the Dialog does not display anymore PinmemberMember 833826021-Oct-11 1:18 
GeneralCMyView::OnContextMenu(.....) won't be called. PinmemberLeeeNN3-Jun-06 9:23 
QuestionCan't set the Item Pinmemberrosalite28-Apr-06 19:00 
Questionstarting the horizontal scrollbar control at a specified position to the left Pinmembervkrishnamurthy23-Apr-06 20:25 
QuestionHow to load file? Pinmembervanthe17-Feb-06 5:41 
QuestionHow can use events when Text changes using it in a comboBox in view? PinmemberNam Sung-ho5-Jan-06 2:55 
GeneralHave a critical error when m_pGrid->Create(... PinmemberGrey0077911-Dec-05 23:33 
GeneralGetting Message to the CView PinmemberRon Gilbert14-Jun-05 9:48 

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 | Mobile
Web02 | 2.8.140926.1 | Last Updated 30 Aug 2000
Article Copyright 2000 by Chris Maunder
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid