Click here to Skip to main content
11,707,470 members (70,202 online)
Click here to Skip to main content

Add Group Collapse Behavior on a Listview Control

, 26 Nov 2008 CPOL 101.8K 4.5K 46
Rate this:
Please Sign up or sign in to vote.
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:

    [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:

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

    [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:

    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

#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. Frown | :(
  • 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)

Share

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

 
QuestionAlign collapse/expand icon to the left Pin
HKe197219-Aug-15 5:55
memberHKe197219-Aug-15 5:55 
BugThere is a bug... Pin
Mikro_12317-Dec-13 21:47
memberMikro_12317-Dec-13 21:47 
QuestionFixing click on collapse icon / Make it run on 32bit Pin
Ρac-Μan23-Sep-12 5:58
memberΡac-Μan23-Sep-12 5:58 
AnswerRe: Fixing click on collapse icon / Make it run on 32bit Pin
Andira Muttakim29-Dec-12 4:57
memberAndira Muttakim29-Dec-12 4:57 
GeneralRe: Fixing click on collapse icon / Make it run on 32bit Pin
PacMani27-Jan-13 6:38
memberPacMani27-Jan-13 6:38 
GeneralRe: Fixing click on collapse icon / Make it run on 32bit Pin
Ed Gadziemski18-Oct-14 18:41
professionalEd Gadziemski18-Oct-14 18:41 
AnswerRe: Fixing click on collapse icon / Make it run on 32bit Pin
Flanter30-May-13 15:38
memberFlanter30-May-13 15:38 
GeneralRe: Fixing click on collapse icon / Make it run on 32bit Pin
DebugErr16-Jul-14 9:49
memberDebugErr16-Jul-14 9:49 
QuestionButton from the group bar Pin
Member 865897530-Jul-12 21:33
memberMember 865897530-Jul-12 21:33 
QuestionCollapse/Expand is not working when you open the MainForm from another Form Pin
msiddu30-Jul-12 19:23
membermsiddu30-Jul-12 19:23 
AnswerRe: Collapse/Expand is not working when you open the MainForm from another Form Pin
Mikro_12317-Dec-13 22:00
memberMikro_12317-Dec-13 22:00 
QuestionDoesn't work Pin
heavylifting18-May-12 8:13
memberheavylifting18-May-12 8:13 
AnswerRe: Doesn't work Pin
heavylifting18-May-12 8:16
memberheavylifting18-May-12 8:16 
GeneralRe: Doesn't work Pin
Shargon_8520-Oct-14 21:41
memberShargon_8520-Oct-14 21:41 
GeneralRepaint problem on Vista/Win7 [modified] Pin
ZhuJinYong26-May-11 18:01
memberZhuJinYong26-May-11 18:01 
GeneralMy vote of 5 Pin
ChewsHumans21-Oct-10 11:48
memberChewsHumans21-Oct-10 11:48 
GeneralMy vote of 3 Pin
o.olll12-Aug-10 20:36
membero.olll12-Aug-10 20:36 
GeneralCorrections Pin
imess18-Jan-10 10:35
memberimess18-Jan-10 10:35 
GeneralRe: Corrections Pin
Member 235497011-Feb-10 5:58
memberMember 235497011-Feb-10 5:58 
GeneralMy vote of 1 Pin
ts2224-Jul-09 10:25
memberts2224-Jul-09 10:25 
GeneralRe: My vote of 1 Pin
Scott Dorman15-Sep-09 5:26
memberScott Dorman15-Sep-09 5:26 
Generalgetting and setting the correct Group Id Pin
Paw Jershauge18-May-09 20:37
memberPaw Jershauge18-May-09 20:37 
QuestionHow to do in VB.Net? Pin
Jimmy Bobby29-Apr-09 14:02
memberJimmy Bobby29-Apr-09 14:02 
AnswerRe: How to do in VB.Net? Pin
Paw Jershauge13-May-09 22:52
memberPaw Jershauge13-May-09 22:52 
QuestionHow to make the click on the group arrow collapse the group... Pin
fmaeseele21-Jan-09 7:21
memberfmaeseele21-Jan-09 7:21 
AnswerRe: How to make the click on the group arrow collapse the group... Pin
rantinori2-May-09 15:54
memberrantinori2-May-09 15:54 
GeneralRe: How to make the click on the group arrow collapse the group... Pin
lewisv2-Jul-09 4:45
memberlewisv2-Jul-09 4:45 
GeneralRe: How to make the click on the group arrow collapse the group... Pin
rantinori2-Jul-09 9:20
memberrantinori2-Jul-09 9:20 
GeneralRe: How to make the click on the group arrow collapse the group... Pin
mcarmonar835-Nov-09 21:24
membermcarmonar835-Nov-09 21:24 
GeneralRe: How to make the click on the group arrow collapse the group... Pin
rantinori6-Nov-09 7:28
memberrantinori6-Nov-09 7:28 
AnswerRe: How to make the click on the group arrow collapse the group... Pin
ZhuJinYong25-Jul-11 23:39
memberZhuJinYong25-Jul-11 23:39 
Generalnot working under xp, VS 2005 Pin
zgelic9-Jan-09 11:40
memberzgelic9-Jan-09 11:40 
GeneralRe: not working under xp, VS 2005 Pin
Paw Jershauge14-May-09 0:33
memberPaw Jershauge14-May-09 0:33 
GeneralI hadn't test the code under Windows XP or win2k Pin
charju27-Nov-08 15:00
membercharju27-Nov-08 15:00 
GeneralRe: I hadn't test the code under Windows XP or win2k Pin
sroche26-Jan-09 4:17
membersroche26-Jan-09 4:17 
GeneralSource Code... Pin
Paw Jershauge26-Nov-08 21:47
memberPaw Jershauge26-Nov-08 21:47 
GeneralRe: Source Code... Pin
charju26-Nov-08 22:53
membercharju26-Nov-08 22:53 
GeneralRe: Source Code... Pin
Paw Jershauge27-Nov-08 0:09
memberPaw Jershauge27-Nov-08 0:09 

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