Click here to Skip to main content
15,887,214 members
Articles / Desktop Programming / Windows Forms
Article

Add Group Collapse Behavior on a Listview Control

Rate me:
Please Sign up or sign in to vote.
4.65/5 (25 votes)
26 Nov 2008CPOL 209.9K   7.7K   51   42
Group collapse behavior added to a listview control under Windows Vista

Introduction

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:

lvnormal.jpg

Default group without Collapse/Expand behavior

lvgroup.jpg

Group with Collapse/Expand behavior

Using the Code

  • Define ListView group struct wrapper:

    C#
    [StructLayout(LayoutKind.Sequential)] 
    public struct LVGROUP 
    { 
            public int cbSize; 
            public int mask; 
            [MarshalAs(UnmanagedType.LPTStr)] 
            public string pszHeader; 
            public int cchHeader; 
            [MarshalAs(UnmanagedType.LPTStr)] 
            public string pszFooter; 
            public int cchFooter; 
            public int iGroupId; 
            public int stateMask; 
            public int state; 
            public int uAlign; 
    } 
  • Define the ENUM data:

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

    C#
    [DllImport("user32.dll")] 
    static extern int SendMessage
    	(IntPtr window, int message, int wParam, IntPtr lParam);
  • Kernel method to set collapse/expand behavior on the ListView group:

    C#
    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;
                    try{
                            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){
                            System.Diagnostics.Trace.WriteLine
    			(ex.Message + Environment.NewLine + ex.StackTrace);
                    }
                    finally{
                            if (null != ip) Marshal.FreeHGlobal(ip);
                    }
           }
    } 

Points of Interest

C++
#define LVM_SETGROUPINFO (LVM_FIRST + 147)
#define ListView_SetGroupInfo(hwnd, iGroupId, pgrp) \
SNDMSG((hwnd), LVM_SETGROUPINFO, (WPARAM)(iGroupId), (LPARAM)(pgrp)) 

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

Limitations

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

History

  • 27th November, 2008: Initial post

License

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


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

Comments and Discussions

 
GeneralRe: getting and setting the correct Group Id Pin
Member 1479827910-Apr-20 6:26
Member 1479827910-Apr-20 6:26 
QuestionHow to do in VB.Net? Pin
Jimmy Bobby29-Apr-09 14:02
Jimmy Bobby29-Apr-09 14:02 
AnswerRe: How to do in VB.Net? Pin
Paw Jershauge13-May-09 22:52
Paw Jershauge13-May-09 22:52 
QuestionHow to make the click on the group arrow collapse the group... Pin
fmaeseele21-Jan-09 7:21
fmaeseele21-Jan-09 7:21 
AnswerRe: How to make the click on the group arrow collapse the group... Pin
rantinori2-May-09 15:54
rantinori2-May-09 15:54 
GeneralRe: How to make the click on the group arrow collapse the group... Pin
lewisv2-Jul-09 4:45
lewisv2-Jul-09 4:45 
GeneralRe: How to make the click on the group arrow collapse the group... Pin
rantinori2-Jul-09 9:20
rantinori2-Jul-09 9:20 
GeneralRe: How to make the click on the group arrow collapse the group... Pin
mcarmonar835-Nov-09 21:24
mcarmonar835-Nov-09 21:24 
GeneralRe: How to make the click on the group arrow collapse the group... Pin
rantinori6-Nov-09 7:28
rantinori6-Nov-09 7:28 
AnswerRe: How to make the click on the group arrow collapse the group... Pin
ZhuJinYong25-Jul-11 23:39
ZhuJinYong25-Jul-11 23:39 
Generalnot working under xp, VS 2005 Pin
ZGelic9-Jan-09 11:40
ZGelic9-Jan-09 11:40 
GeneralRe: not working under xp, VS 2005 Pin
Paw Jershauge14-May-09 0:33
Paw Jershauge14-May-09 0:33 
GeneralI hadn't test the code under Windows XP or win2k Pin
Charles Ju27-Nov-08 15:00
Charles Ju27-Nov-08 15:00 
GeneralRe: I hadn't test the code under Windows XP or win2k Pin
sroche26-Jan-09 4:17
sroche26-Jan-09 4:17 
GeneralSource Code... Pin
Paw Jershauge26-Nov-08 21:47
Paw Jershauge26-Nov-08 21:47 
GeneralRe: Source Code... Pin
Charles Ju26-Nov-08 22:53
Charles Ju26-Nov-08 22:53 
GeneralRe: Source Code... PinPopular
Paw Jershauge27-Nov-08 0:09
Paw Jershauge27-Nov-08 0:09 

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.