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

CListCtrl and Grouping Rows

By , 27 Jul 2011
 

Introduction

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.

screenshot.png

Background

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.mask = LVGF_GROUPID | LVGF_HEADER | LVGF_STATE | LVGF_ALIGN;
    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.

History

  • 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

License

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
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5memberMihai MOGA10 May '13 - 18:41 
This is a great inspiring article. I am pretty much pleased with your good work. You put really very helpful information. Keep it up once again.
GeneralMy vote of 5memberRiqiTang23 Feb '13 - 6:24 
Excellent, well done
GeneralMy vote of 5memberJJMatthews17 Jul '12 - 23:57 
Thank you for the article and code
QuestionSort by Numbersmemberjens_weller31 May '12 - 23:32 
Is it possible to sort numbers in correct order. No its only sorted alpabeticaly.
 
in this way its implemented
1
11
2
23
245
30
4
 
what I need is
 
1
2
4
11
23
30
245
 
Can you help me?
AnswerRe: Sort by NumbersmemberRolf Kristensen2 Jun '12 - 2:31 
The easy solution is just to convert the item text to integer (ex. atoi), and then compare those in your sort function.
 
You could also consider using CGridListCtrlEx - Grid Control Based on CListCtrl[^] and call SetSortFormatNumber() for the column-trait responsible for the column with the numbers.
QuestionGrouping and WM_SETFONTmemberDavide Zaccanti19 May '12 - 23:28 
If you change the font to list control using WM_SETFONT each row is shown using new font, but the group text will remain displayed in standard font.
 
Do you have an idea on how to change also group text font ?
 
TIA
QuestionIcon view problemmemberRohit Dubey from Hyderabad28 Feb '12 - 5:27 
First of all, thanks for code.
 
Playing with groups I had some problem, maybe someone can help me.
 
Changing list control style to 'Icon' view cause painting problem and is possible to select only the first item of a group.
GeneralGreat article.memberRichard Hunn16 Nov '11 - 3:49 
The best article I've found on CListCtrl Grouping.
GeneralMy vote of 5memberZhuJinYong21 Jul '11 - 23:09 
Good article and codes to control Group of Listview.
One question:
Why lvhitinfo.iGroup always return 0? ListView_HitTestEx can't return right Index?
 

{
#if _WIN32_WINNT >= 0x0600
#ifdef ListView_HitTestEx
#ifdef LVHT_EX_GROUP
#ifdef ListView_GetGroupInfoByIndex
LVHITTESTINFO lvhitinfo = {0};
lvhitinfo.pt = point;
ListView_HitTestEx(m_hWnd, &lvhitinfo);
if ((lvhitinfo.flags & LVHT_EX_GROUP)==0)
return -1;
 
LVGROUP lg = {0};
lg.cbSize = sizeof(lg);
lg.mask = LVGF_GROUPID;
VERIFY( ListView_GetGroupInfoByIndex(m_hWnd, lvhitinfo.iGroup, &lg) );
return lg.iGroupId;
#endif
#endif
#endif
#endif
}


modified on Friday, July 22, 2011 8:38 PM

GeneralRe: My vote of 5memberRolf Kristensen26 Jul '11 - 10:10 
Sorry about the late response. I just returned from 2 weeks vacation with my family.
 
Yes you are completely right that the quoted code simply doesn't work. According to MSDN then iGroup will only be initialized if using owner data. I will try to update the article and remove the offending code.
 
Thank you for telling.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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