Click here to Skip to main content
13,296,374 members (50,697 online)
Click here to Skip to main content
Add your own
alternative version


64 bookmarked
Posted 6 Jun 2005


, 6 Jun 2005
Rate this:
Please Sign up or sign in to vote.
This article describes GridCtrl that consists of common controls such as EditBox, ComboBox and Button. The GridCtrl helps you create, exchange and manage layouts.

Sample Image


Some times I work at forms that have many data fields, such as combo, edit and buttons. Also, I need removing, dynamically adding and managing the layout of these fields automatically. Finding nothing such as I needed, I decided to create a control by myself, that should have these features.

I want to express my thanks to Mathias Tunared for his AdvComboBox that I am using in my project.

What the GridCtrl does:

  • Creates three controls: edit, combo or button; that have a static (header) field on left side;
  • Combines these fields into column and row, that have header (on top);
  • Allows you to have access to any field with help index or ID manager;
  • Makes exchanging data with fields possible;
  • Adds or removes any field at run-time.

How to use

At first, you should create GridCtrl. For that, use the function Create, that is defined as:

bool CGridCtrl::Create(DWORD dwStyle,RECT& rct,CWnd* pParent,UINT nID);
  • DWORD dwStyle - style of the GridCtrl which can be one of the following:
    • 0 - create simple grid;
    • GS_AUTOVSIZE - create grid that will change its own vertical size.
  • RECT& rct - size of GridCtrl. If you define GS_AUTOVSIZE, RECT::bottom has no effect.
  • CWnd* pParent - parent window.
  • UINT nID - ID of GridCtrl.

Before adding a field, I should explain some features. The GridCtrl has a class CGridMgr, that manages operations like insertion, deletion etc. It's available via the overloaded operator->. There are two ways in GridCtrl for managing these operations: using the appropriate functions or using properties.

gi_hdr(const char* pcName=0, DWORD dwStyle=0, 
        DWORD dwID=-1, void* pvData=0); //Header item
gi_edit(...);  //Edit item
gi_combo(...); //Combo item

const char* pcName //- title;
DWORD dwStyle      //- style

/////// Layout style /////////
#define GLS_NR        0x000000    //create with new row
#define GLS_NC        0x010000    //create with new col

/////// Grid edit style //////////////
#define GES_PASS    0x1        //password
#define GES_NUMB    0x2        //number

/////// Grid combo style /////////////
#define GCS_DRDNL    0x0        //Drop down list
#define GCS_DRDN    0x1        //Drop down

/////// Grid custom style ////////////
#define GCSS_CSBT    0x0100 
// Item with custom button
// (send command message to parent when click)

/////// Grid shared style ////////////
#define GSS_RDNL    0x10000000    //Item is readonly
  • DWORD dwID - ID of item (any message sent to parent is a standard notifications message).
  • void* pvData - User defined data.

Now, we have enough information for creating GridCtrl and adding some items. Let me show an example to you, so that you can fully appreciate the process:

CGridCtrl m_Grid;

m_Grid <<gi_hdr("Header")

    <<gi_combo("Combo (drop down list) with cbutton",GCS_DRDNL|GCSS_CSBT,0x03)

//Follow code example how to exhange data with items:

//set text to edit
m_Grid->ID[0x00]->Text="Edit Text";
//insert item into ComboBox
m_Grid->ID[0x01]->SubItem->Insert("Text ListBox Item",-1,0);
//select first item

//get text from edit
CString strEdit(m_Grid->ID[0x00]->Text);

//get current selectetd item from combo
int nSel(m_Grid->ID[0x01]->SubItem->Select);

//get text from first item
CString strSubItem(m_Grid->ID[0x01]->SubItem->Text[0]);


CString strSubItem(m_Grid->ID[0x01]->SubItem->Text);
//get text from current selected item

Function and classes

  • Class CGridCtrl
    • bool Create(DWORD dwStyle,RECT& rct,CWnd* pParent,UINT nID) - see top of this article.
    • _GridMgr* operator->() - gives access to a _GridMgr object.
    • CGridCtrl& operator<<(grid_item& gi) - insert new item to back.
    • void MoveWindow(RECT* rct) - move grid control according to the set GS_AUTOVSIZE flag.
  • template<class TObj>class CGridMgr
    • CItemMgr<TOBJ>* ID[] - get property for accessing item manager with ID.
    • CItemMgr<TOBJ>* Index[] - get property for accessing item manager with Index.
    • bool ReadOnlyAll - put property set's all item as read only.
    • DWORD Count - get property return number of item in manager.
    • void DeleteAll() - delete all items from grid manager.
  • template<class TObj> class CItemMgr
    • bool Insert(grid_item& gi) - insert item into item manager.
    • bool Delete() - remove item from item manager.
    • CString Name - put/get property set/get header item name.
    • CString Text - put/get property set/get item text.
    • GI_TYPE Type - get property return type of item.
    • void* Data - put/get property set/get user defined data to/from item.
    • CSubItemMgr<CADVCOMBOBOX>* SubItem - get property return subitem manager for an item as combo box.
    • int ID - get property return item ID.
    • int Index - get property return item index.
    • bool ReadOnly - put/get property set/get read only state.
    • void SetFocus() - sets focus to item.
  • template<class TCombo> class CSubItemMgr
    • void Insert(const char* pcText,LPARAM lData=-1,int nIndex=-1) - insert subitem into combobox.
    • void Delete(int nIndex=-1) - delete by index. If nIndex = -1, last item will be deleted.
    • int Find(const char* pcText) - find item with specified string. If the find succeeds return index of the item, otherwise return -1.
    • int Find(LPARAM lData) - find item with specified data, return value same as int Find(const char* pcText).
    • int Count - get property, return count item in combobox.
    • int Select - put/get property, set/get selection to currently selected item in combo box.
    • CString Text[] - get property, get text from item with index.
    • LPARAM Data[] - get property, get data from item with index.

Color and messages

Color of GridCtrl has been selected at the time of development, but you can change any color using macros:


#define G_BKGND          0xC9DBC8        //background
#define G_GHEAD_CLR      0x9EC29B        //color of header (from item top)
#define G_IHEAD_CLR      0xACC2AB        //color of header (at item left)
#define G_EDIT_RDNL_CLR  0xE7EEE7        //background of readonly items

Any message from items in the grid are sent to parent window that created the grid. Thr ID of message is the ID that is passed to one of these:



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


About the Author

Arshynkin Maksym
Ukraine Ukraine
No Biography provided

You may also be interested in...

Comments and Discussions

General"class" should change to "typename" in vc9 Pin
stonexin22-Feb-11 16:27
memberstonexin22-Feb-11 16:27 
GeneralRe: "class" should change to "typename" in vc9 Pin
sl11l11l11l30-May-11 23:18
membersl11l11l11l30-May-11 23:18 
GeneralRe: "class" should change to "typename" in vc9 Pin
stonexin31-May-11 5:08
memberstonexin31-May-11 5:08 
In vc9,I change the keyword "class" to "typename"
int file GridCtrl.h line 320 to solve it.

Laugh | :laugh:
GeneralRe: "class" should change to "typename" in vc9 Pin
sl11l11l11l31-May-11 18:18
membersl11l11l11l31-May-11 18:18 
GeneralItem split percentage Pin
jaclambert20-Nov-07 10:40
memberjaclambert20-Nov-07 10:40 
GeneralSolution for problem with Visual Styles enabled Pin
mi-chi4-Jul-07 5:58
membermi-chi4-Jul-07 5:58 
GeneralFeature request Pin
Boniolopez29-May-06 0:03
memberBoniolopez29-May-06 0:03 
GeneralRe: Feature request Pin
Arshinkin Maxim20-Jul-06 4:13
memberArshinkin Maxim20-Jul-06 4:13 
QuestionHow can i handle the button click event? Pin
dirlee26-Dec-05 15:59
memberdirlee26-Dec-05 15:59 
AnswerRe: How can i handle the button click event? Pin
L2Max5-Jan-06 2:32
memberL2Max5-Jan-06 2:32 
GeneralVery usefull Pin
caesten5-Dec-05 13:11
membercaesten5-Dec-05 13:11 
GeneralExactly what i was looking for Pin
tabor2521-Jul-05 11:29
membertabor2521-Jul-05 11:29 
GeneralI can't test it in VC6 Pin
Anonymous22-Jun-05 0:41
sussAnonymous22-Jun-05 0:41 
GeneralRe: I can't test it in VC6 Pin
i8423-Aug-05 22:53
memberi8423-Aug-05 22:53 
GeneralRe: I can't test it in VC6 Pin
onlysfang25-Oct-08 3:10
memberonlysfang25-Oct-08 3:10 
GeneralRe: I can't test it in VC6 Pin
shaonew14-Dec-09 21:05
membershaonew14-Dec-09 21:05 
GeneralVisual Studio Format Version 7.00 Pin
jan marco15-Jun-05 19:08
memberjan marco15-Jun-05 19:08 
GeneralRe: Visual Studio Format Version 7.00 Pin
Pinselwascher22-Jun-05 5:25
memberPinselwascher22-Jun-05 5:25 
GeneralCOOL Pin
MMs_xH9-Jun-05 0:03
memberMMs_xH9-Jun-05 0:03 
GeneralRe: COOL Pin
Anonymous9-Jun-05 5:20
sussAnonymous9-Jun-05 5:20 
GeneralRe: COOL Pin
MMs_xH9-Jun-05 5:50
memberMMs_xH9-Jun-05 5:50 
GeneralRe: COOL Pin
L2Max9-Jun-05 9:51
memberL2Max9-Jun-05 9:51 

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 | Terms of Use | Mobile
Web01 | 2.8.171207.1 | Last Updated 7 Jun 2005
Article Copyright 2005 by Arshynkin Maksym
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid