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

The Ultimate Grid Beginner's Guide

, 25 Aug 2007 CPOL
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.

Contents

Introduction

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:

/***************************************************
OnSetup
    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

     SetNumberRows(rows);
     SetNumberCols(cols);

     // fill cells with data

     GetCell(0,1,&cell);
     for (i = 0; i < cols; i++) {
          for (j = 0; j < rows; j++) {
               temp.Format("%d",(i+1)*(j+1));
               cell.SetText(temp);
               SetCell(i,j,&cell);
         }
     }
     // add column headngs

     for (i = 0; i < cols; i++) {
          temp.Format("%d",(i+1));
          cell.SetText(temp);
          SetCell(i,-1,&cell);
     }
     // add row headings

     for (j = 0; j < rows; j++) {
          temp.Format("%d",(j+1));
          cell.SetText(temp);
          SetCell(-1,j,&cell);
     }
}

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()
{
     CDialog::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.

History

Initial CodeProject release August 2007.

License

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

Share

About the Author

The Ultimate Toolbox
Web Developer
Canada Canada
In January 2005, David Cunningham and Chris Maunder created TheUltimateToolbox.com, 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

394 members


Comments and Discussions

 
QuestionI can not download UltimateGrid72_docs.zip PinmemberniceJinSoo19-Oct-14 2:37 
AnswerRe: I can not download UltimateGrid72_docs.zip Pinmemberhraw30-Oct-14 11:25 
QuestionUltimate Grid in C++ PinmemberAnthony Oyovwe15-Dec-13 6:07 
QuestionSetParam/GetParam does not allow saving a pointer in 64 bit? [modified] Pinmemberehaerim25-Oct-13 19:56 
QuestionFrozen grid in split CView PinmemberMichael B Pliam15-Sep-13 15:30 
QuestionMissing StartEdit() in MyCug::OnDClicked ? Pinmembernickhalfasleep22-Nov-11 10:11 
QuestionDebugging PinmemberMember 799071521-Nov-11 11:41 
QuestionWhen I use SortBy(col, UG_SORT_DESCENDING); RedrawAll(); , it fails to work in dialog base project. PinmemberGennady4622-Aug-11 14:25 
Questionerror LNK2001: unresolved external symbol in ultimate grid dialog base project. PinmemberGennady4628-Jul-11 12:13 
GeneralGrid not shown! Pinmembersinaocean31-Jul-10 19:56 
Questionhow to use two grid object? Pinmemberryugisang24-Sep-09 0:34 
AnswerRe: how to use two grid object? PineditorTim Deveaux25-Sep-09 6:54 
Questionhow to set up an Additional Include path Pinmemberhaibara28-Nov-08 5:59 
AnswerRe: how to set up an Additional Include path PinmemberJerry Evans28-Nov-08 6:06 
GeneralRe: how to set up an Additional Include path Pinmemberhaibara28-Nov-08 16:09 
GeneralRe: how to set up an Additional Include path PineditorTim Deveaux29-Nov-08 8:58 
GeneralRe: how to set up an Additional Include path Pinmemberhaibara29-Nov-08 16:54 
Generallink problems Pinmemberdel Fuego13-Nov-08 7:36 
GeneralRe: link problems PinmemberJerry Evans13-Nov-08 7:47 
GeneralRe: link problems Pinmemberdel Fuego13-Nov-08 8:02 
QuestionRe: link problems PinmemberJerry Evans13-Nov-08 8:38 
AnswerRe: link problems Pinmemberdel Fuego13-Nov-08 9:18 
NewsRe: link problems PinmemberJerry Evans13-Nov-08 9:48 
GeneralRe: link problems Pinmemberdel Fuego13-Nov-08 10:38 
GeneralRe: link problems PinmemberJerry Evans13-Nov-08 10:48 
GeneralRe: link problems Pinmemberdel Fuego13-Nov-08 11:19 
GeneralRe: link problems PinmemberJerry Evans13-Nov-08 10:51 
GeneralRe: link problems Pinmemberdel Fuego13-Nov-08 11:23 
GeneralRe: link problems PinmemberJerry Evans13-Nov-08 14:30 
GeneralRe: link problems Pinmemberdel Fuego14-Nov-08 6:40 
GeneralRe: link problems PineditorTim Deveaux13-Nov-08 14:33 
GeneralRe: link problems Pinmemberdel Fuego14-Nov-08 6:06 
GeneralRe: link problems Pinmemberdel Fuego14-Nov-08 6:42 
GeneralRe: link problems PineditorTim Deveaux14-Nov-08 6:52 
QuestionHow to use help file? PinmemberTommyVikberg13-Nov-08 3:18 
AnswerRe: How to use help file? PineditorTim Deveaux13-Nov-08 6:24 
GeneralRe: How to use help file? PinmemberTommyVikberg13-Nov-08 7:29 
GeneralWindows CE PinmemberCüneyt ELÝBOL16-Sep-08 10:16 
GeneralRe: Windows CE PineditorTim Deveaux16-Sep-08 14:18 
Generalabout displaying picture Pinmemberjiangbing20-Jul-08 5:06 
GeneralRe: about displaying picture PineditorTim Deveaux20-Jul-08 15:39 
GeneralRe: about displaying picture Pinmemberjiangbing20-Jul-08 16:41 
Generalabout Static Lib Settings Pinmemberjiangbing8-Jul-08 5:03 
GeneralRe: about Static Lib Settings PineditorTim Deveaux8-Jul-08 14:21 
GeneralRe: about Static Lib Settings Pinmemberjiangbing8-Jul-08 17:21 
GeneralRe: about Static Lib Settings PineditorTim Deveaux9-Jul-08 6:57 
Questionthe applications in the "Demos" folder can not work, why? Pinmemberjiangbing7-Jul-08 6:06 
AnswerRe: the applications in the "Demos" folder can not work, why? PineditorTim Deveaux7-Jul-08 7:28 
GeneralRe: the applications in the "Demos" folder can not work, why? Pinmemberjiangbing7-Jul-08 16:41 
GeneralRe: the applications in the "Demos" folder can not work, why? PineditorTim Deveaux8-Jul-08 14:45 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

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