Click here to Skip to main content
12,820,031 members (29,658 online)
Click here to Skip to main content
Add your own
alternative version


87 bookmarked
Posted 14 Sep 2002

Adding "Merge cells" support to the MFC GridControl

, 29 Dec 2002
Rate this:
Please Sign up or sign in to vote.
Adding "Merge cells" support to the MFC GridControl.

Sample Image - gridcontrl.jpg

Sample Image - print.jpg


This example demonstrates how to add "merge cells" ability to Chris Maunder's MFC Grid control (derived from CWnd).

The example modifies only two classes: CGridCellBase and CGridCtrl. In the class CGridCellBase ,the following four private member variables are added:

CCellRange m_MergeRange;
bool m_IsMergeWithOthers;
CCellID m_MergeCellID;
bool m_Hide;

Six public member functions are also added:

void UnMerge();
virtual bool IsShow() ;
virtual CCellRange GetMergeRange();
virtual bool IsMergeWithOthers();
virtual CCellID GetMergeCellID();
virtual void SetMergeCellID(CCellID cell);

In the class CGridCtrl, the following six public member functions are added:

CGridCellBase* GetCell(CCellID cell);
CCellID GetMergeCellID(CCellID cell);
void UnMergeSelectedCells();
void MergeSelectedCells();
void UnMergeCells(int nStartRow, int nStartCol, int nEndRow, int nEndCol);
void MergeCells(int nStartRow, int nStartCol, int nEndRow, int nEndCol);


  • Use CGridCtrl::MergeSelectedCells() and MergeCells() to merge the selected cells;
  • Use CGridCtrl::UnMergeSelectedCells() and UnMergeCells() to restore the selected cell to its original state.


  • 1.7 fixed the bug when merging in the corner, by Luther Bruck - 2002/12/31
  • 1.6 fixed the bug when resizing the merged row - 2002/11/20
  • 1.5 fixed the bug when resizing the merged column - 2002/11/18
  • 1.4 bug when printing merge cell across pages fixed - 2002/10/10
  • 1.3 fixed cells merge added - 2002/9/27
  • 1.2 print bug fixed - 2002/9/23
  • 1.1 scroll bug fixed - 2002/9/18
  • 1.0 first release - 2002/9/15

(Note that users of the Grid control are bound by Chris' copyright requirements detailed on his web page.)


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Huang Wei
Web Developer
China China
He got Master degree of Theory and Application of Automatic Control from Tsinghua university(Beijing,China) in 1997,from then on he has been working for Tsinghua TongFang Software Co.Ltd,Beijing China.

You may also be interested in...

Comments and Discussions

GeneralRe: Clicking on a merged cell and moving by keyboard to merged cell - Solution Pin
andrewtruckle24-May-06 0:24
memberandrewtruckle24-May-06 0:24 
GeneralAdd MergeCell function with CGridCtrl2.25 Pin
MajorVon11-Aug-05 23:09
memberMajorVon11-Aug-05 23:09 
GeneralRe: Add MergeCell function with CGridCtrl2.25 Pin
andrew.truckle@atkinsglobal.com19-Sep-05 1:42
memberandrew.truckle@atkinsglobal.com19-Sep-05 1:42 
Questionautosize merged cells? Pin
markoot28-Jul-05 11:19
membermarkoot28-Jul-05 11:19 
If i have text in cell (1,1) and i merged cells (1,1,1,7)
and when my text is too big for cell (1,1) AutoSize function resizes my (1,1) cells, but my text isn't too big for merged cells (1,1,1,7) (i mean there is a lot of place in whole row which is merged as one cells)$;
Better for me would be AutoSize also for merge cells.

Hmm, i will try to draw mky problem;
I have :
|MyBigText___| <-this one is merged

After using AutoSize i have

Column number 0 shouldn't be resized

// ___ means spacebar

Somebody helps me?
Thanks a lot.

//I hope i will be understood

GeneralI fixed the bug that it can't show the last line of grid. Pin
victor_zhao10-Jul-05 23:07
membervictor_zhao10-Jul-05 23:07 
Generalcannot downlaod the demo Pin
manjuvk9-Apr-05 20:35
membermanjuvk9-Apr-05 20:35 
GeneralVirtual Mode Pin
TimPreuss5-Jan-05 3:32
memberTimPreuss5-Jan-05 3:32 
Generalword wrapping Pin
andrew.truckle@atkinsglobal.com7-Oct-04 4:47
memberandrew.truckle@atkinsglobal.com7-Oct-04 4:47 
GeneralRe: word wrapping Pin
Ice137421-May-06 6:45
memberIce137421-May-06 6:45 
GeneralRe: word wrapping Pin
andrewtruckle21-May-06 9:14
memberandrewtruckle21-May-06 9:14 

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
Web02 | 2.8.170308.1 | Last Updated 30 Dec 2002
Article Copyright 2002 by Huang Wei
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid