Click here to Skip to main content
15,867,453 members
Articles / Programming Languages / C#
Article

Groupable ListView

Rate me:
Please Sign up or sign in to vote.
4.72/5 (38 votes)
15 Sep 2006CPOL1 min read 242K   9.4K   201   41
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.

C#
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)


Written By
Chief Technology Officer Misakai Ltd.
Ireland Ireland
Roman Atachiants, Ph.D. is the architect behind emitter.io service, a real-time, low-latency publish/subscribe service for IoT, Gaming. He is a software engineer and scientist with extensive experience in different computer science domains, programming languages/principles/patterns & frameworks.

His main expertise consists of C# and .NET platform, game technologies, cloud, human-computer interaction, big data and artificial intelligence. He has an extensive programming knowledge and R&D expertise.



Comments and Discussions

 
QuestionReturn of Groupable ListView ....? Pin
Mahesh Sapre20-Aug-06 20:29
Mahesh Sapre20-Aug-06 20:29 
AnswerRe: Return of Groupable ListView ....? Pin
davepermen20-Aug-06 21:01
davepermen20-Aug-06 21:01 
GeneralNice idea! Pin
Marlun20-Aug-06 9:10
Marlun20-Aug-06 9:10 
GeneralRe: Nice idea! Pin
Kel_20-Aug-06 12:12
Kel_20-Aug-06 12:12 
GeneralRe: Nice idea! Pin
Paul Conrad22-Aug-06 8:14
professionalPaul Conrad22-Aug-06 8:14 
GeneralRe: Nice idea! Pin
Jecc11-Sep-06 21:59
Jecc11-Sep-06 21:59 
GeneralAwwww Pin
Ed.Poore20-Aug-06 9:05
Ed.Poore20-Aug-06 9:05 
GeneralRe: Awwww Pin
Kel_20-Aug-06 12:14
Kel_20-Aug-06 12:14 
I still have to check W2K compatibility, haven't tested it yet ^^


-- Everything is possible, even the impossible! ^_^

GeneralRe: Awwww Pin
Ed.Poore20-Aug-06 13:49
Ed.Poore20-Aug-06 13:49 
GeneralRe: Awwww Pin
Jamie Nordmeyer15-Sep-06 11:52
Jamie Nordmeyer15-Sep-06 11:52 
GeneralRe: Awwww Pin
Ed.Poore17-Sep-06 8:33
Ed.Poore17-Sep-06 8:33 
QuestionRe: Awwww Pin
pinkjones6-Nov-06 4:08
pinkjones6-Nov-06 4:08 
AnswerRe: Awwww Pin
Jamie Nordmeyer6-Nov-06 4:19
Jamie Nordmeyer6-Nov-06 4:19 
GeneralNice! Pin
dzCepheus20-Aug-06 8:34
dzCepheus20-Aug-06 8:34 
GeneralRe: Nice! Pin
Kel_20-Aug-06 12:11
Kel_20-Aug-06 12:11 

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.