Click here to Skip to main content
11,928,740 members (47,733 online)
Click here to Skip to main content
Add your own
alternative version


46 bookmarked

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.

You may also be interested in...

Comments and Discussions

QuestionCollapse/Expand is not working when you open the MainForm from another Form Pin
msiddu30-Jul-12 20:23
membermsiddu30-Jul-12 20:23 
AnswerRe: Collapse/Expand is not working when you open the MainForm from another Form Pin
Mikro_12317-Dec-13 23:00
memberMikro_12317-Dec-13 23:00 

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