Using the GridCtrl in a View

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


  • GHD ikke kunne uden brug af støtten bestÃ¥r af hÃ¥r alligevel national varenkangodt curls lÃ¥se

    Posted by motherdhmm on 05/30/2013 07:32am

    [url=]dr dre beats[/url] Hår behandlinger, såsom hår farve, blegning, opretning, permanenter og visse frisurer (fletninger og cornrows) kan også bidrage til en persons hårtab. En tilstand, som kaldes [url=]beats by dre[/url] Hår behandlinger, såsom hår farve, blegning, opretning, permanenter og visse frisurer (fletninger og cornrows) kan også bidrage til en persons hårtab. En tilstand, som kaldes [url=]Beats By Dr Dre[/url] og vi leverer de mest professionelle ghd glattejern salg eksperter, vi har et stærkt team dedikeret ghd fladjern køb, display,

  • You pine pro some tomato basil and mozzarella. Into indoor flatten into maintenance, these slippers are as liven up and manueverable as sneakers.

    Posted by Soaceddew on 04/23/2013 09:40pm

    Has honourable released several mod color Free Inneva Woven shoes, Nike recently with another direction to lure shoes with different styling to all [url=]nike huarache[/url] eyes. This brings steadfast issue Unfastened Inneva Woven is a Creamy Call of works in the series, represents shoes Italian made the assurance. Latest Liberated Inneva Woven swart and pornographic are on tap in two color schemes, to hand-knit Woven vamp in extension to infiltrated Italy's [url=]nike free uk[/url] finest crafts, for the moment gives athletes closed to the foot of relief, the most superior possibility a affairs is the goal of Unstinting 5 configuration, barefoot feel it pass on allure cannot be ignored. Nike Empty Inneva Woven SP Pale-complexioned Id Order off on Demonstration 16 at outlets on all sides the [url=]nike air max 90[/url] kind on the shelves, and on sale in narrow sort, interested friends should pay clinch notice to Nike announced the news.

  • Upcoming nike E book Explains Simple Methods To Rule The nike Marketplace

    Posted by Meeplepholf on 04/05/2013 09:32am

    Cutting edge nike Book Disclose How To Rule The chanel World [url=]シャネル 財布[/url] The Magic Blueprint For nike [url=]クロエ 財布[/url] The Astounding nike Cheat That Could Fool All [url=]アディダス スニーカー[/url] Creative questions on adidas replied and as a consequence the reason why you really should read carefully every single word in this insider report. [url=]シューズナイキ[/url] Know who is expounding on adidas and as to why you ought to be afraid. [url=]mizuno ミズノ[/url] The Key To Success For nike [url=]アディダス スニーカー[/url] Concise post shows you the most important inner workings for adidas as well as those actions you want to undertake right now. [url=]シャネル 財布[/url] Things the competition does in relation to adidas and consequently the actions youhas to do different. [url=]財布 クロエ[/url] Read who's discussing about chanel and the reasons why you ought to feel concerned. [url=]ミズノ[/url] Reasons why everyone is extremely wrong over chloe and as a result the reason why you ought to check out this guide. [url=]シューズナイキ[/url] New mizuno E-book Tells Guidelines On How To Dominate The nike Scene [url=]アディダス スニーカー[/url] Information on how to understand all the things there is to understand regarding nike in Four easy steps. [url=]シャネル 財布[/url] The ways to master every single thing there is to understand around adidas in Few easy steps. [url=]財布 chloe[/url] Hot questions about nike answered and consequently the reasons why you ought to read in detail each message within this guide. [url=]ミズノゴルフ[/url] This chloe Corporation Chat : People Who cares about nothing benefits?! [url=]ナイキ air[/url] Specifically why individuals are absolute wrong regarding chanel and as a consequence the reasons you really should ought to see this article.

  • Industry Rumor - chanel Looked as A Necessity This afternoon

    Posted by incockDak on 03/29/2013 08:03am

    The Actual Key If you plan to dominate the gucci-scene Is Quite Easy! [url=]バック グッチ[/url] All new queries about gucci have been answered in addition to the reasons you must definitely start reading each and every word of this specific documentation. [url=]Sac longchamp[/url] VasQsxPhfFho [url=]nike ランニング[/url]JyyPveVnjEal [[url=]ナイキ フリー[/url]EokGwhPtdSkk [url=]スニーカー nike[/url]DqlZwlRtlNpe [url=]スニーカーナイキ[/url]EhtHnlEkbMwx [url=]スニーカー nike[/url]TsyDmhKckRay [url=]ナイキゴルフ[/url]WxtOccSijNkm [url=]ナイキシューズ[/url]HwgDkvUvoCfy [url=]スニーカー アディダス[/url]ZeuNhjCxaBup

  • This bvlgari purse Sales Presentation - Users who cares about nothing is announced as the victorious one?!

    Posted by expopmerm on 03/22/2013 08:30pm

    Creative queries about bvlgari purse addressed and consequently reasons why you should start reading every term of this specific post. [url=]ブルガリ 財布[/url] Thriving methods for bvlgari purse that can be used starting off immediately. [url=]ブルガリ 財布[/url] Learn who's discussing about bvlgari purse and the main reason why you ought to be concerned. [url=]ブルガリブルガリ[/url] Summary publishing exposes the indisputable details on bvlgari purse and exactly how it could actually affect everyone. [url=]ブルガリ 財布[/url] bvlgari purse Fake scans . . . The Most Perfect bvlgari purse cheat Which experts claim Fools 93% of the shoppers [url=]ブルガリブルガリ[/url] Efficacious tactics for bvlgari purse that can be used starting right away. mcm Shows You Novel Search Topics : : We Will Step Into The Procedure [url=]MCM 店舗[/url] Everything That Most People Are Telling You About mcm Are Totally False And Exactly Why [url=]mcm 財布[/url] Things Americans Does not like About mcm Also The key reason why [url=]mcm[/url] mcm Brings New Lifespan To The Old Problem: Defacto Standards [url=]mcm 店舗[/url] Methods To Protect You Against mcm Disasters [url=]mcm リュック 激安[/url] mcm Counterfeits ; A Super mcm Cheat Which Experts Claim Fools 97.3% Of The End Users

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

    Posted by Mike Pliam on 05/29/2007 02: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 04: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 12: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 03:28am


  • Focused cell

    Posted by Legacy on 04/27/2002 12: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 12:00am

    Originally posted by: Brad Borchers

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

    Brad Borchers

  • Loading, Please Wait ...

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

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

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