Using the GridCtrl in a View


Desktop-as-a-Service Designed for Any Cloud ? Nutanix Frame

Download demo project.

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_pGridCtrl = new CGridCtrl;        // Create the Gridctrl object
            if (!m_pGridCtrl) return;
            CRect rect;                         // Create the Gridctrl window
            m_pGridCtrl->Create(rect, this, 100);
            m_pGridCtrl->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:
        if (m_pGrid)
            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);

Last updated: 29 November 1998


  • How can I determine if a cell has been edited?

    Posted by Mike Pliam on 05/29/2007 09:32pm

    How can the View determine if a cell has been edited? I have tried all the tricks I know without being able accomplish this. Any help greatly appreciated. Thanks

  • Simple fix for XP Pro and C++.NET 7.0

    Posted by Mike Pliam on 05/25/2007 11:30pm

    This fix is if you try to put the grid control in a CScrollView instead of CView.  (I now realize that there is no need to do this because the grid control has it's own scrolling built in.)  Anyway, if you insist on putting the control code into a CScrollView, do the following and it should work:  (It simply uncovers some bad coding.)  Rewrite the following:
    BOOL CTitleTip::PreTanslateMessage(MSG* pMsg)
         Cwnd *pWnd
         int hittest;
         POINTS pts;
         POINT point;
         switch (pMsg->message)
          case WM_MBUTTONDOWN:
              pts = MAKEPOINTS( pMsg->lParam );
    Then it compiles just fine.

  • Grid Control

    Posted by Legacy on 07/22/2002 07:00am

    Originally posted by: Kothavari Rajendran

    I need to place the grid control in the dialog . All i read about placing in the views..Can some one help me out please..

    • a

      Posted by ujongun on 10/16/2005 10:28am


  • Focused cell

    Posted by Legacy on 04/27/2002 07:00am

    Originally posted by: Barmaley

    Please, tell me how can I easily make focused cell appearance not being distinguished from other cells?
    Like if i use grid in list mode, focused cell doesn't look nice in the selected row.

  • doesnt work in XP

    Posted by Legacy on 03/29/2002 08:00am

    Originally posted by: Brad Borchers

    The control works in win 95,98,ME,2000, but not XP. Why would this be? 

    Brad Borchers

  • about splite window

    Posted by Legacy on 12/20/2001 08:00am

    Originally posted by: Jack

    I use your GridCtrl in my splite view, when I edit a grid cell, then click the grid cell, it was pop-up a window told me to abort, retry or ignore, how to resolve the problem.
    Thanks a lot!

    Here are some code in my program.
    //m_wndSplitter is derive from CView
    //CRightTopView is derive from CView, too
    //class CRightTopView is same as the class CGridViewDemo in your sample(gridctrl in view)

    m_wndSplitter.CreateStatic(this, 2, 1);

    CCreateContext *pContext = (CCreateContext*) lpCreateStruct->lpCreateParams;

    m_wndSplitter.CreateView(0,0,RUNTIME_CLASS(CRightTopView), CSize(150,0), pContext);
    m_wndSplitter.CreateView(1,0,RUNTIME_CLASS(CClientView), CSize(0,0), pContext);

  • How do I change the cell colour?

    Posted by Legacy on 11/26/2001 08:00am

    Originally posted by: Brian Reffold

    I am trying to change a group of cells colours from the Default to Blue, heres the code I am using:

    COLORREF background;
    background = RGB(0,0,255);

    GV_ITEM Item;
    Item.mask = GVIF_TEXT|GVIF_FORMAT;
    Item.row = 0;
    Item.col = 0;
    Item.nFormat = DT_LEFT|DT_WORDBREAK;
    Item.crBkClr = background;

    what I am I doing wrong?


  • Many memory leaks

    Posted by Legacy on 11/14/2001 08:00am

    Originally posted by: vadim-g

    You Grid control is great but it have many memory leaks.
    What can I do?

  • How to add combobox to a particular column in MSflexgrid control

    Posted by Legacy on 04/03/2001 07:00am

    Originally posted by: Mahesh Kumar

    How to add combobox to a particular column in MSflexgrid control ?

  • How can i merge 2 cells !

    Posted by Legacy on 12/18/2000 08:00am

    Originally posted by: MALICHARD

    How can i merge 2 cells !

  • Loading, Please Wait ...

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date