Click here to Skip to main content
Click here to Skip to main content

Using the Grid Control in a Doc/View framework

By , 29 Aug 2000
 

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)

About the Author

Chris Maunder
Founder CodeProject
Canada Canada
Member
Chris is the Co-founder, Administrator, Architect, Chief Editor and Shameless Hack who wrote and runs The Code Project. He's been programming since 1988 while pretending to be, in various guises, an astrophysicist, mathematician, physicist, hydrologist, geomorphologist, defence intelligence researcher and then, when all that got a bit rough on the nerves, a web developer. He is a Microsoft Visual C++ MVP both globally and for Canada locally.
 
His programming experience includes C/C++, C#, SQL, MFC, ASP, ASP.NET, and far, far too much FORTRAN. He has worked on PocketPCs, AIX mainframes, Sun workstations, and a CRAY YMP C90 behemoth but finds notebooks take up less desk space.
 
He dodges, he weaves, and he never gets enough sleep. He is kind to small animals.
 
Chris was born and bred in Australia but splits his time between Toronto and Melbourne, depending on the weather. For relaxation he is into road cycling, snowboarding, rock climbing, and storm chasing.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionThe file has 0 bytesmemberazolotnitsky1 Apr '12 - 3:19 
Attached zip is empty - it has 0 bytes. Can you reupload the demo project?
AnswerRe: The file has 0 bytesadminChris Maunder2 Apr '12 - 8:17 
I've updated the download to the latest zip. Sorry about that.
cheers,
Chris Maunder
 
The Code Project | Co-founder
Microsoft C++ MVP

QuestionHow to use in CFormView derived class ?memberManmohan2925 Aug '11 - 0:09 
I have two views ( CMainView and CMembersView ) in my SDI application. The code below resizes my MFC GridCtrl according to size of app's window when the view is initialized.
I get valid values for CRect grid_rect {top=0 bottom=952 left=0 right=1819} in CMainView. But in CMemberView I am having some undesired values.
 
This is working fine :-
 
void CMainView::OnInitialUpdate() // CMainView is my application's default view
{
CFormView::OnInitialUpdate();

initialized = TRUE;
CWnd *pGridCtrl = GetDlgItem(IDC_SALEPURCHASEGRIDCTRL);
CRect rect,grid_rect;

CWnd *pWnd = this->GetOwner();
pWnd->GetClientRect(rect);


pGridCtrl->GetWindowRect(grid_rect); // works fine here
grid_rect = rect;
grid_rect.left += 16;
grid_rect.top += 150;
grid_rect.bottom -= 300;
grid_rect.right -= 16;

pGridCtrl->MoveWindow(grid_rect); // GridCtrl is resized properly
.
.
.
 

This is not working properly:-
 
void CMembersView::OnInitialUpdate()
{

CFormView::OnInitialUpdate();

initialized = TRUE;
CWnd *pGridCtrl = this->GetDlgItem(IDC_MEMBERSGRID);
ASSERT(pGridCtrl); // Must exist
 
CRect rect,grid_rect;

CWnd *pWnd = this->GetOwner();
pWnd->GetClientRect(rect); // Also tried with &rect

pGridCtrl->GetWindowRect(grid_rect); // problem is here. no grid_rect coordinates
// grid_rect {top=-32639 bottom=-32483 left=-32709 right=-31884} CRect

grid_rect = rect;
grid_rect.left += 16;
grid_rect.top += 150;
grid_rect.bottom -= 300;
grid_rect.right -= 16;

pGridCtrl->MoveWindow(grid_rect); // Also tried with &grid_rect
.
.
.
 
Confused | :confused:
---------------------------------------------
" Future Lies in Present "Manmohan Bishnoi
 

GeneralCompile error at m_pGrid = new CGridCtrl;membermaetiop19 May '09 - 1:32 
I follow the instruction, but get compile error. Who could tell me how to resolve this problem?
CTestGridView::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);
 
        ...
        ...        
        // ... etc
    }
}
 
Error 2 error C2512: 'CGridCtrl' : no appropriate default constructor available
 
Thanks for help!
GeneralRe: Compile error at m_pGrid = new CGridCtrl;membermaetiop19 May '09 - 1:45 
I change the declaration of CGridCtrl in the header file, then it bulids OK.
 
change from
 
class CGridCtrl;
 
to
 
#include "Gridctrl.h"
GeneralGridViewDemo VS 2008 a lot of errors when run this demomemberguru8826 Oct '08 - 10:35 
------ Build started: Project: GridViewDemo, Configuration: Debug Win32 ------
Compiling...
StdAfx.cpp
WINVER not defined. Defaulting to 0x0600 (Windows Vista)
Compiling...
GridCell.cpp
GridCellBase.cpp
GridCtrl.cpp
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(425) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(425) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(425) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(426) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(1952) : error C2065: 'pos' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(1952) : error C2065: 'pos' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(1956) : error C2065: 'pos' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(2214) : warning C4244: 'initializing' : conversion from 'ULONGLONG' to 'DWORD', possible loss of data
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(2618) : error C2065: 'col' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(2630) : error C2065: 'col' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(2633) : error C2065: 'col' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(2649) : error C2065: 'row' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(2661) : error C2065: 'row' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(2664) : error C2065: 'row' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(2716) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(2716) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(2720) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(2720) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(2720) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(2722) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(2729) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(2729) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(2756) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(2756) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(2762) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(2762) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(2762) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(2764) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(2768) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(2768) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(3192) : error C2065: 'j' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(3192) : error C2065: 'j' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(3192) : error C2065: 'j' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(3194) : error C2065: 'j' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(3194) : error C2065: 'j' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(3195) : error C2065: 'j' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(3196) : error C2065: 'j' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(3246) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(3246) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(3246) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(3249) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(3249) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(3250) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(3251) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(3412) : error C2065: 'col' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(3412) : error C2065: 'col' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(3412) : error C2065: 'col' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(3416) : error C2065: 'col' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(3416) : error C2065: 'col' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(3490) : error C2668: 'CUIntArray::InsertAt' : ambiguous call to overloaded function
c:\program files\microsoft visual studio 9.0\vc\atlmfc\include\afxcoll.h(336): could be 'void CUIntArray::InsertAt(INT_PTR,CUIntArray *)'
c:\program files\microsoft visual studio 9.0\vc\atlmfc\include\afxcoll.h(333): or 'void CUIntArray::InsertAt(INT_PTR,UINT,INT_PTR)'
while trying to match the argument list '(int, int)'
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(3565) : error C2668: 'CUIntArray::InsertAt' : ambiguous call to overloaded function
c:\program files\microsoft visual studio 9.0\vc\atlmfc\include\afxcoll.h(336): could be 'void CUIntArray::InsertAt(INT_PTR,CUIntArray *)'
c:\program files\microsoft visual studio 9.0\vc\atlmfc\include\afxcoll.h(333): or 'void CUIntArray::InsertAt(INT_PTR,UINT,INT_PTR)'
while trying to match the argument list '(int, int)'
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(4739) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(4739) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(4739) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(4740) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(4745) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(4745) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(4745) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(4746) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(4806) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(4806) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(4806) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(4807) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(4812) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(4812) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(4812) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(4813) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(6559) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(6559) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(6559) : error C2065: 'i' : undeclared identifier
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\gridctrl.cpp(6563) : error C2065: 'i' : undeclared identifier
GridDropTarget.cpp
InPlaceEdit.cpp
TitleTip.cpp
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\titletip.cpp(314) : error C2360: initialization of 'pts' is skipped by 'case' label
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\titletip.cpp(275) : see declaration of 'pts'
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\titletip.cpp(315) : error C2360: initialization of 'pts' is skipped by 'case' label
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_src\titletip.cpp(275) : see declaration of 'pts'
ChildFrm.cpp
GridViewDemo.cpp
c:\documents and settings\guru\pulpit\gridctrl_demo221\gridctrl_in_view\gridviewdemo.cpp(61) : warning C4996: 'CWinApp::Enable3dControls': CWinApp::Enable3dControls is no longer needed. You should remove this call.
c:\program files\microsoft visual studio 9.0\vc\atlmfc\include\afxwin.h(4818) : see declaration of 'CWinApp::Enable3dControls'
GridViewDemoDoc.cpp
GridViewDemoView.cpp
MainFrm.cpp
Generating Code...
Build log was saved at "file://C:\Documents and Settings\Guru\Pulpit\gridctrl_demo221\gridctrl_in_view\Debug\BuildLog.htm"
GridViewDemo - 72 error(s), 2 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
 
How to fix it? Please help...
GeneralRe: GridViewDemo VS 2008 a lot of errors when run this demomemberBrent Rauscher6 Apr '09 - 12:02 
I concur - the same error list at compile time from Visual Studio C++. This code is NOT designed for this compiler, but I cannot tell where it is supposed to work. If these incidental bugs are removed, the run-time is EVEN WORSE. This is a very pretty utility that I REALLY WANT, but it is wasting days of my time and yields nothing.
 
NOTICE: Please DO NOT USE this code!!!!!!!!!!!!!!! It does not work - stay very FAR AWAY....
GeneralRe: GridViewDemo VS 2008 a lot of errors when run this demoadminChris Maunder6 Apr '09 - 12:03 
The code works fine on the compiler it was built and tested on. I can only apologise that I've not had a chance to update it for newer compilers.
 
Please do a search on this site for 'Ultimate Grid' for a more up to date grid.
 
cheers,
Chris Maunder
 
The Code Project Co-founder
Microsoft C++ MVP

GeneralRe: GridViewDemo VS 2008 a lot of errors when run this demomemberMohd Amanullah Zafar6 May '11 - 3:11 
This is because of a VC++ properties for Loop.
go to Property Pages --> C/C++ --> Language --> Set No to "Force Conformance in For Loop Scope".
SHould Fix this Error.
GeneralThe download zip is corruptmemberandrewtruckle18 May '08 - 2:50 
Zip is corrupt
GeneralRe: The download zip is corruptadminChris Maunder18 May '08 - 9:34 
Works perfectly for me. Can you retry?
 
cheers,
Chris Maunder
CodeProject.com : C++ MVP

GeneralRe: The download zip is corruptmemberandrewtruckle18 May '08 - 9:37 
It is refusing to work for me. Sorry Frown | :(
 
Could you email it?
 
I am trying to do a dummy MDI app in VS2005 because I have bigtime headaches with my proper app and the grid class not working after porting. I mention this elewhere in the other grid article.
 
Thanks.
Questionis there anyway to use without dialog-based and document/view?memberroroo21 Jun '07 - 7:30 
hi~ i'm a begginer of vc++.
i just wany to create a small program.
so, i didn't use dialog and doc/view strucrture.
 
it seems not work on typical win32 application?
 
Thanks~
QuestionTransparent Grid ControlmemberParinda22 May '07 - 23:42 
How to make the grid control transparent ?
 
Also if I have multiple grid controls on my View, how do I select more than one at a time ?
GeneralAfter adding a Custom Control, the Dialog does not display anymorememberAndyKiser19 Feb '07 - 23:48 
First of all, thanks for the great work!
 
While I tried to include the control in my own Dialog (as a custom control), my dialog did not appear anymore. It seems, I was not the first user facing this problem.
I found the solution in a user forum:
http://www.codeguru.com/forum/showthread.php?t=344824
I thought it may be worth posting this here, to save other guys' time...
 
Problem
-----------------------
1) created a solution with an MFC Application project (dialog based)
2) added a custom control to the dialog
3) Set the new control's class to MFCGridCtrl
4) Added all the .cpp and .h files for MFCGridCtrl files to the project
5) Placed #include "GridCtrl.h" in testDlg.cpp
6) Compiled
 
When run DoModal returns -1.
Investigating using debug the problem seems to be in:
dlgcore.cpp function CWnd::CreateDlgIndirect(..), just after AfxHookWindowCreate(this); (about line 315) there is:
 
hWnd = ::CreateDialogIndirect(hInst, lpDialogTemplate, pParentWnd->GetSafeHwnd(), AfxDlgProc);
 
hWnd remains null after this call and the GetLastError that follows returns 0.
A little later hWnd is tested for null and if true some debug statements are shown then FALSE is returned.

 
Solution
------------------------------------
 
Re: about Custom Control
Thank you - exactly the answer that I had been searching for. the steps I took were:
 
1) moved '#include "GridCtrl.h"' from testDlg.cpp to testDlg.h
2) declared variable 'CGridCtrl m_gridCtrl' in testDlg.h
3) added 'DDX_GridControl(pDX, IDC_CUSTOM1, m_gridCtrl);' in DoDataExchange
4) Recompiled

...
 
Andy
GeneralRe: After adding a Custom Control, the Dialog does not display anymorememberronasando95 Apr '07 - 20:47 
It really work.
Thank u so much!
God bless kind-hearted guy!
GeneralRe: After adding a Custom Control, the Dialog does not display anymorememberMember 833826021 Oct '11 - 1:18 
Hi,
I did exactly the same.
 
1. Added Custom Control in Resource View via Toolbox
2. Set the control's class to MFCGridCtrl via PropertySheet
3. Added all the files to the project
4. Placed #include "GridCtrl.h" in *.h
5. declared variable CGridCtrl m_gridCtrl in *.h
6. added "DDX_GridControl(pDX, IDC_GRIDCTRL, m_gridCtrl);" in DoDataExchange
 
result of DoModal() is still -1. Without the custom control the dialog pops up.
 
Anybody has a clue?
GeneralCMyView::OnContextMenu(.....) won't be called.memberLeeeNN3 Jun '06 - 9:23 
Hi,
I add afx_msg void CMyView::OnContextMenu(.....), But it won't be called when I right click button on mouse. But the GridDemoDlg example works.
I noticed in CGridCtrl::OnRClick(), it call CWnd::OnRClick() which invokes OnContextMenu().
in the dialog demo example, CGridDemoDlg::OnContextMenu() is called, why won't work in the Doc/View example?
 
thanks
QuestionCan't set the Itemmemberrosalite28 Apr '06 - 19:00 
I have use'd Grid ctrl in a View .It worked.But I finded I can't set the Item unless the View is being actived.
 
I creat a function like this:
 
void CMyAppView::Filesave(CString path)
{
m_pGrid->SetItemText(0,0,"str");
AfxMessageBox("save");
}
 
When I called this function in CMyAppDoc.
The Msgbox is done.
But SetItemText doesn't work.
 
How can I do this problem?
 

Questionstarting the horizontal scrollbar control at a specified position to the leftmembervkrishnamurthy23 Apr '06 - 20:25 
Hi,
 
I am new to MFCs. So, I will appreciate any help in this regard.
 
I am using Chris Maunder's grid control. However, the horizontal scroll bar control (not the scroll box postion but the control postion) starts from the extreme left of the grid control application. How can I start the control from a specified (x,y) position. I need to have the control start from below column 1. Because, I will be placing another control beneath column0.
 
Please note, I am talking about the actual scroll bar control position and not the scroll box position itself.
 
Thank you!
vishwa
QuestionHow to load file?membervanthe17 Feb '06 - 5:41 
I want to load file text to grid using menu item as ID_FILE_OPEN. How can I do??
QuestionHow can use events when Text changes using it in a comboBox in view?memberNam Sung-ho5 Jan '06 - 2:55 
How can I use events like OnGridEndEdit or OnGridStartEdit or OnGridStartSelChange or OnGridEndSelChange when Text changes using it in a comboBox in view?
 
How can I use ComboBox events have a look of CBN_EDITCHAGE events?
 
Thanks in advance for your help
 
d
 
-- modified at 3:23 Monday 9th January, 2006
GeneralHave a critical error when m_pGrid->Create(...memberGrey0077911 Dec '05 - 23:33 
m_pGrid->Create(rect,this,100) except a critical error.
i think in mfc library CWnd::CreateEx(...
Why?Cry | :((
GeneralGetting Message to the CViewmemberRon Gilbert14 Jun '05 - 9:48 
I asked this once before, but never got a response, so can someone please help me.
 
I have a CView that creates the CGridCtrl object, but my CView is not getting any messages like OnChar(). These are grabbed by the Grid Control and never passed up the chain.
 
How can I make this happen? I need to get these events in the CView.
 

GeneralRe: Getting Message to the CViewmemberNoName2316 Jun '05 - 4:41 
Here is how I was able to get it to work. I don't know if it is the right way if anyone has any other suggestions I am more than open to them. You can override your OnNotify in your view and do the following...
 
if(wParam==m_pGrid->GetDlgCtrlID()) //message is coming from the grid
{
NM_GRIDVIEW* pNMHDR;
pNMHDR = (NM_GRIDVIEW*)lParam;
CString szData;
if(pNMHDR->hdr.code == NM_DBLCLK)
{
TRACE("Double clicked on Cell);
*pResult = 0;
return TRUE;
}
}
return CView::OnNotify(wParam, lParam, pResult);
 
You could switch on the pNMHDR->hdr.code to see and handle the notification accordingly. Look in the Gridctrl.cpp and search for SendMessageToParent and you will see the format of the NM_GRIDVIEW stuct and you will also see the messages that the grid passes to it's parent. You could also catch it in the OnCmdMsg you will have to include the AFXPRIV.H I would put the following code after the suggested code in the article if you are going to do it this way.
 
if(nID == m_pGrid->GetDlgCtrlID()) //message is coming from the grid
{
if(pExtra)
{
AFX_NOTIFY *notify;
notify = (AFX_NOTIFY*)pExtra;
NM_GRIDVIEW* pNMHDR;
pNMHDR = (NM_GRIDVIEW*)notify->pNMHDR;
if(pNMHDR->hdr.code == NM_DBLCLK)
{
TRACE("Double clicked on Cell);
notify->pResult = 0;
return TRUE;
}
}
}
Hope this helps

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 30 Aug 2000
Article Copyright 2000 by Chris Maunder
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid