Click here to Skip to main content
Licence CPOL
First Posted 20 Aug 2006
Views 130,512
Bookmarked 183 times

Groupable ListView

By | 15 Sep 2006 | Article
Extended ListView with GUIs for easy grouping.

Sample Image - GroupableListView.png

Introduction

Some time ago, I thought about writing a small extension to the ListView control. This would dynamically create a graphic user interface to allow the end-user create/modify groups, something like ‘GROUP BY’ in SQL. The control will automatically create a ToolStrip and a ToolStripButton for each column.

Usage

To use the control, just add ExListView.cs to your project, and switch the GroupsGUIs property to true, if you want to give the possibility to group items for the users.

Properties

  • GroupsGUIs - Show or not the ToolStrip to allow group items.
  • ToolStripImage - The image to show on ToolStripButtons of the groups.
  • ShowGroupLabel - Show or hide the 'ShowGroup' label.
  • ShowGroupLabelText - The 'ShowGroup' label. Default: 'Group by:'.

Example

I prepared a small example project, with a ListView which contains some employees. Each employee has a name, sex, and a job. So, if the user wants to group the employees by sex, he just needs to press one button, and the ListView will analyze each ListViewItem and create groups for the data. It creates one group for each different data in selected colums.

Here you see an example of ListView, grouped by Sex and Job:

Sample Image - GroupableListView.png

Code

To analyze and create the groups, I wrote a GroupBy(ColumnHeader[] Headers) method, it’s a simple thread-safe method with a few loops which add groups for each new different data found.

delegate void dGroupBy(ColumnHeader[] Headers);
public void GroupBy(ColumnHeader[] Headers)
{
    if (this.InvokeRequired)
    {
        dGroupBy d = new dGroupBy(GroupBy);
        this.Invoke(d, new object[] { Headers });
    }
    else
    {
        //code
        foreach (ListViewItem lvi in this.Items)
        {
            string header = "";

            foreach (ColumnHeader ch in Headers)
            {
                header += " " + 
                   lvi.SubItems[ch.Index].Text;
            }
            ListViewGroup group = 
                new ListViewGroup(header);
            ListViewGroup found = null;
            foreach (ListViewGroup g in Groups)
            {
                if (g.Header == group.Header)
                { found = g; break; }
            }
            if (found == null)
            {
                this.Groups.Add(group);
                group.Items.Add(lvi);
            }
            else
            {
                found.Items.Add(lvi);
            }
        }
    }
}

Sample Image - GroupableListView.png

History

  • 06/09/06 – v.1.01: Dock fill bug fixed.
  • 20/08/06 – Initial release.

License

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

About the Author

Kel_

Software Developer (Senior)

Belgium Belgium

Member

Follow on Twitter Follow on Twitter
Roman Atachiants (aka Kel) is a software engineer and scientist with extensive experience in different computer science domains, programming languages/principles/patterns & frameworks.
 
Founder and active developer of the Spike-Engine Project: www.spike-engine.com, a real-time networking library with automatic proxy generation.
 
His main expertise consists of C# and .Net platform, Video Gaming technology, Data Bases and Artificial Intelligence. He has an extensive programming knowledge and R&D expertise. He successfully designed various large software systems: from CRMs and ERPs to video games and on-demand movie distribution system.

 


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

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralVery nice extension! PinmemberSam Jacobs12:40 17 Mar '10  
Generalwrite text in Grouping PinmemberRohit Mahajan21:39 21 May '08  
GeneralVista Style Group/Sort PinmemberAlexandru Stanciu17:54 27 Feb '08  
QuestionNo scroll bar PinmemberBill W4:29 15 Nov '07  
QuestionStrange Issue Pinmember-_-_-_-_-_21:10 22 Aug '07  
AnswerRe: Strange Issue Pinmember-_-_-_-_-_21:18 22 Aug '07  
QuestionWidth-Problem when ListView in LargeIcon-Mode PinmemberStephan Mantel3:21 17 Jun '07  
GeneralGrouping Sorted Pinmemberchancer1014:56 16 May '07  
GeneralSeem a bug PinmemberdvptUml16:05 3 Apr '07  
Generaluse combobox instead of a toolstrip... [modified] PinmemberTheCardinal21:04 17 Oct '06  
QuestionExpand/Collapse Group Pinmemberriz33222:06 3 Oct '06  
AnswerRe: Expand/Collapse Group Pinmembersonofdaedalus8:23 24 Jan '07  
QuestionRe: Expand/Collapse Group PinmemberSoxiz11:32 11 Jun '07  
QuestionA Question Pinmemberphonefans7:41 15 Sep '06  
AnswerRe: A Question PinmemberKel_9:47 15 Sep '06  
GeneralRe: A Question Pinmemberphonefans16:52 15 Sep '06  
GeneralThanks but there is no group if listview is full dock Pinmemberjvhow20:59 5 Sep '06  
GeneralRe: Thanks but there is no group if listview is full dock PinmemberKel_22:00 5 Sep '06  
GeneralI think its great. Pinmemberilovthecov1:16 29 Aug '06  
GeneralRe: I think its great. PinmemberKel_1:24 29 Aug '06  
Questiontree diagram produced by ? PinmemberBillWoodruff1:11 26 Aug '06  
AnswerRe: tree diagram produced by ? PinmemberKel_5:51 26 Aug '06  
QuestionWhy this code ???? PinmemberDotNET747:04 22 Aug '06  
AnswerRe: Why this code ???? PinmemberSteven Roebert11:07 22 Aug '06  
QuestionReturn of Groupable ListView ....? PinmemberMahesh Sapre20:29 20 Aug '06  

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.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 15 Sep 2006
Article Copyright 2006 by Kel_
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid