Click here to Skip to main content
Click here to Skip to main content

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.

Comments and Discussions

GeneralFix to problem not showing grid when changing cell colors Pinmembershawkins9-Jan-04 8:48 
GeneralRe: Fix to problem not showing grid when changing cell colors PinsussAnonymous16-Apr-04 11:42 
GeneralRe: Problem when changing cell colors Pinmemberjfougero5-Sep-06 7:04 
GeneralRe: Problem when changing cell colors Pinmembercplky1-Feb-09 0:05 
GeneralHmmmmm Pinmembertankhoasp3-Oct-03 21:14 
GeneralBug when inserting rows before merged-columns row... PinmemberLeaetherStrip20-Aug-03 0:07 
GeneralOK, I fixed it myself :) PinmemberLeaetherStrip20-Aug-03 1:00 
GeneralRe: OK, I fixed it myself :) Pinmemberandrew.truckle@atkinsglobal.com19-Sep-05 20:43 
GeneralRe: Minor Bug... PinmemberYails22-Mar-06 4:06 
GeneralRe: Minor Bug... Pinmemberandrewtruckle23-Mar-06 0:38 
GeneralRe: OK, I fixed it myself :) [modified] Pinmemberandrewtruckle23-May-06 21:54 
Generalload or save merge cells Pinmemberlzp2-Jun-03 20:32 
GeneralRe: load or save merge cells PinmemberBigFanTian22-Mar-04 15:38 
GeneralBug with Merged Cells Pinmemberbummel14-Apr-03 23:49 
GeneralRe: Bug with Merged Cells - my FIX Pinmemberbummel15-Apr-03 3:01 
Generalbug Pinmemberxipeng29-Mar-03 5:24 
GeneralBug In Your Merge Application Pinmemberabosafia7-Mar-03 11:38 
GeneralRe: Bug In Your Merge Application Pinmembersangbee18-Mar-03 13:05 
GeneralRe: Bug In Your Merge Application Pinmemberabosafia18-Mar-03 13:54 
QuestionCells Still Have Postions After Merge..? Pinmemberabosafia3-Mar-03 12:47 
AnswerRe: Cells Still Have Postions After Merge..? Pinmemberandrewtruckle23-May-06 22:52 
GeneralAn importante bug Pinmemberjaneyre14-Jan-03 23:04 
GeneralRe: An importante bug PinsussAnonymous16-Jan-03 14:50 
Generalcode changes Pinmembernomadik13-Jan-03 7:07 
Generalmerge cells in virtual mode Pinmemberbluescreen11-Jan-03 9:55 
Generalbug fix for unicode Pinmemberbluescreen11-Jan-03 9:21 
Generalbugfix PinmemberLuther Bruck27-Dec-02 3:47 
GeneralRe: bugfix PinmemberHuang Wei30-Dec-02 14:29 
GeneralRe: bugfix top left corner Pinmemberbluescreen11-Jan-03 9:53 
GeneralCorner merge bug PinmemberLuther Bruck12-Dec-02 3:45 
GeneralProblem with merging cell(0,0) PinsussRVK25-Nov-02 0:07 
GeneralMulti-row fixed sells and merge PinsussLuther Bruck12-Nov-02 23:01 
GeneralRe: Multi-row fixed sells and merge PinmemberHuang Wei13-Nov-02 16:48 
GeneralRe: Multi-row fixed sells and merge PinmemberHuang Wei17-Nov-02 15:01 
GeneralRe: Multi-row fixed sells and merge PinmemberLuther Bruck18-Nov-02 23:14 
GeneralRe: Multi-row fixed sells and merge PinmemberHuang Wei19-Nov-02 20:08 
GeneralBest jobs PinsussAnonymous2-Oct-02 1:15 
Questionprint merged cell? Pinsussyt_lhj27-Sep-02 16:09 
AnswerRe: print merged cell? PinmemberHuang Wei27-Sep-02 20:17 
GeneralRe: print merged cell? Pinsussyt_lhj27-Sep-02 22:20 
Generalsuggestion PinsussAnonymous26-Sep-02 17:32 
GeneralRe: suggestion PinmemberHuang Wei29-Sep-02 15:54 
Generalbug PinsussAnonymous26-Sep-02 17:28 
GeneralBug PinsussAnonymous24-Sep-02 0:10 
GeneralRe: Bug PinmemberHuang Wei24-Sep-02 15:09 
GeneralRe: Bug PinsussAnonymous29-Sep-02 15:49 
Generalupdate PinmemberHuang Wei16-Sep-02 18:27 
GeneralRe: update PinmemberHolger Persch16-Sep-02 23:15 
GeneralRe: update PinmemberJohn Johansson17-Sep-02 0:42 
GeneralRe: update PinmemberHuang Wei17-Sep-02 17:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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
Web04 | 2.8.150224.1 | Last Updated 30 Dec 2002
Article Copyright 2002 by Huang Wei
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid