Click here to Skip to main content
12,701,133 members (31,428 online)
Click here to Skip to main content
Add your own
alternative version


87 bookmarked

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

BugMergeCell Sort Pin
Member 1038662910-Feb-15 19:38
memberMember 1038662910-Feb-15 19:38 
GeneralMy vote of 5 Pin
Faint789020-Apr-13 23:01
memberFaint789020-Apr-13 23:01 
Questionset color of tool bar button text Pin
Uday P.Singh16-Feb-12 2:57
memberUday P.Singh16-Feb-12 2:57 
GeneralDrawing issue in merging cell Pin
Member 304756327-May-11 4:19
memberMember 304756327-May-11 4:19 
GeneralSetItemBkColour function will make grid line disappear Pin
zhuguo30-Jun-09 2:06
memberzhuguo30-Jun-09 2:06 
GeneralMerged cell disapears while sorting Pin
TanmaySathe17-Feb-09 19:14
memberTanmaySathe17-Feb-09 19:14 
QuestionHow can i generate a DLL and an OCX file for this code in your attachment? Pin
praveen_sap25-Jan-09 12:55
memberpraveen_sap25-Jan-09 12:55 
General[Message Removed] Pin
immetoz6-Oct-08 8:55
memberimmetoz6-Oct-08 8:55 
Questionunable to download the attachment file Pin
G Haranadh30-Jul-08 3:48
memberG Haranadh30-Jul-08 3:48 
Generalprint probs Pin
zhkingsmac7-Jul-08 17:35
memberzhkingsmac7-Jul-08 17:35 
Generalin virtual mode, merge cells function doesn't work properly Pin
Member 46478006-Mar-08 22:02
memberMember 46478006-Mar-08 22:02 
QuestionPrintCell is wrong Pin
andrewtruckle5-Aug-07 21:06
memberandrewtruckle5-Aug-07 21:06 
AnswerRe: PrintCell is wrong Pin
andrewtruckle5-Aug-07 23:01
memberandrewtruckle5-Aug-07 23:01 
OK, I had a look at an older set of data files I have for the grid control and I noticed the PrintCell worked a little differently:

            crFG = (GetBackClr() != CLR_DEFAULT ||
                (GetTextClr() != CLR_DEFAULT && GetTextClr() != pDefaultCell->GetTextClr())) ?
                    GetTextClr() : RGB(0, 0, 0);

This fixes my font colours. Now my print preview behaves for font colours.

As for the grid lines being drawn, I did this:

//Used for merge cells
    //by Huang Wei
//  AJT
// pDC->Rectangle(rect);

This sorts me out. If I was using merged cells then I think I would have a problem here, but I don't. The thing is, it is the GRID that draws the cells grid lines in OnPrint just after the PrintCell call. Thus the ->Rectangle call should NOT be done in PrintCell. If anything, OnPrint should be adjusted just after the PrintCell call to allow for merged cells and thus print the boundary of the merged cell based on the GridLines parameters. I am not brave enough to do this. Smile | :)

QuestionHow to save the merged grid infomation?? Pin
hhuwangzhp198026-Jun-07 19:01
memberhhuwangzhp198026-Jun-07 19:01 
GeneralAdding a BITMAP Pin
hodeck~10-Sep-06 21:08
memberhodeck~10-Sep-06 21:08 
QuestionHas anyone got this working properly in v2.26 ? Pin
jfougero4-Sep-06 18:05
memberjfougero4-Sep-06 18:05 
AnswerRe: Has anyone got this working properly in v2.26 ? Pin
hamo200827-Nov-06 21:05
memberhamo200827-Nov-06 21:05 
AnswerRe: Has anyone got this working properly in v2.26 ? Pin
Member 382164027-Jun-08 0:56
memberMember 382164027-Jun-08 0:56 
Generalbug: Merge the merged cells --fixed Pin
hamo200826-Jul-06 6:40
memberhamo200826-Jul-06 6:40 
GeneralRe: bug: Merge the merged cells --fixed Pin
cuiting9-Dec-12 23:18
membercuiting9-Dec-12 23:18 
GeneralProblem deleting rows Pin
andrewtruckle8-Jun-06 2:39
memberandrewtruckle8-Jun-06 2:39 
GeneralRe: Problem deleting rows Pin
andrewtruckle8-Jun-06 4:53
memberandrewtruckle8-Jun-06 4:53 
GeneralRe: Problem deleting rows - FIXED! Pin
andrewtruckle9-Jun-06 4:01
memberandrewtruckle9-Jun-06 4:01 
GeneralAutoSizeColumn problem Pin
andrewtruckle24-May-06 22:04
memberandrewtruckle24-May-06 22:04 
GeneralClicking on a merged cell and moving by keyboard to merged cell Pin
andrewtruckle23-May-06 22:49
memberandrewtruckle23-May-06 22:49 
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 
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 
QuestionCan I have Tree Cells and merged cells on the same grid Pin
Rafael Melo13-Sep-04 10:45
sussRafael Melo13-Sep-04 10:45 
QuestionHow to print MFC GridControl ? Pin
lzs14-Aug-04 18:10
memberlzs14-Aug-04 18:10 
GeneralI can't download the source files. Pin
TommyChoi18-Jun-04 19:59
sussTommyChoi18-Jun-04 19:59 
General2.25version Pin
Darter27-Apr-04 2:24
memberDarter27-Apr-04 2:24 
GeneralRe: 2.25version Pin
igrzalja30-Aug-04 5:33
memberigrzalja30-Aug-04 5:33 
GeneralRe: 2.25version Pin
andrew.truckle@atkinsglobal.com19-Sep-05 1:46
memberandrew.truckle@atkinsglobal.com19-Sep-05 1:46 
GeneralRe: 2.25version Pin
qz_yc_kj13-Nov-05 4:37
memberqz_yc_kj13-Nov-05 4:37 
GeneralRe: 2.25version Pin
homerhuang21-Feb-06 18:42
memberhomerhuang21-Feb-06 18:42 
GeneralRe: 2.25version Pin
mikewolfli25-Apr-06 23:32
membermikewolfli25-Apr-06 23:32 
GeneralProblem with Scrolling Pin
JIJESH.G1-Mar-04 2:16
memberJIJESH.G1-Mar-04 2:16 
GeneralRe: Problem with Scrolling Pin
Kiato L.6-Aug-04 2:49
sussKiato L.6-Aug-04 2:49 
GeneralProblem with printing merging cells in fixed columns Pin
wlegierski2-Dec-03 2:29
memberwlegierski2-Dec-03 2:29 
GeneralRe: Problem with printing merging cells in fixed columns Pin
yuhonggang27-Nov-04 6:02
memberyuhonggang27-Nov-04 6:02 
GeneralRe: Problem with printing merging cells in fixed columns Pin
Cui Sheng14-Feb-05 23:15
memberCui Sheng14-Feb-05 23:15 
GeneralProblem when changing cell colors Pin
JAlcant12327-Oct-03 13:30
memberJAlcant12327-Oct-03 13:30 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170118.1 | Last Updated 30 Dec 2002
Article Copyright 2002 by Huang Wei
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid