Click here to Skip to main content
15,390,701 members
Articles / Desktop Programming / MFC
Posted 14 Sep 2002


90 bookmarked

Adding "Merge cells" support to the MFC GridControl

Rate me:
Please Sign up or sign in to vote.
4.87/5 (34 votes)
29 Dec 2002
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.

Comments and Discussions

QuestionCould you please upgrade your class to use the latest CGridCtrl source? It is newer with column ordering features Pin
Andrew Truckle12-Mar-22 22:54
professionalAndrew Truckle12-Mar-22 22:54 
QuestionCan't find the source code of spin control cell as the figure. Pin
gearboy7825-Oct-18 22:29
Membergearboy7825-Oct-18 22:29 
BugMergeCell Sort Pin
Member 1038662910-Feb-15 18:38
MemberMember 1038662910-Feb-15 18:38 
GeneralMy vote of 5 Pin
Faint789020-Apr-13 22:01
MemberFaint789020-Apr-13 22:01 
Questionset color of tool bar button text Pin
Uday P.Singh16-Feb-12 1:57
MemberUday P.Singh16-Feb-12 1:57 
GeneralDrawing issue in merging cell Pin
Member 304756327-May-11 3:19
MemberMember 304756327-May-11 3:19 
GeneralSetItemBkColour function will make grid line disappear Pin
zhuguo30-Jun-09 1:06
Memberzhuguo30-Jun-09 1:06 
GeneralMerged cell disapears while sorting Pin
TanmaySathe17-Feb-09 18:14
MemberTanmaySathe17-Feb-09 18:14 
QuestionHow can i generate a DLL and an OCX file for this code in your attachment? Pin
praveen_sap25-Jan-09 11:55
Memberpraveen_sap25-Jan-09 11:55 
General[Message Removed] Pin
immetoz6-Oct-08 7:55
Memberimmetoz6-Oct-08 7:55 
Questionunable to download the attachment file Pin
G Haranadh30-Jul-08 2:48
MemberG Haranadh30-Jul-08 2:48 
Generalprint probs Pin
zhkingsmac7-Jul-08 16:35
Memberzhkingsmac7-Jul-08 16:35 
Generalin virtual mode, merge cells function doesn't work properly Pin
Member 46478006-Mar-08 21:02
MemberMember 46478006-Mar-08 21:02 
QuestionPrintCell is wrong Pin
andrewtruckle5-Aug-07 20:06
Memberandrewtruckle5-Aug-07 20:06 
AnswerRe: PrintCell is wrong Pin
andrewtruckle5-Aug-07 22:01
Memberandrewtruckle5-Aug-07 22:01 
QuestionHow to save the merged grid infomation?? Pin
hhuwangzhp198026-Jun-07 18:01
Memberhhuwangzhp198026-Jun-07 18:01 
GeneralAdding a BITMAP Pin
hodeck~10-Sep-06 20:08
Memberhodeck~10-Sep-06 20:08 
QuestionHas anyone got this working properly in v2.26 ? Pin
jfoug4-Sep-06 17:05
Memberjfoug4-Sep-06 17:05 
AnswerRe: Has anyone got this working properly in v2.26 ? Pin
hamo200827-Nov-06 20:05
Memberhamo200827-Nov-06 20:05 
AnswerRe: Has anyone got this working properly in v2.26 ? Pin
Tokyo Hot26-Jun-08 23:56
MemberTokyo Hot26-Jun-08 23:56 
Generalbug: Merge the merged cells --fixed Pin
hamo200826-Jul-06 5:40
Memberhamo200826-Jul-06 5:40 
GeneralRe: bug: Merge the merged cells --fixed Pin
cuiting9-Dec-12 22:18
Membercuiting9-Dec-12 22:18 
GeneralProblem deleting rows Pin
andrewtruckle8-Jun-06 1:39
Memberandrewtruckle8-Jun-06 1:39 
GeneralRe: Problem deleting rows Pin
andrewtruckle8-Jun-06 3:53
Memberandrewtruckle8-Jun-06 3:53 
GeneralRe: Problem deleting rows - FIXED! Pin
andrewtruckle9-Jun-06 3:01
Memberandrewtruckle9-Jun-06 3:01 

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.