Click here to Skip to main content
Click here to Skip to main content
Go to top

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

Introduction

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

Usage

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

History

  • 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.)

License

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

Share

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

 
General2.25version PinmemberDarter27-Apr-04 1:24 
GeneralRe: 2.25version Pinmemberigrzalja30-Aug-04 4:33 
GeneralRe: 2.25version Pinmemberandrew.truckle@atkinsglobal.com19-Sep-05 0:46 
GeneralRe: 2.25version Pinmemberqz_yc_kj13-Nov-05 3:37 
GeneralRe: 2.25version Pinmemberhomerhuang21-Feb-06 17:42 
GeneralRe: 2.25version Pinmembermikewolfli25-Apr-06 22:32 
GeneralProblem with Scrolling PinmemberJIJESH.G1-Mar-04 1:16 
GeneralRe: Problem with Scrolling PinsussKiato L.6-Aug-04 1:49 
I had the same problem when I scrolled verticaly. The two bugs are identical so what I have done for my problem can be easily adapted for yours.
 
In the GridCtrl.cpp file, search for the OnDraw function. Then inside of this function, we have a part commented as :
// draw rest of non-fixed cells"
 
Inside this block of code, begining with

rect.bottom = nFixedRowHeight-1;
for (row = minVisibleRow; row <= maxVisibleRow; row++)

 
You will find the following line :

if(!pCell->IsMergeWithOthers())

 
Replace it by :

if(!pCell->IsMergeWithOthers() || row == minVisibleRow )

 
One of the next lines is :

pCell->Draw(pDC, row, col, rect, FALSE);

 
Replace it by :

if( row == minVisibleRow && pCell->IsMergeWithOthers())
pCell->KLDraw(pDC, row, col, rect, FALSE);
else
pCell->Draw(pDC, row, col, rect, FALSE);

 
Where KLDraw is exactly the same as the Draw function but without the lines :

if(m_Hide && !IsMerged())
{
return TRUE;
}

 
That works for me and solve my problem with the vertical scroll and the resize of the grid.
 
Minimum changes should be made to solve the same problem with the horizontal croll.
GeneralProblem with printing merging cells in fixed columns Pinmemberwlegierski2-Dec-03 1:29 
GeneralRe: Problem with printing merging cells in fixed columns Pinmemberyuhonggang27-Nov-04 5:02 
GeneralRe: Problem with printing merging cells in fixed columns PinmemberCui Sheng14-Feb-05 22:15 
GeneralProblem when changing cell colors PinmemberJAlcant12327-Oct-03 12:30 
GeneralFix to problem not showing grid when changing cell colors Pinmembershawkins9-Jan-04 7:48 
GeneralRe: Fix to problem not showing grid when changing cell colors PinsussAnonymous16-Apr-04 10:42 
GeneralRe: Problem when changing cell colors Pinmemberjfougero5-Sep-06 6:04 
GeneralRe: Problem when changing cell colors Pinmembercplky31-Jan-09 23:05 
GeneralHmmmmm Pinmembertankhoasp3-Oct-03 20:14 
GeneralBug when inserting rows before merged-columns row... PinmemberLeaetherStrip19-Aug-03 23:07 
GeneralOK, I fixed it myself :) PinmemberLeaetherStrip20-Aug-03 0:00 
GeneralRe: OK, I fixed it myself :) Pinmemberandrew.truckle@atkinsglobal.com19-Sep-05 19:43 
GeneralRe: Minor Bug... PinmemberYails22-Mar-06 3:06 
GeneralRe: Minor Bug... Pinmemberandrewtruckle22-Mar-06 23:38 
GeneralRe: OK, I fixed it myself :) [modified] Pinmemberandrewtruckle23-May-06 20:54 
Generalload or save merge cells Pinmemberlzp2-Jun-03 19:32 
GeneralRe: load or save merge cells PinmemberBigFanTian22-Mar-04 14:38 

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 | Mobile
Web04 | 2.8.140916.1 | Last Updated 30 Dec 2002
Article Copyright 2002 by Huang Wei
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid