Click here to Skip to main content
15,883,961 members
Articles / Desktop Programming / MFC
Article

The Ultimate Grid Beginner's Guide

Rate me:
Please Sign up or sign in to vote.
4.33/5 (6 votes)
24 Aug 2007CPOL6 min read 251.3K   42   82
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:

Image 1

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:

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


#include "stdafx.h"

#include "mycug.h" // add the header to the YourApp and YourView cpp files  

#include "test.h"

#include "MainFrm.h"

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

C++
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:

C++
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:

C++
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:

C++
/***************************************************
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:

Image 2
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:

Image 3

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:

C++
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:

C++
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:

C++
#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)


Written By
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.
This is a Organisation

476 members

Comments and Discussions

 
GeneralRe: about displaying picture Pin
jiangbing20-Jul-08 15:41
jiangbing20-Jul-08 15:41 
Generalabout Static Lib Settings Pin
jiangbing8-Jul-08 4:03
jiangbing8-Jul-08 4:03 
GeneralRe: about Static Lib Settings Pin
Tim Deveaux8-Jul-08 13:21
Tim Deveaux8-Jul-08 13:21 
GeneralRe: about Static Lib Settings Pin
jiangbing8-Jul-08 16:21
jiangbing8-Jul-08 16:21 
GeneralRe: about Static Lib Settings Pin
Tim Deveaux9-Jul-08 5:57
Tim Deveaux9-Jul-08 5:57 
Questionthe applications in the "Demos" folder can not work, why? Pin
jiangbing7-Jul-08 5:06
jiangbing7-Jul-08 5:06 
AnswerRe: the applications in the "Demos" folder can not work, why? Pin
Tim Deveaux7-Jul-08 6:28
Tim Deveaux7-Jul-08 6:28 
GeneralRe: the applications in the "Demos" folder can not work, why? Pin
jiangbing7-Jul-08 15:41
jiangbing7-Jul-08 15:41 
Yes, i have downloaded all the folders and installed them as shown in the picture. The applications in the "samples" folder works well while the applications in the "Demos" folder can not work. I mean that they can be compiled without any problem, but the .exe application can not work. For example the CellTypes project int the Demos folder can be compiled and linked, then we can get the "Cell types.exe" application. when Run it , the application shows a messagebox:
File:viewhtml.cpp
Line:419
For information on how your program can cause an asertion failure, see the Visual C++ documetation on asserts.
(Press Retry to debug the application)

I do not know why.
Thanks for your hospitality! Smile | :)
GeneralRe: the applications in the "Demos" folder can not work, why? Pin
Tim Deveaux8-Jul-08 13:45
Tim Deveaux8-Jul-08 13:45 
Generaluglibsel.h problem (DLL build) Pin
Graham Shanks5-Jun-08 0:26
Graham Shanks5-Jun-08 0:26 
GeneralRe: uglibsel.h problem (DLL build) Pin
Tim Deveaux5-Jun-08 6:49
Tim Deveaux5-Jun-08 6:49 
GeneralRe: uglibsel.h problem (DLL build) Pin
Graham Shanks5-Jun-08 12:09
Graham Shanks5-Jun-08 12:09 
GeneralReplacing one grid with another Pin
Timothy W. Okrey21-Jan-08 7:01
professionalTimothy W. Okrey21-Jan-08 7:01 
GeneralPopup menu Pin
dobrzan15-Jan-08 12:46
dobrzan15-Jan-08 12:46 
GeneralLink issues Pin
The Yariv15-Jan-08 9:48
The Yariv15-Jan-08 9:48 
GeneralRe: Link issues Pin
Tim Deveaux15-Jan-08 10:01
Tim Deveaux15-Jan-08 10:01 
QuestionAnother link error Pin
dobrzan14-Jan-08 21:43
dobrzan14-Jan-08 21:43 
GeneralRe: Another link error Pin
Tim Deveaux15-Jan-08 6:46
Tim Deveaux15-Jan-08 6:46 
GeneralRe: Another link error Pin
dobrzan15-Jan-08 12:34
dobrzan15-Jan-08 12:34 
GeneralLink Errors Pin
aelfassi27-Sep-07 2:32
aelfassi27-Sep-07 2:32 
GeneralRe: Link Errors Pin
Tim Deveaux27-Sep-07 3:39
Tim Deveaux27-Sep-07 3:39 
GeneralRe: Link Errors Pin
aelfassi27-Sep-07 3:48
aelfassi27-Sep-07 3:48 
GeneralLink Errors Pin
aelfassi26-Sep-07 6:44
aelfassi26-Sep-07 6:44 
GeneralRe: Link Errors Pin
Tim Deveaux26-Sep-07 7:05
Tim Deveaux26-Sep-07 7:05 
GeneralCUGExpandType quirk Pin
mhorowit24-Sep-07 16:34
mhorowit24-Sep-07 16:34 

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.