Click here to Skip to main content
15,867,594 members
Articles / Desktop Programming / MFC
Article

GridCtrl

Rate me:
Please Sign up or sign in to vote.
4.92/5 (23 votes)
6 Jun 2005CPOL3 min read 113.1K   8.1K   65   22
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

Introduction

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_edit("Edit",0,0x00)
    <<gi_combo("Combo",0,0x01)

    <<gi_hdr("Header",GLS_NR)
    <<gi_edit("Edit(password)",GES_PASS,0x02)
    <<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
m_Grid->ID[0x01]->SubItem->Select=0;

//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]);

    //or

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:

//GridCtrl.h

#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:

gi_hdr(...)
gi_edit(...)
gi_combo(...)

License

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


Written By
Architect
Ukraine Ukraine
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
General"class" should change to "typename" in vc9 Pin
stonexin22-Feb-11 15:27
stonexin22-Feb-11 15:27 
GeneralRe: "class" should change to "typename" in vc9 Pin
sl11l11l11l30-May-11 22:18
sl11l11l11l30-May-11 22:18 
GeneralRe: "class" should change to "typename" in vc9 Pin
stonexin31-May-11 4:08
stonexin31-May-11 4:08 
GeneralRe: "class" should change to "typename" in vc9 Pin
sl11l11l11l31-May-11 17:18
sl11l11l11l31-May-11 17:18 
GeneralItem split percentage Pin
obsolete20-Nov-07 9:40
obsolete20-Nov-07 9:40 
GeneralSolution for problem with Visual Styles enabled Pin
mi-chi4-Jul-07 4:58
mi-chi4-Jul-07 4:58 
GeneralFeature request Pin
Boniolopez28-May-06 23:03
Boniolopez28-May-06 23:03 
GeneralRe: Feature request Pin
Arshynkin Maksym20-Jul-06 3:13
Arshynkin Maksym20-Jul-06 3:13 
QuestionHow can i handle the button click event? Pin
dirlee26-Dec-05 14:59
dirlee26-Dec-05 14:59 
AnswerRe: How can i handle the button click event? Pin
Arshynkin Maksym5-Jan-06 1:32
Arshynkin Maksym5-Jan-06 1:32 
GeneralVery usefull Pin
caesten5-Dec-05 12:11
caesten5-Dec-05 12:11 
GeneralExactly what i was looking for Pin
tabor2521-Jul-05 10:29
tabor2521-Jul-05 10:29 
GeneralI can't test it in VC6 Pin
Anonymous21-Jun-05 23:41
Anonymous21-Jun-05 23:41 
GeneralRe: I can't test it in VC6 Pin
23-Aug-05 21:53
suss23-Aug-05 21:53 
GeneralRe: I can't test it in VC6 Pin
onlysfang25-Oct-08 2:10
onlysfang25-Oct-08 2:10 
I've run it successfully in VC6.
thanks for i84!
GeneralRe: I can't test it in VC6 Pin
shaonew14-Dec-09 20:05
shaonew14-Dec-09 20:05 
GeneralVisual Studio Format Version 7.00 Pin
jm.alkema15-Jun-05 18:08
jm.alkema15-Jun-05 18:08 
GeneralRe: Visual Studio Format Version 7.00 Pin
Pinselwascher22-Jun-05 4:25
Pinselwascher22-Jun-05 4:25 
GeneralCOOL Pin
MMs_xH8-Jun-05 23:03
MMs_xH8-Jun-05 23:03 
GeneralRe: COOL Pin
Anonymous9-Jun-05 4:20
Anonymous9-Jun-05 4:20 
GeneralRe: COOL Pin
MMs_xH9-Jun-05 4:50
MMs_xH9-Jun-05 4:50 
GeneralRe: COOL Pin
Arshynkin Maksym9-Jun-05 8:51
Arshynkin Maksym9-Jun-05 8: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.