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


40 bookmarked
Posted 24 Aug 2007
Licenced CPOL

The Ultimate Grid Cell Types

, 24 Aug 2007
Rate this:
Please Sign up or sign in to vote.
An introduction to using built-in and custom cell types with the Ultimate Grid

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

Source code and project files for this sample are included in the Demos\Cell types directory of the sample projects download.



Cell types...

...and more cell types.

The display and interaction properties of a cell are handled through the assignment of cell type classes to individual cell objects. These cell types can be built-in types or add-ons (either your own derived cell type classes or one of a selection of additional types provided in the CellTypes directory of the source code download).

Available Cell Types

There are a number of pre-built cell types that ship with the Ultimate Grid. Built-in types are selectable through #defined values passed to CUGCell::SetCellType or CUGCtrl::QuickSetCellType. Some cell types also have extended cell type #defines that can be used to modify their behavior through calls to CUGCell::QuickSetCellTypeEx or CUGCtrl::QuickSetCellTypeEX.

Many cell types also provide for special notification codes which are sent to the grid's virtual OnCellTypeNotify function (e.g. the drop list type will send UGCT_DROPLISTSTART and UGCT_DROPLISTSELECT.

Note that most cell types will use the default CEdit derived edit class for text editing, but that other edit controls may be selected as well.

Here is a brief overview of the major cell types that ship with the Ultimate Grid and their extended and notification codes:

Built-in typesBuilt in types can be selected into the cell through the use of #defined values.

Normal text

Built-in type #define: UGCT_NORMAL (the default cell type for the grid)

Extended #defines: (note that the normal cell type is the base class for all others, so these CellTypeEx attributes can be applied to most cell types that display text)


Drop list

Built-in type #define: UGCT_DROPLIST

Extended #defines:

Notification #defines:

Check box

Built-in type #define: UGCT_CHECKBOX

Extended #defines:


Notification #defines:


Built-in type #define: UGCT_ARROW

Extended #defines:

Additional typesAdd-on types must be added to the grid through the CUGCtrl::AddCellType function, and set to the cell using the index returned.

Auto font

Will automatically size the font of any cell so that all of the text will be visible.


Simulates a standard windows push button control.

Extended #defines:

Notification #defines:

Auto size

Extends the standard cell type providing functionality to size its cell to make sure that cell's data is visible in full. It will not make columns or rows smaller, but it will enlarge them whenever needed.

Date time picker

Displays a button along with a cell that can be used to activate a full featured Date-Time picker.

Notification #defines:

Drop grid

The CUGCTDropGrid cell type was created to provide users with ability to show a popup window containing a grid, just like a drop list. By default an instance of the CDropDownCug will be created and used as the grid control within the popup window, but it can be replaced with any <code>CDropDownCug derived class through the OnCellTypeNotify notification.

Extended #defines:

Notification #defines:

Outlook header

Provides gradient drawing of cells for an 'Outlook' style headers.


Draws the cells value as text. Plus adds a ellipsis button on the right hand side of the cell. This button when clicked will fire a notification.

Extended #defines:

Notification #defines:


Expanding/collapsing cell type. This cell type displays a button which, when pressed, will expand or collapse a group of rows under it, up until the next cell of the same type.


Can display a label as well as cell text in the same cell.


Displays text in a horizontally scrolling 'marquee' style.

Multi font

Can display text using multiple fonts in the same cell.


A note type cell will display an indicator when text is set to the label - this text will display when the mouse is over the cell in a multi-line, note window (note - this is in addition to any text displayed as a tooltip in the OnHint override).

Pie chart

Displays a pie chart in a cell.

Extended #defines:

Progress / Advanced progress

Progress bar cell types that can display different types of bars.

For the CUGProgress type the UGCT_PROGRESSUSELABEL extended flag is available.

For the CUGAdvProgressType:

Extended #defines:


Can display a group of radio buttons.

Notification #define:


Displays a slider like control in a cell.

Notification #define:

Sort arrow

Displays an up or down sort arrow in a (typically) header cell in response to a user click.

Notification #defines:

Spin button

Allows spin button control over cell values.

Extended #define:

Notification #defines:

Using Cell Types

The code that follows is typical of code you would add to your derived CUGCtrl class to create and use a simple cell type. We provide a MyCug class skeleton that you can copy to your project to get started - we'll use that class here.

For each non-built-in type you'll need to add the CPP file for that type to your project. Cell type code files live in the CellTypes directory. Include the header file for that type and create a member variable and index in your MyCug.h header:

#include "ugctrl.h"

#include "resource.h" 

#include "UGCTSpin.h"    // include header for spin type

class MyCug: public CUGCtrl 
     //****** Create the New Object and reserve an index member

     CUGSpinButtonType m_spin;
     int               m_nSpinIndex

Next, the new cell type needs to be added to the grid's array of available types. This is typically done along with other initialization in the OnSetup member of the derived grid control:

void MyCug::OnSetup()
     //Spin Button cell type

     m_nSpinIndex = AddCellType(&m_spin);

Next, assuming we've set some rows and columns to the grid, we can add code either in OnSetup or OnReset to set the spin type as the controlling type for one or more cells. In the following code (taken from the Cell Types demo in the Demos directory) the param member of the cell is also set with an app defined constant that serves to differentiate a number of cells that are using this cell type in different ways (displaying text or numeric values, e.g.):

CUGCell cell;    
// get the cell we want


// could also use cell.SetNumber()

// tell the cell to draw and interact using the celltype at this 

// index (spin type)          


// optional - spin buttons will be hidden until cell has focus


// extra info can be stored in the param member of the cell 


// set the cell data back to the grid


So, at this point we have a cell controlled by the CUGSpinButtonType. When the user interacts with the up or down spin buttons, we'll receive notifications in our derived grid controls OnCellTypeNotify method:

int CCellTypeDemoGrid::OnCellTypeNotify(long ID,int col,long row,long msg,
    long param)
     CUGCell cell;
     long num;


     if(ID == m_nSpinIndex) 
           if(msg == UGCT_SPINBUTTONUP)
           else  //  UGCT_SPINBUTTONDOWN

     // cell.SetNumber would work as well...

     CString str;

Et voila, the spin button can modify the cell data.

Cell Types and Virtual Loading

Cell types can also be used with data that is loaded from a custom datasource, with some limitations based on how much data can persist - typically, with a database oriented datasource like DAO or ODBC, the datasource's GetCell function retrieves only the text/number associated with that datum, so things like the param data of the cell cannot persist, but you can still vector the grid controls OnGetCell notification to use custom types with a cell.

This snippet from the MyAccess datasource sample shows how a derived grid might override OnGetCell to set a boolean cell to use a checkbox type:

void CUGAccessCtrl::OnGetCell(int col,long row,CUGCell *cell)
     if (row < 0 )

     if(-1 < row)
          int type = 0;
          // Change bool cell types to check box.

          if ((CUGDAODataSource*)GetDataSource(
              GetDefDataSource())->GetColType(col, &type) == UG_SUCCESS)
               switch (type) {
               case 1: // boolean

                    cell->SetCellTypeEx(UGCT_CHECKBOX3DRAISED) ;

A spin type could work as well for numeric data, and might be handy if restricting input to certain stepped increments.

Again, depending on the nature of the datasource, some cell types will be problematic to use. Consider the drop list, note, and label types, which rely on the label text data of the cell - a db type datasource will typically persist only the text/number cell attribute back to the database, so values set with cell.SetLabelText will be lost.

Creating Custom Cell Types

The CUGCellType is a CObject derived class that doubles as the default cell type and the base class for all grid cell types. Note that while some cell types — such as the note or calendar types — may have the capability of displaying a window in response to a user action, they are not CWnd derivatives. The CUGGrid member of the CUGCtrl class is the active CWnd for the control, and has the responsibility of relaying mouse, key, and paint messages to the cell type.

Here is a subset of the virtual message based methods most likely to be overridden in a new CUGCellType derived class:

virtual BOOL OnLClicked(int col,long row,int updn,RECT *rect,POINT *point);

virtual BOOL OnRClicked(int col,long row,int updn,RECT *rect,POINT *point);
virtual BOOL OnDClicked(int col,long row,RECT *rect,POINT *point);

virtual BOOL OnKeyDown(int col,long row,UINT *vcKey);
virtual BOOL OnKeyUp(int col,long row,UINT *vcKey);

virtual BOOL OnCharDown(int col,long row,UINT *vcKey);
virtual BOOL OnMouseMove(int col,long row,POINT *point,UINT nFlags);

virtual void OnDraw(CDC *dc,RECT *rect,int col,long row,CUGCell *cell,int 
    selected,int current);

virtual BOOL OnDrawFocusRect(CDC *dc,RECT *rect,int col,int row);

And there are also general purpose overrides for common functionality:

//general purpose routines

virtual int DrawBitmap(CDC *dc,CBitmap * bitmap,RECT *rect,COLORREF backcolor);

virtual void DrawBorder(CDC *dc,RECT *rect,RECT *rectout,CUGCell * cell);
virtual void DrawText(CDC *dc,RECT *rect,int offset,int col,long row,
    CUGCell *cell, int selected,int current);

virtual void DrawBackground(CDC *dc,RECT *rect,COLORREF backcolor, 
    int row = -1, int col = -1, CUGCell * cell = NULL, bool current = false, 
    bool selected = false);

virtual int GetCellOverlapInfo(CDC* dc,int col,long row,
    int *overlapCol,CUGCell *cell);
bool DrawThemedText(HDC dc, int left, int top, RECT* rect, LPCTSTR string, 
    int stringLen, DWORD textFormat, UGXPCellType cellType, UGXPThemeState 

See the documentation and Include\ugceltyp.h file for more on the available overrides.

Probably the simplest cell type to create would involve only an override to OnDraw, and draw a colored circle.

You may or may not need to create a new cell type, but it can be an excellent way to add custom functionality to the grid. In many cases, you may find it's enough to modify an existing type to get the particular drawing or interactive quality that is required.


Initial CodeProject release August 2007.


The add-on cell types listed above have evolved over several years/releases but remained as add-on types. One of the original goals of the Ultimate Grid design was maintaining a small footprint with respect to code size, and incorporating cell types on an 'as needed' basis was part of that philosophy.

Some users, notably those using the static lib build of the grid, might like to modify the code base to incorporate one or more add-on cell types into the control as built-in types. This can be done by modifying the CUGCtrl class to accommodate the types in the same way as it does the current built in types. You'll find the code that calls AddCellType for the built-in types in the constructor of CUGCtrl:

     //add the standard cell types


The indices returned from these calls correspond to the #defines declared in Include\UGDefine.h :

//Normal CellType/CellType Ex Values - most other cell types 

//also use these values, so bits 1-8 are reserved generally reserved

#define UGCT_NORMAL 0


//Droplist CellType

//CheckBox CellType

//Arrow CellType

#define UGCT_ARROW 3

So, additional types could be added to CUGCtrl.h as member objects, added to the grid's available built-in types in the destructor, and the corresponding additional built-in type #defines added to ugdefine.h to add more built in types.


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

QuestionDroplist clipped to height of the grid Pin
Member 41579874-Apr-14 4:23
memberMember 41579874-Apr-14 4:23 
QuestionExpand Cell Type messes up on InsertRow Pin
rblum11-Mar-13 5:31
memberrblum11-Mar-13 5:31 
QuestionAbout Bitmaps Example Pin
billyleung8820-Sep-12 23:14
memberbillyleung8820-Sep-12 23:14 
QuestionDateTime can not change value Pin
maplewang18-Jul-12 16:23
membermaplewang18-Jul-12 16:23 
QuestionIs there any way to bind a datasource to an dropdownlist? Pin
maplewang15-Jul-12 21:04
membermaplewang15-Jul-12 21:04 
Questioncheckbox cell type does not work Pin
sdphg12-Nov-11 22:52
membersdphg12-Nov-11 22:52 
Generalbug for pie chart cell type - FYI Pin
honnymoon23-May-10 20:28
memberhonnymoon23-May-10 20:28 
QuestionDate Time picker that actually lets you pick times? Pin
spoky7-Apr-10 7:07
memberspoky7-Apr-10 7:07 
GeneralAccess violation in Marquee Cell Type Pin
spoky24-Feb-10 4:03
memberspoky24-Feb-10 4:03 
GeneralCalendar Sample Pin
Adamw18-Feb-09 10:03
memberAdamw18-Feb-09 10:03 
Generalabout adding bitmap into cell [modified] Pin
jiangbing15-Feb-09 22:08
memberjiangbing15-Feb-09 22:08 
GeneralButton Focus Problem Pin
AcePocket10-Jul-08 15:52
memberAcePocket10-Jul-08 15:52 
GeneralRe: Button Focus Problem Pin
Tim Deveaux11-Jul-08 6:50
memberTim Deveaux11-Jul-08 6:50 
QuestionCheckbox color/shade Pin
Bob Conway2-Jul-08 16:09
memberBob Conway2-Jul-08 16:09 
AnswerRe: Checkbox color/shade Pin
Tim Deveaux3-Jul-08 13:18
memberTim Deveaux3-Jul-08 13:18 
GeneralRe: Checkbox color/shade Pin
allen shiels16-Jul-08 15:41
memberallen shiels16-Jul-08 15:41 
AnswerRe: Checkbox color/shade Pin
Brett Rowbotham18-Jul-08 1:59
memberBrett Rowbotham18-Jul-08 1:59 
GeneralRe: Checkbox color/shade Pin
charshep24-Jul-08 10:57
membercharshep24-Jul-08 10:57 
Questionhow to create check box already checked? Pin
oddpoint11-Apr-08 18:38
memberoddpoint11-Apr-08 18:38 
AnswerRe: how to create check box already checked? Pin
Tim Deveaux12-Apr-08 6:41
memberTim Deveaux12-Apr-08 6:41 
GeneralBug in datetime picker Pin
mhorowit27-Sep-07 20:08
membermhorowit27-Sep-07 20:08 
QuestionHow To use Date Time picker to get date and Time Pin
mhorowit27-Sep-07 20:05
membermhorowit27-Sep-07 20:05 
AnswerRe: How To use Date Time picker to get date and Time Pin
Tim Deveaux28-Sep-07 8:19
memberTim Deveaux28-Sep-07 8:19 
GeneralRe: How To use Date Time picker to get date and Time Pin
mhorowit28-Sep-07 8:37
membermhorowit28-Sep-07 8:37 
QuestionHow to use CRichEditCtrl in Ultimate Grid? Pin
brazilien5-Sep-07 22:04
memberbrazilien5-Sep-07 22:04 

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
Web03 | 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