Click here to Skip to main content
11,922,641 members (28,452 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


114 bookmarked

CListCtrl and Grouping Rows

, 27 Jul 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
Example of how to enable categories in the MFC list control


Microsoft's CListCtrl has support for displaying data in a grid, but also supports grouping of data. This article will demonstrate how we can activate the grouping functionality of CListCtrl.

The demo application allows you to experience how grouping can be used. Just right-click a column header, to group the data according to that header.



Microsoft extended the CListCtrl with support for grouping, with the release of Windows XP. The new feature wasn't promoted that much, and was also limited in functionality (no collapsing of groups). The implementation was later extended with more functionality when Windows Vista was released (collapsing, footer, subtitle, and more).

There are already some .NET articles describing how to use grouping in Windows XP:

There is also MSDN - Windows Vista Control Enhancements, which describes some of the new stuff in Windows Vista.

The first time I actually saw a useful application make use of this grouping feature was with TortoiseSVN 1.5.

How To Activate Grouping in CListCtrl

Before grouping can be activated, some things have to be in order:

  • The Operating System must support Common Controls ver. 6 (Windows XP/Vista and newer)
  • The application must enable Common Controls ver. 6 through its manifest
  • The application must be compiled with _WIN32_WINNT set to at least 0x0501

When the above requirements are met, we can create a group like this:

LRESULT CListCtrl_Category_Groups::CreateSingleGroup(int nIndex, 
                  int nGroupId, const CString& strHeader)
    EnableGroupView( TRUE );
    LVGROUP lg = {0};
    lg.cbSize = sizeof(lg);
    lg.iGroupId = nGroupId;
    lg.state = LVGS_NORMAL;
    lg.uAlign = LVGA_HEADER_LEFT;
    // Header-title must be unicode (Convert if necessary)
    lg.pszHeader = strHeader.GetBuffer();
    lg.cchHeader = strHeader.GetLength();
    nGroupId = InsertGroup(nIndex, &lg );
    if (nGroupId==-1)
        return nGroupId;
    // Insert all current items into this group
    for(int nRow = 0; nRow < GetItemCount(); ++nRow)
        LVITEM lvItem = {0};
        lvItem.mask = LVIF_GROUPID;
        lvItem.iItem = nRow;
        lvItem.iSubItem = 0;
        lvItem.iGroupId = nGroupID;
        SetItem( &lvItem );

The above example code creates a new group with the following properties:

  • The group will be inserted at nIndex in the CListCtrl internal list of groups.
  • The group will get the external identifier nGroupId.
  • The group headline will become the text-string strHeader.

Limitations in Windows XP

When working with groups on Windows XP, we will discover the following are missing:

  • It is not possible to ask how many groups there are in the CListCtrl internal list of groups. Instead, it is recommended to keep track of the created groups ourselves.
  • It is not possible to iterate over the groups in the CListCtrl internal list of groups. Instead, it is recommended to keep track of the created groups ourselves.
  • It is not possible to ask the CListCtrl if the mouse cursor is currently over a group. Instead, we have to do a lot of guessing.
  • It is not possible to change the group state, so it becomes collapsed.

These limitations have been solved with Windows Vista, and at the same time, the following features have been added:

  • Can attach a task-link to the group
  • Can provide a subtitle-text that is placed just below the header-text
  • Can provide a footer-text to the group

Using the Source Code

The source code provided demonstrates how to activate the different grouping features:

  • InsertGroupHeader() - Creates a new group. Wrapper around CListCtrl::InsertGroup().
  • SetRowGroupId() - Adds a row to an existing group. Wrapper around CListCtrl::SetItem() with LVIF_GROUPID.
  • GroupByColumn(int nCol) - Creates a new group for each unique cell text in the specified column.
  • SortColumn(int nCol) - Sorts the groups generated from the specified column. Wrapper around CListCtrl::SortGroups().
  • GroupHitTest(const CPoint& point) - Attempts to find the group below the given mouse point.
  • CheckEntireGroup() - When having the CListCtrl extended style LVS_EX_CHECKBOXES enabled, this method will change the check box state of an entire group.

It also demonstrates how to use some of the new Windows Vista features:

  • CollapseAllGroups() - Loops through all the groups and makes them collapse. Wrapper around CListCtrl::SetGroupInfo() with LVGS_STATE.
  • SetGroupTask() - Changes the task-link of a group. When the user clicks the task link, it will generate a LVN_LINKCLICK message.
  • SetGroupSubtitle() - Changes the subtitle of a group.
  • SetGroupFooter() - Changes the footer of a group.


  • 2008-09-16 - First release of the article
  • 2009-09-28 - Backport of bug-fixes from CGridListCtrlEx, and at startup it now groups the rows by the 3rd column
  • 2011-07-27 - Fixed bug in GroupHitTest() when used on Vista\Win7 and compiled with _WIN32_WINNT >= 0x0600


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Rolf Kristensen
Software Developer
Denmark Denmark
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionCustom Draw: borders are drawn between items Pin
Gregory Nisnevich19-Oct-14 13:29
memberGregory Nisnevich19-Oct-14 13:29 
QuestionHow to get items from Group? Pin
Omar.Pessoa27-Aug-14 8:00
memberOmar.Pessoa27-Aug-14 8:00 
AnswerRe: How to get items from Group? Pin
Rolf Kristensen3-Sep-14 23:12
memberRolf Kristensen3-Sep-14 23:12 
Questionhow can i group column Pin
yixuan17830-Jul-14 17:40
memberyixuan17830-Jul-14 17:40 
AnswerRe: how can i group column Pin
Rolf Kristensen30-Jul-14 21:20
memberRolf Kristensen30-Jul-14 21:20 
GeneralRe: how can i group column Pin
yixuan1781-Aug-14 5:43
memberyixuan1781-Aug-14 5:43 
QuestionHow to change the style of Collapse Button? Pin
handsome0317-Aug-13 22:32
memberhandsome0317-Aug-13 22:32 
QuestionHow to change the header text color ? Pin
Mike Kuna21-Jun-13 3:58
memberMike Kuna21-Jun-13 3:58 
AnswerRe: How to change the header text color ? Pin
Rolf Kristensen22-Jun-13 1:04
memberRolf Kristensen22-Jun-13 1:04 
GeneralRe: How to change the header text color ? Pin
Mike Kuna25-Jun-13 4:18
memberMike Kuna25-Jun-13 4:18 
GeneralMy vote of 5 Pin
Mihai MOGA10-May-13 19:41
memberMihai MOGA10-May-13 19:41 
GeneralMy vote of 5 Pin
RiqiTang23-Feb-13 7:24
memberRiqiTang23-Feb-13 7:24 
GeneralMy vote of 5 Pin
JJMatthews18-Jul-12 0:57
memberJJMatthews18-Jul-12 0:57 
QuestionSort by Numbers Pin
jens_weller1-Jun-12 0:32
memberjens_weller1-Jun-12 0:32 
AnswerRe: Sort by Numbers Pin
Rolf Kristensen2-Jun-12 3:31
memberRolf Kristensen2-Jun-12 3:31 
QuestionGrouping and WM_SETFONT Pin
Davide Zaccanti20-May-12 0:28
memberDavide Zaccanti20-May-12 0:28 
QuestionIcon view problem Pin
Rohit Dubey from Hyderabad28-Feb-12 6:27
memberRohit Dubey from Hyderabad28-Feb-12 6:27 
GeneralGreat article. Pin
Richard Hunn16-Nov-11 4:49
memberRichard Hunn16-Nov-11 4:49 
GeneralMy vote of 5 Pin
ZhuJinYong22-Jul-11 0:09
memberZhuJinYong22-Jul-11 0:09 
GeneralRe: My vote of 5 Pin
Rolf Kristensen26-Jul-11 11:10
memberRolf Kristensen26-Jul-11 11:10 
GeneralRe: My vote of 5 [modified] Pin
ZhuJinYong26-Jul-11 16:30
memberZhuJinYong26-Jul-11 16:30 
GeneralRe: My vote of 5 Pin
Rolf Kristensen27-Jul-11 5:24
memberRolf Kristensen27-Jul-11 5:24 
GeneralRe: My vote of 5 [modified] Pin
ZhuJinYong27-Jul-11 5:39
memberZhuJinYong27-Jul-11 5:39 
GeneralGreat! Pin
clxye14-Jul-11 16:03
memberclxye14-Jul-11 16:03 
GeneralMy vote of 1 Pin
tha_specializt8-Dec-10 5:01
membertha_specializt8-Dec-10 5: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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.151125.1 | Last Updated 27 Jul 2011
Article Copyright 2008 by Rolf Kristensen
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid