5,423,696 members and growing! (16,486 online)
Email Password   helpLost your password?
Desktop Development » List Controls » ListView controls     Intermediate License: The Code Project Open License (CPOL)

Groupable ListView

By Kel_

Extended ListView with GUIs for easy grouping.
C# 2.0, C#, Windows, .NET, .NET 2.0VS2005, Visual Studio, Dev

Posted: 20 Aug 2006
Updated: 15 Sep 2006
Views: 66,740
Bookmarked: 123 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
32 votes for this Article.
Popularity: 6.79 Rating: 4.51 out of 5
1 vote, 3.1%
1
0 votes, 0.0%
2
1 vote, 3.1%
3
7 votes, 21.9%
4
23 votes, 71.9%
5

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_


Kel, passionate by 3D Graphics and gaming developement still his young age. Now a professional 3D Software Developer & Architect, worked for a flight simulator projects and collaborated to a 3D Engine developement projects.
Started programming at age of 14, on vb, then moved to asp and C# passing by C, C++.
Open Minded, likes huge concepts and ideas, AI, database and game developement.
Hobbies: Mangas & Animes, Playing and getting out with friends.
Occupation: Software Developer (Senior)
Company: Flying-Cam S.A.
Location: Belgium Belgium

Other popular List Controls articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 38 (Total in Forum: 38) (Refresh)FirstPrevNext
Subject  Author Date 
Generalwrite text in GroupingmemberRohit Mahajan22:39 21 May '08  
GeneralVista Style Group/SortmemberAlexandru Stanciu18:54 27 Feb '08  
QuestionNo scroll barmemberBill W5:29 15 Nov '07  
QuestionStrange Issuemember-_-_-_-_-_22:10 22 Aug '07  
AnswerRe: Strange Issuemember-_-_-_-_-_22:18 22 Aug '07  
QuestionWidth-Problem when ListView in LargeIcon-ModememberStephan Mantel4:21 17 Jun '07  
GeneralGrouping Sortedmemberchancer1015:56 16 May '07  
GeneralSeem a bugmemberdvptUml17:05 3 Apr '07  
Generaluse combobox instead of a toolstrip... [modified]memberTheCardinal22:04 17 Oct '06  
QuestionExpand/Collapse Groupmemberriz33223:06 3 Oct '06  
AnswerRe: Expand/Collapse Groupmembersonofdaedalus9:23 24 Jan '07  
QuestionRe: Expand/Collapse GroupmemberSoxiz12:32 11 Jun '07  
QuestionA Questionmemberphonefans8:41 15 Sep '06  
AnswerRe: A QuestionmemberKel_10:47 15 Sep '06  
GeneralRe: A Questionmemberphonefans17:52 15 Sep '06  
GeneralThanks but there is no group if listview is full dockmemberjvhow21:59 5 Sep '06  
GeneralRe: Thanks but there is no group if listview is full dockmemberKel_23:00 5 Sep '06  
GeneralI think its great.memberilovthecov2:16 29 Aug '06  
GeneralRe: I think its great.memberKel_2:24 29 Aug '06  
Generaltree diagram produced by ?memberBillWoodruff2:11 26 Aug '06  
GeneralRe: tree diagram produced by ?memberKel_6:51 26 Aug '06  
GeneralWhy this code ????memberDotNET748:04 22 Aug '06  
GeneralRe: Why this code ????memberSteven Roebert12:07 22 Aug '06  
GeneralReturn of Groupable ListView ....?memberMahesh Sapre21:29 20 Aug '06  
GeneralRe: Return of Groupable ListView ....?memberdavepermen22:01 20 Aug '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 15 Sep 2006
Editor: Smitha Vijayan
Copyright 2006 by Kel_
Everything else Copyright © CodeProject, 1999-2008
Web10 | Advertise on the Code Project