Click here to Skip to main content
13,893,974 members
Click here to Skip to main content
Add your own
alternative version


42 bookmarked
Posted 24 Aug 2007
Licenced CPOL

The Ultimate Grid Beginner's Guide

, 24 Aug 2007
Rate this:
Please Sign up or sign in to vote.
Getting started with the Ultimate Grid

Visit the Ultimate Grid main page for an overview and configuration guide to the Ultimate Grid library.



This is a brief introduction to getting started with the Ultimate Grid. You should find it pretty straightforward to get going with the grid in your MFC-based application after reading these short tutorials on using the grid in view and dialog based applications. You can also download the CHM documentation, which contains a Getting Started section as well as a more detailed tutorial on displaying your first grid in an MFC application.

You can build the core Ultimate Grid source into a static library or DLL with the projects provided and link to it in that format, but we'll start here with examples of using the grid source code directly in your project.

Using the Grid in a CView

To begin, create a new SDI or MDI based MFC application project. Add the CPP files contained in the Ultimate Grid source directory, and the .h files contained in the include directory. You will probably need to set up an Additional Include path to the Ultimate Grid\Include directory in your projects preprocessor settings. As noted on the main page, the source code and sample projects zip files should integrate to form the following directory structure:

That done, your project should build cleanly - compiling the basic grid source code.

Next, we'll need to integrate a grid (CUGCtrl) derived class into the project. We've provided a MyCug 'skeleton' class in the Skel directory. This CUGCtrl derived class has all the grid override and setup function overrides you'll need to work with, allowing you to customize the behaviour of you grid with a minimum of additional coding.

Copy the MyCug.h and MyCug.cpp files to your project directory, and add the files to your project.

Next, you'll need to include the MyCug.h file in the app and view classes - like so:

// YourApp.cpp : Defines the class behaviours for the application.

#include <span class="code-string">"stdafx.h"</span>

#include <span class="code-string">"mycug.h" // add the header to the YourApp and YourView cpp files  </span>

#include <span class="code-string">"test.h"</span>

#include <span class="code-string">"MainFrm.h"</span>

Next, in the header for the view class, declare an instance of the MyCug grid class:

class CMyView : public CView{
     //******** Create a New Grid Object ********

     MyCug m_grid;

Next, in the OnCreate method of the view, we can create our grid:

int CMyView::OnCreate(LPCREATESTRUCT lpCreateStruct) {
     if (CView::OnCreate(lpCreateStruct) == -1)
          return -1;

     // TODO: Add your specialized creation code here

     m_grid.CreateGrid( WS_CHILD|WS_VISIBLE, CRect(0,0,0,0), this, 
         1234 );    // arbitrary ID of 1234 - season to taste

     return 0;

And the last bit will tie the grid to the client area of the view - add an OnSize handler, and the following code:

void CMyView::OnSize(UINT nType, int cx, int cy){

     CView::OnSize(nType, cx, cy);

     // TODO: Add your message handler code here

     m_grid.MoveWindow( 0, 0, cx, cy );       // size the grid control to 

                                              // fit the view


At this point, should you compile and run the application, the view should contain something almost, but not quite entirely, unlike a grid. So, let's set up some columns and rows and populate cells with data.

We'll place this code in the OnSetup notification of the derived grid class:

    This function is called just after the grid window 
    is created or attached to a dialog item.
    It can be used to initially setup the grid
void MyCug::OnSetup(){
     int rows = 20;
     int cols = 20;
     int i,j;
     CString  temp;
     CUGCell  cell;

     // setup rows and colums


     // fill cells with data

     for (i = 0; i < cols; i++) {
          for (j = 0; j < rows; j++) {
     // add column headngs

     for (i = 0; i < cols; i++) {
     // add row headings

     for (j = 0; j < rows; j++) {

Now, on running the program, you should see the grid populated with 20 rows and columns of numeric data:

This is a screenshot of the Ex2 project, found in the Examples\BasicMDI directory of the samples download.

Using the Grid in a CDialog

For a dialog based application, you'll want to attach the grid to a control placed on the dialog with the resource editor. Here we've just expanded the default 'TODO' static text item, and given it an ID of IDC_GRID1:

Leaving out the steps shown above (including the source and MyCug files etc), the difference here is in grid creation. In the OnInitDialog method of our dialog, we'll call the Attach method of the grid instead of the Create method used in the view example:

BOOL CEx3Dlg::OnInitDialog()
     // TODO: Add extra initialization here

     // attach the grid to the static control

     m_ctrl.AttachGrid(this, IDC_GRID1);

     return TRUE;  // return TRUE  unless you set the focus to a control


From here, initialization can be coded in the MyCug::OnSetup routine as in the above view example. You can refer to the Ex3 project in the Samples\BasicDlg directory of the samples download for details.

There are times when you might want to use a member pointer in order to destroy and recreate the grid on a dialog. The problem here is that once attached to the static control window, that window is destroyed along with the grid on calling delete. So, the control needs to be recreated and assigned a known ID so that a new grid can be reattached.

Here is one solution:

void CSampleGridDlgDlg::OnCreatebtn() 
     if(m_pGrid != NULL) {
          delete m_pGrid;
          m_pGrid = NULL;
     HWND hWnd = CreateWindowEx(0,"STATIC","Test",
         WS_CHILD | WS_VISIBLE,0,100,720,400,this->m_hWnd,NULL,NULL,0);
     if(hWnd != NULL) {
          SetWindowLong(hWnd, GWL_ID, 37773);    // arbitrary ID for the 

                                                 // static control

          int id = ::GetDlgCtrlID(hWnd);
          m_pGrid = new MyCug;
          m_pGrid->AttachGrid(this, id);

Using the Grid in a DLL or Static Library

Two projects are included that will build the static and dynamic (DLL) libraries and place them in the Lib and Dlls folders of the Ultimate Grid directory. As installed, these projects will reference the core Source and Include files and enable you to link without compiling these in your project, but will not incorporate the add on cell types and data sources, which will still need to be added to your project as needed. (See the comments section of the cell types article for instructions on incorporating the advanced cell types in a library build).

Static Lib Settings

The static library project in the BuildLib directory has 16 configurations (batch mode build is recommended) that accommodate the various combinations of MFC linkage, Ole/non-Ole, Unicode/MBCS, and debug/release configurations. Each build configuration is set to create the *.lib file and place it in the Lib directory.

DLL Settings

The DLL project in the BuildDLL directory contains only four configurations, as it assumes all builds link dynamically to the MFC library and are Ole enabled. On creation, the *.lib, *.exp, and *.dll files will be placed in the DLLs directory.

Project Changes - DLL Usage

You can now remove the Ultimate Grid\Source *.cpp files from your project. You'll still need a path to the Ultimate Grid\Include directory, as the headers will still be referenced through the CUGCtrl derived class MyCug.

We've provided a header file that should be added to your project that will select the correct module to link against based on your projects current build settings - uglibsel.h can be found in the Ultimate Grid\Lib directory. We recommend adding this file to your stdafx.h header. If you use this file directly from the Lib directory, you'll need an additional include path for this as well.

If you want to link to the DLL version of the grid, your preprocessor settings should contain the define _LINK_TO_UG_IN_EXTDLL. This will define the UG_CLASS_DECL declaration as AFX_CLASS_IMPORT for the Ultimate Grid classes.

If using uglibsel.h, you will also need to define a path to the DLLs directory as an additional library directory in the linker settings of your project. Alternatively, you can choose to use the following code in your stdafx.h file, replacing the relative paths with their correct equivalents for your project:

#ifdef _DEBUG
    #ifdef _UNICODE
        #pragma message("Automatically linking with UGMFCDU.lib - " +
            "please make sure this file is built.")
        #pragma comment(lib, "..\\..\\DLLs\\UGMFCDU.lib")
    #else // not unicode    

        #pragma message("Automatically linking with UGMFCD.lib - " +
            " please make sure this file is built.")
        #pragma comment(lib, "..\\..\\DLLs\\UGMFCD.lib")
    #endif // _UNICODE

#else // RELEASE

    #ifdef _UNICODE
        #pragma message("Automatically linking with UGMFCU.lib - " +
            "please make sure this file is built.")
        #pragma comment(lib, "..\\..\\DLLs\\UGMFCU.lib")
    #else // not unicode    

        #pragma message("Automatically linking with UGMFC.lib - " +
            "please make sure this file is built.")
        #pragma comment(lib, "..\\..\\DLLs\\UGMFC.lib")
    #endif // _UNICODE

#endif // _DEBUG

Of course, the actual DLL that will be used will need to be in the system path or available in the project directory at runtime.

Project Changes - Static Library Usage

Again, you can now remove the Ultimate Grid\Source files from your project, keeping the additional include path to the Ultimate Grid\Include directory in your preprocessor settings.

Add an additional library directory path to the linker settings (set this to the Ultimate Grid\Lib directory) and include the uglibsel.h header from that directory or copy the file to your project directory.

Your project should now build and link with the correct static library based on your project settings.


Initial CodeProject release August 2007.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

The Ultimate Toolbox
Web Developer
Canada Canada
In January 2005, David Cunningham and Chris Maunder created, a new group dedicated to the continued development, support and growth of Dundas Software’s award winning line of MFC, C++ and ActiveX control products.

Ultimate Grid for MFC, Ultimate Toolbox for MFC, and Ultimate TCP/IP have been stalwarts of C++/MFC development for a decade. Thousands of developers have used these products to speed their time to market, improve the quality of their finished products, and enhance the reliability and flexibility of their software.
Group type: Organisation

458 members

You may also be interested in...

Comments and Discussions

QuestionVC 8 error LNK2001: Pin
Member 802701721-Jan-16 20:33
memberMember 802701721-Jan-16 20:33 
QuestionTab order Pin
HungryCPPDev15-Apr-15 1:55
memberHungryCPPDev15-Apr-15 1:55 
QuestionI can not download Pin
niceJinSoo19-Oct-14 1:37
memberniceJinSoo19-Oct-14 1:37 
AnswerRe: I can not download Pin
hraw30-Oct-14 10:25
memberhraw30-Oct-14 10:25 
GeneralRe: I can not download Pin
BlastAss24-Apr-15 10:04
memberBlastAss24-Apr-15 10:04 
AnswerRe: I can not download Pin
Nick Tillich8-Feb-16 12:23
memberNick Tillich8-Feb-16 12:23 
QuestionUltimate Grid in C++ Pin
Tonex24715-Dec-13 5:07
memberTonex24715-Dec-13 5:07 
QuestionSetParam/GetParam does not allow saving a pointer in 64 bit? Pin
ehaerim25-Oct-13 18:56
memberehaerim25-Oct-13 18:56 
QuestionFrozen grid in split CView Pin
Michael B Pliam15-Sep-13 14:30
memberMichael B Pliam15-Sep-13 14:30 
QuestionMissing StartEdit() in MyCug::OnDClicked ? Pin
nickhalfasleep22-Nov-11 9:11
membernickhalfasleep22-Nov-11 9:11 
QuestionDebugging Pin
ITISAG21-Nov-11 10:41
memberITISAG21-Nov-11 10:41 
QuestionWhen I use SortBy(col, UG_SORT_DESCENDING); RedrawAll(); , it fails to work in dialog base project. Pin
Gennady4622-Aug-11 13:25
memberGennady4622-Aug-11 13:25 
Questionerror LNK2001: unresolved external symbol in ultimate grid dialog base project. Pin
Gennady4628-Jul-11 11:13
memberGennady4628-Jul-11 11:13 
GeneralGrid not shown! Pin
sinaocean31-Jul-10 18:56
membersinaocean31-Jul-10 18:56 
Questionhow to use two grid object? Pin
ryugisang23-Sep-09 23:34
memberryugisang23-Sep-09 23:34 
AnswerRe: how to use two grid object? Pin
Tim Deveaux25-Sep-09 5:54
memberTim Deveaux25-Sep-09 5:54 
Questionhow to set up an Additional Include path Pin
haibara28-Nov-08 4:59
memberhaibara28-Nov-08 4:59 
AnswerRe: how to set up an Additional Include path Pin
Jerry Evans28-Nov-08 5:06
memberJerry Evans28-Nov-08 5:06 
GeneralRe: how to set up an Additional Include path Pin
haibara28-Nov-08 15:09
memberhaibara28-Nov-08 15:09 
GeneralRe: how to set up an Additional Include path Pin
Tim Deveaux29-Nov-08 7:58
memberTim Deveaux29-Nov-08 7:58 
GeneralRe: how to set up an Additional Include path Pin
haibara29-Nov-08 15:54
memberhaibara29-Nov-08 15:54 
Generallink problems Pin
del Fuego13-Nov-08 6:36
memberdel Fuego13-Nov-08 6:36 
GeneralRe: link problems Pin
Jerry Evans13-Nov-08 6:47
memberJerry Evans13-Nov-08 6:47 
GeneralRe: link problems Pin
del Fuego13-Nov-08 7:02
memberdel Fuego13-Nov-08 7:02 
QuestionRe: link problems Pin
Jerry Evans13-Nov-08 7:38
memberJerry Evans13-Nov-08 7:38 

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01 | 2.8.190306.1 | Last Updated 25 Aug 2007
Article Copyright 2007 by The Ultimate Toolbox
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid