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

Add Group Collapse Behavior on a Listview Control

By , 26 Nov 2008
 

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

About the Author

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

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

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionFixing click on collapse icon / Make it run on 32bitmemberΡac-Μan23 Sep '12 - 5:58 
AnswerRe: Fixing click on collapse icon / Make it run on 32bitmemberAndira Muttakim29 Dec '12 - 4:57 
GeneralRe: Fixing click on collapse icon / Make it run on 32bitmemberPacMani27 Jan '13 - 6:38 
QuestionButton from the group barmemberMember 865897530 Jul '12 - 21:33 
QuestionCollapse/Expand is not working when you open the MainForm from another Formmembermsiddu30 Jul '12 - 19:23 
QuestionDoesn't workmemberheavylifting18 May '12 - 8:13 
AnswerRe: Doesn't workmemberheavylifting18 May '12 - 8:16 
GeneralRepaint problem on Vista/Win7 [modified]memberZhuJinYong26 May '11 - 18:01 
GeneralMy vote of 5memberChewsHumans21 Oct '10 - 11:48 
GeneralMy vote of 3membero.olll12 Aug '10 - 20:36 
GeneralCorrectionsmemberimess18 Jan '10 - 10:35 
GeneralRe: CorrectionsmemberMember 235497011 Feb '10 - 5:58 
GeneralMy vote of 1memberts2224 Jul '09 - 10:25 
GeneralRe: My vote of 1memberScott Dorman15 Sep '09 - 5:26 
Generalgetting and setting the correct Group IdmemberPaw Jershauge18 May '09 - 20:37 
QuestionHow to do in VB.Net?memberJimmy Bobby29 Apr '09 - 14:02 
AnswerRe: How to do in VB.Net?memberPaw Jershauge13 May '09 - 22:52 
QuestionHow to make the click on the group arrow collapse the group...memberfmaeseele21 Jan '09 - 7:21 
AnswerRe: How to make the click on the group arrow collapse the group...memberrantinori2 May '09 - 15:54 
GeneralRe: How to make the click on the group arrow collapse the group...memberlewisv2 Jul '09 - 4:45 
GeneralRe: How to make the click on the group arrow collapse the group...memberrantinori2 Jul '09 - 9:20 
GeneralRe: How to make the click on the group arrow collapse the group...membermcarmonar835 Nov '09 - 21:24 
GeneralRe: How to make the click on the group arrow collapse the group...memberrantinori6 Nov '09 - 7:28 
AnswerRe: How to make the click on the group arrow collapse the group...memberZhuJinYong25 Jul '11 - 23:39 
Generalnot working under xp, VS 2005memberzgelic9 Jan '09 - 11:40 

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 27 Nov 2008
Article Copyright 2008 by Charles Ju
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid