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:
    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_pGridCtrl = new CGridCtrl;        // Create the Gridctrl object
            if (!m_pGridCtrl) return;
    
            CRect rect;                         // Create the Gridctrl window
            GetClientRect(rect);
            m_pGridCtrl->Create(rect, this, 100);
    
            m_pGridCtrl->SetRowCount(50);        // fill it up with stuff
            m_pGridCtrl->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
    {
        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



Comments

  • 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=http://www.buy-beatsdrdre.com/category/2012-dr-dre-beats-outlet]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=http://www.buy-beatsdrdre.com/]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=http://www.buy-beatsdrdre.com/category/new-beats-dr-dre-headphones]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,

    Reply
  • 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=http://fossilsdirect.co.uk/glossarey.cfm]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=http://markwarren.org.uk/goodbuy.cfm]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=http://markwarren.org.uk/property-waet.cfm]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.

    Reply
  • 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=http://www.21xqt.com/chanel.html]シャネル 財布[/url] The Magic Blueprint For nike [url=http://www.21xqt.com/chloe.html]クロエ 財布[/url] The Astounding nike Cheat That Could Fool All [url=http://www.21xqt.com/adidas.html]アディダス スニーカー[/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=http://www.21xqt.com/nike.html]シューズナイキ[/url] Know who is expounding on adidas and as to why you ought to be afraid. [url=http://www.21xqt.com/mizuno.html]mizuno ミズノ[/url] The Key To Success For nike [url=http://www.chyfc.com/include/adidasjp.html]アディダス スニーカー[/url] Concise post shows you the most important inner workings for adidas as well as those actions you want to undertake right now. [url=http://www.chyfc.com/style/chaneljp.html]シャネル 財布[/url] Things the competition does in relation to adidas and consequently the actions youhas to do different. [url=http://www.chyfc.com/cheng/chloejp.html]財布 クロエ[/url] Read who's discussing about chanel and the reasons why you ought to feel concerned. [url=http://www.chyfc.com/cert/mizunojp.html]ミズノ[/url] Reasons why everyone is extremely wrong over chloe and as a result the reason why you ought to check out this guide. [url=http://www.chyfc.com/templates/nikejp.html]シューズナイキ[/url] New mizuno E-book Tells Guidelines On How To Dominate The nike Scene [url=http://www.padgam.com/blog/adidasja.html]アディダス スニーカー[/url] Information on how to understand all the things there is to understand regarding nike in Four easy steps. [url=http://www.padgam.com/blog/chanelja.html]シャネル 財布[/url] The ways to master every single thing there is to understand around adidas in Few easy steps. [url=http://www.padgam.com/style/chloeja.html]財布 chloe[/url] Hot questions about nike answered and consequently the reasons why you ought to read in detail each message within this guide. [url=http://www.padgam.com/style/mizunoja.html]ミズノゴルフ[/url] This chloe Corporation Chat : People Who cares about nothing benefits?! [url=http://www.padgam.com/nikeja.html]ナイキ air[/url] Specifically why individuals are absolute wrong regarding chanel and as a consequence the reasons you really should ought to see this article.

    Reply
  • 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=http://growth-management.alachua.fl.us/comprehensive_planning/gucci.html]バック グッチ[/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=http://growth-management.alachua.fl.us/comprehensive_planning/saclongchamp.php]Sac longchamp[/url] VasQsxPhfFho [url=http://running-nike0.seesaa.net/]nike ランニング[/url]JyyPveVnjEal [[url=http://free-nike-nikeo.seesaa.net/]ナイキ フリー[/url]EokGwhPtdSkk [url=http://xn--nike-ul4c5c5fyqb.seesaa.net/]スニーカー nike[/url]DqlZwlRtlNpe [url=http://nikejapan0.seesaa.net/]スニーカーナイキ[/url]EhtHnlEkbMwx [url=http://nikesneakersjp.seesaa.net/]スニーカー nike[/url]TsyDmhKckRay [url=http://nikegolf00.seesaa.net/]ナイキゴルフ[/url]WxtOccSijNkm [url=http://nikeshoes00.seesaa.net/]ナイキシューズ[/url]HwgDkvUvoCfy [url=http://sneaker-adidas-jp.seesaa.net/]スニーカー アディダス[/url]ZeuNhjCxaBup

    Reply
  • 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=http://www.bvlgarishopjprie.com]ブルガリ 財布[/url] Thriving methods for bvlgari purse that can be used starting off immediately. [url=http://www.bvlgarioutletshopjp.com]ブルガリ 財布[/url] Learn who's discussing about bvlgari purse and the main reason why you ought to be concerned. [url=http://www.bvlgarisalekodojp.com]ブルガリブルガリ[/url] Summary publishing exposes the indisputable details on bvlgari purse and exactly how it could actually affect everyone. [url=http://www.bvlgarisakaijpsale.com]ブルガリ 財布[/url] bvlgari purse Fake scans . . . The Most Perfect bvlgari purse cheat Which experts claim Fools 93% of the shoppers [url=http://www.bvlgariaokisalejp.com]ブルガリブルガリ[/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=http://www.mcmbagsjphadori.com]MCM 店舗[/url] Everything That Most People Are Telling You About mcm Are Totally False And Exactly Why [url=http://www.mcmoutletjpinoue.com]mcm 財布[/url] Things Americans Does not like About mcm Also The key reason why [url=http://www.mcmsalejapanoka.com]mcm[/url] mcm Brings New Lifespan To The Old Problem: Defacto Standards [url=http://www.mcmzankijpshop.com]mcm 店舗[/url] Methods To Protect You Against mcm Disasters [url=http://www.mcmstorejpkodo.com]mcm リュック 激安[/url] mcm Counterfeits ; A Super mcm Cheat Which Experts Claim Fools 97.3% Of The End Users

    Reply
  • 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

    Reply
  • 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.

    Reply
  • Grid Control

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

    Originally posted by: Kothavari Rajendran

    Hi,
    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..
    Thanks

    • a

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

      a

      Reply
    Reply
  • 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.

    Reply
  • 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? 
    
    

    Thanks,
    Brad Borchers

    Reply
  • Loading, Please Wait ...

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

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • In support of their business continuity and disaster recovery plans, many midsized companies endeavor to avoid putting all their eggs in one basket. Understanding the critical role of last-mile connectivity and always available Internet access for their enterprises, savvy firms utilize redundant connections from multiple service providers. Despite the good intentions, their Internet connectivity risk may still be in a single basket. That is because internet service providers (ISPs) and competitive local …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds