Click here to Skip to main content
15,868,349 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 351.2K   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

 
GeneralUNICODE Support Pin
Matt Newman15-Apr-02 11:52
Matt Newman15-Apr-02 11:52 
GeneralRe: UNICODE Support Pin
Chris Maunder12-Jul-02 23:18
cofounderChris Maunder12-Jul-02 23:18 
Questionhow to resize grid for long cell data? Pin
ricolee9926-Mar-02 10:17
ricolee9926-Mar-02 10:17 
AnswerRe: how to resize grid for long cell data? Pin
leizhou28-Nov-03 16:03
leizhou28-Nov-03 16:03 
GeneralGridCtrlDemo Pin
4-Nov-01 22:05
suss4-Nov-01 22:05 
QuestionHow to capture the changes in cell data?!? Pin
15-Oct-01 3:53
suss15-Oct-01 3:53 
QuestionHow to merge two or more cells? Pin
7-Oct-01 17:09
suss7-Oct-01 17:09 
Generalwrapping text in a cell Pin
1-Oct-01 16:50
suss1-Oct-01 16:50 
GeneralNot able to resize columns and rows by using mouse Pin
LEENA CHAUDHARI4-Jul-01 17:34
LEENA CHAUDHARI4-Jul-01 17:34 
Questionview sample with bitmaps and release build ? Pin
13-Jun-01 7:06
suss13-Jun-01 7:06 
Generaltroubled behaviour after messagebox Pin
14-Mar-01 5:32
suss14-Mar-01 5:32 
GeneralRe: troubled behaviour after messagebox Pin
Marco Giuntoni15-Mar-01 1:22
professionalMarco Giuntoni15-Mar-01 1:22 
GeneralPopup dialog on right click Pin
Scott!15-Feb-01 12:27
Scott!15-Feb-01 12:27 
Questionsaving Grid Datas using the save function in CGridCtrl?? Pin
Carsten Kemper31-Jan-01 5:09
Carsten Kemper31-Jan-01 5:09 
QuestionHow to save the Grid datas??? Pin
25-Jan-01 22:53
suss25-Jan-01 22:53 
AnswerRe: How to save the Grid datas??? Pin
Holger Persch26-Jan-01 1:35
Holger Persch26-Jan-01 1:35 
GeneralThe view on startup is blank Pin
19-Jan-01 4:35
suss19-Jan-01 4:35 
GeneralGrid Control Not drawing correctly Pin
dpitman7-Nov-00 13:58
dpitman7-Nov-00 13:58 
QuestionCan't work in Win2000? Pin
Tase houny17-Oct-00 3:43
sussTase houny17-Oct-00 3:43 
AnswerRe: Can't work in Win2000? Pin
Jason Teagle16-Jan-02 7:18
Jason Teagle16-Jan-02 7:18 
Generalwin98 problems Pin
psuedopodia15-Sep-00 5:22
psuedopodia15-Sep-00 5:22 
GeneralRe: win98 problems Pin
malinrob12-Nov-02 8:37
malinrob12-Nov-02 8:37 
QuestionWhy?I get a rim? Pin
Tase houny11-Sep-00 17:40
sussTase houny11-Sep-00 17:40 
AnswerRe: Why?I get a rim? Pin
Thomas Freudenberg4-Oct-00 9:47
Thomas Freudenberg4-Oct-00 9:47 
GeneralVersion 2.21 Pin
Chris Maunder30-Aug-00 15:04
cofounderChris Maunder30-Aug-00 15:04 

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.