Click here to Skip to main content
15,881,715 members
Articles / Mobile Apps / Windows Mobile

Using the Grid Control in a Doc/View framework

Rate me:
Please Sign up or sign in to vote.
4.78/5 (25 votes)
29 Aug 2000CPOL 353.8K   4.2K   104   110
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)


Written By
Founder CodeProject
Canada Canada
Chris Maunder is the co-founder of CodeProject and ContentLab.com, and has been a prominent figure in the software development community for nearly 30 years. Hailing from Australia, Chris has a background in Mathematics, Astrophysics, Environmental Engineering and Defence Research. His programming endeavours span everything from FORTRAN on Super Computers, C++/MFC on Windows, through to to high-load .NET web applications and Python AI applications on everything from macOS to a Raspberry Pi. Chris is a full-stack developer who is as comfortable with SQL as he is with CSS.

In the late 1990s, he and his business partner David Cunningham recognized the need for a platform that would facilitate knowledge-sharing among developers, leading to the establishment of CodeProject.com in 1999. Chris's expertise in programming and his passion for fostering a collaborative environment have played a pivotal role in the success of CodeProject.com. Over the years, the website has grown into a vibrant community where programmers worldwide can connect, exchange ideas, and find solutions to coding challenges. Chris is a prolific contributor to the developer community through his articles and tutorials, and his latest passion project, CodeProject.AI.

In addition to his work with CodeProject.com, Chris co-founded ContentLab and DeveloperMedia, two projects focussed on helping companies make their Software Projects a success. Chris's roles included Product Development, Content Creation, Client Satisfaction and Systems Automation.

Comments and Discussions

 
GeneralRe: Getting Message to the CView Pin
NoName2316-Jun-05 4:41
NoName2316-Jun-05 4:41 
GeneralScrollbar not visible Pin
Muralidharan K.K25-Mar-05 23:47
Muralidharan K.K25-Mar-05 23:47 
GeneralOK, new problems. Pin
richiebabes1-Jun-04 6:07
richiebabes1-Jun-04 6:07 
GeneralRe: OK, new problems. Pin
eldar_200123-Mar-05 8:59
eldar_200123-Mar-05 8:59 
GeneralRe: OK, new problems. Pin
cc.caprani25-Aug-11 11:58
cc.caprani25-Aug-11 11:58 
GeneralHelp - can't get it to work! Pin
richiebabes1-Jun-04 5:31
richiebabes1-Jun-04 5:31 
GeneralRe: Help - can't get it to work! Pin
wilson_wanf5525-Jul-07 0:36
wilson_wanf5525-Jul-07 0:36 
GeneralDoes not work Pin
richiebabes31-May-04 10:07
richiebabes31-May-04 10:07 
GeneralGood works. Pin
eric feng26-May-04 7:06
eric feng26-May-04 7:06 
QuestionHowto capture messages for GridCtrl? Pin
ra_8210-May-04 2:40
ra_8210-May-04 2:40 
GeneralQuery regarding the addition of Scroll bar to a Grid Contol. Pin
nandineeswari8-May-04 5:19
nandineeswari8-May-04 5:19 
Generalmfc42u.lib File not found Pin
firehead9-Feb-04 23:21
firehead9-Feb-04 23:21 
GeneralRe: mfc42u.lib File not found Pin
speedi12319-Feb-04 13:01
speedi12319-Feb-04 13:01 
GeneralRe: mfc42u.lib File not found Pin
gaz6630-Mar-04 1:23
gaz6630-Mar-04 1:23 
GeneralRe: mfc42u.lib File not found Pin
dyjin26-Sep-05 0:24
dyjin26-Sep-05 0:24 
GeneralRe: mfc42u.lib File not found Pin
s780822-Feb-04 20:20
s780822-Feb-04 20:20 
GeneralI Can't Run It Pin
firehead9-Feb-04 22:39
firehead9-Feb-04 22:39 
QuestionHow can use events when using it in a view Pin
fill2-Oct-03 6:01
fill2-Oct-03 6:01 
AnswerRe: How can use events when using it in a view Pin
Cozumel992-Dec-03 3:33
Cozumel992-Dec-03 3:33 
GeneralMessages with no dialog ID not passed to CView in later versions of Visual Studio Pin
darkhalf50117-Aug-12 15:42
darkhalf50117-Aug-12 15:42 
QuestionHow to end edit before exit Pin
Phil Sherrod8-Sep-03 10:08
Phil Sherrod8-Sep-03 10:08 
GeneralProblem Editing Cell in virtual mode Pin
Dominik00778-Sep-03 4:58
Dominik00778-Sep-03 4:58 
QuestionChris, Thanks. one thing.. Can I hide Row header? Pin
desafinado31-Aug-03 23:54
desafinado31-Aug-03 23:54 
Generalm_pGridCtrl undeclared identifier Pin
mfcuser20-Aug-03 7:14
mfcuser20-Aug-03 7:14 
GeneralRe: m_pGridCtrl undeclared identifier Pin
Chris Maunder20-Aug-03 11:35
cofounderChris Maunder20-Aug-03 11: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.