Click here to Skip to main content
11,411,494 members (65,142 online)
Click here to Skip to main content

Add Group Collapse Behavior on a Listview Control

, 26 Nov 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
Group collapse behavior added to a listview control under Windows Vista


ListView control is an important but complex control in the WinForm environment. Group behavior is added into this control, but unfortunately, we can't collapse or expand the group.

I'll show how to use some simple code to add collapse/expand behavior on the ListView control.

This is a sample image on Windows Vista and Windows Server 2008:


Default group without Collapse/Expand behavior


Group with Collapse/Expand behavior

Using the Code

  • Define ListView group struct wrapper:

    public struct LVGROUP 
            public int cbSize; 
            public int mask; 
            public string pszHeader; 
            public int cchHeader; 
            public string pszFooter; 
            public int cchFooter; 
            public int iGroupId; 
            public int stateMask; 
            public int state; 
            public int uAlign; 
  • Define the ENUM data:

    public enum GroupState 
            COLLAPSIBLE = 8, 
            COLLAPSED = 1, 
            EXPANDED = 0 
  • Interop for SendMessage function:

    static extern int SendMessage
    	(IntPtr window, int message, int wParam, IntPtr lParam);
  • Kernel method to set collapse/expand behavior on the ListView group:

    private void SetGroupCollapse(GroupState state)
            for (int i = 0; i <= aoc.Groups.Count; i++){
                    LVGROUP group = new LVGROUP();
                    group.cbSize = Marshal.SizeOf(group);
                    group.state = (int)state; // LVGS_COLLAPSIBLE 
                    group.mask = 4; // LVGF_STATE 
                    group.iGroupId = i;
                    IntPtr ip = IntPtr.Zero;
                            ip = Marshal.AllocHGlobal(group.cbSize);
                            Marshal.StructureToPtr(group, ip, true);
                            SendMessage(aoc.Handle, 0x1000 + 147, i, ip); // #define
                            LVM_SETGROUPINFO (LVM_FIRST + 147) 
                    catch (Exception ex){
    			(ex.Message + Environment.NewLine + ex.StackTrace);
                            if (null != ip) Marshal.FreeHGlobal(ip);

Points of Interest

#define ListView_SetGroupInfo(hwnd, iGroupId, pgrp) \

The above is the definition from SDK file: commctrl.h.


  • In the current version, the rightmost collapse/expand icon doesn't work. Frown | :(
  • The collapse/expand behavior only exist in Windows Vista and Win2k8 systems.


  • 27th November, 2008: Initial post


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


About the Author

Charles Ju
Software Developer
China China
I love C#.
I love using windbg to help customers solve the performance issues.

Comments and Discussions

BugThere is a bug... PinmemberMikro_12317-Dec-13 22:47 
QuestionFixing click on collapse icon / Make it run on 32bit PinmemberΡac-Μan23-Sep-12 6:58 
AnswerRe: Fixing click on collapse icon / Make it run on 32bit PinmemberAndira Muttakim29-Dec-12 5:57 
GeneralRe: Fixing click on collapse icon / Make it run on 32bit PinmemberPacMani27-Jan-13 7:38 
GeneralRe: Fixing click on collapse icon / Make it run on 32bit PinprofessionalEd Gadziemski18-Oct-14 19:41 
AnswerRe: Fixing click on collapse icon / Make it run on 32bit PinmemberFlanter30-May-13 16:38 
GeneralRe: Fixing click on collapse icon / Make it run on 32bit PinmemberDebugErr16-Jul-14 10:49 
QuestionButton from the group bar PinmemberMember 865897530-Jul-12 22:33 
QuestionCollapse/Expand is not working when you open the MainForm from another Form Pinmembermsiddu30-Jul-12 20:23 
AnswerRe: Collapse/Expand is not working when you open the MainForm from another Form PinmemberMikro_12317-Dec-13 23:00 
QuestionDoesn't work Pinmemberheavylifting18-May-12 9:13 
AnswerRe: Doesn't work Pinmemberheavylifting18-May-12 9:16 
GeneralRe: Doesn't work PinmemberShargon_8520-Oct-14 22:41 
GeneralRepaint problem on Vista/Win7 [modified] PinmemberZhuJinYong26-May-11 19:01 
Listview may get repaint issue when groups are collapsing or expanding with scrollbar presenting or disappearing. Tested on Win7.

A picture means everything.[^]

Follow up on 26/7/2011: API call SendMessage to force a redraw:

Public Property Let AllowRedraw(bRedraw As Boolean)

If m_hListView Then

Call SendMessage(m_hListView, WM_SETREDRAW, Abs(CLng(bRedraw)), 0&)

m_AllowRedraw = bRedraw

End If

End Property

modified on Tuesday, July 26, 2011 5:43 AM

GeneralMy vote of 5 PinmemberChewsHumans21-Oct-10 12:48 
GeneralMy vote of 3 Pinmembero.olll12-Aug-10 21:36 
GeneralCorrections Pinmemberimess18-Jan-10 11:35 
GeneralRe: Corrections PinmemberMember 235497011-Feb-10 6:58 
GeneralMy vote of 1 Pinmemberts2224-Jul-09 11:25 
GeneralRe: My vote of 1 PinmemberScott Dorman15-Sep-09 6:26 
Generalgetting and setting the correct Group Id PinmemberPaw Jershauge18-May-09 21:37 
QuestionHow to do in VB.Net? PinmemberJimmy Bobby29-Apr-09 15:02 
AnswerRe: How to do in VB.Net? PinmemberPaw Jershauge13-May-09 23:52 
QuestionHow to make the click on the group arrow collapse the group... Pinmemberfmaeseele21-Jan-09 8:21 
AnswerRe: How to make the click on the group arrow collapse the group... Pinmemberrantinori2-May-09 16:54 

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
Web03 | 2.8.150414.5 | Last Updated 27 Nov 2008
Article Copyright 2008 by Charles Ju
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid