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

Groupable ListView

By , 15 Sep 2006
 

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_
Chief Technology Officer Misakai Ltd.
Ireland Ireland
Member
Roman Atachiants is the guy behind www.spike-engine.com project, a real-time, client-server networking layer (SOA, RPC) for .NET developers.
 
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.

 

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   
GeneralVery nice extension!memberSam Jacobs17 Mar '10 - 12:40 
Generalwrite text in GroupingmemberRohit Mahajan21 May '08 - 21:39 
GeneralVista Style Group/SortmemberAlexandru Stanciu27 Feb '08 - 17:54 
QuestionNo scroll barmemberBill W15 Nov '07 - 4:29 
QuestionStrange Issuemember-_-_-_-_-_22 Aug '07 - 21:10 
AnswerRe: Strange Issuemember-_-_-_-_-_22 Aug '07 - 21:18 
QuestionWidth-Problem when ListView in LargeIcon-ModememberStephan Mantel17 Jun '07 - 3:21 
GeneralGrouping Sortedmemberchancer10116 May '07 - 4:56 
GeneralSeem a bugmemberdvptUml3 Apr '07 - 16:05 
Generaluse combobox instead of a toolstrip... [modified]memberTheCardinal17 Oct '06 - 21:04 
QuestionExpand/Collapse Groupmemberriz3323 Oct '06 - 22:06 
AnswerRe: Expand/Collapse Groupmembersonofdaedalus24 Jan '07 - 8:23 
QuestionRe: Expand/Collapse GroupmemberSoxiz11 Jun '07 - 11:32 
QuestionA Questionmemberphonefans15 Sep '06 - 7:41 
AnswerRe: A QuestionmemberKel_15 Sep '06 - 9:47 
GeneralRe: A Questionmemberphonefans15 Sep '06 - 16:52 
GeneralThanks but there is no group if listview is full dockmemberjvhow5 Sep '06 - 20:59 
GeneralRe: Thanks but there is no group if listview is full dockmemberKel_5 Sep '06 - 22:00 
GeneralI think its great.memberilovthecov29 Aug '06 - 1:16 
I think its great for you to share this work, I appreciate your effort and desire to let others learn... Have good fortune in all you do.
Thanks.
GeneralRe: I think its great.memberKel_29 Aug '06 - 1:24 
Questiontree diagram produced by ?memberBillWoodruff26 Aug '06 - 1:11 
AnswerRe: tree diagram produced by ?memberKel_26 Aug '06 - 5:51 
QuestionWhy this code ????memberDotNET7422 Aug '06 - 7:04 
AnswerRe: Why this code ????memberSteven Roebert22 Aug '06 - 11:07 
QuestionReturn of Groupable ListView ....?memberMahesh Sapre20 Aug '06 - 20:29 

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

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