Click here to Skip to main content
15,442,754 members
Articles / Programming Languages / C#
Posted 20 Aug 2006


201 bookmarked

Groupable ListView

Rate me:
Please Sign up or sign in to vote.
4.72/5 (38 votes)
15 Sep 2006CPOL1 min read
Extended ListView with GUIs for easy grouping.

Sample Image - GroupableListView.png


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.


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.


  • 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:'.


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


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 });
        foreach (ListViewItem lvi in this.Items)
            string header = "";

            foreach (ColumnHeader ch in Headers)
                header += " " + 
            ListViewGroup group = 
                new ListViewGroup(header);
            ListViewGroup found = null;
            foreach (ListViewGroup g in Groups)
                if (g.Header == group.Header)
                { found = g; break; }
            if (found == null)

Sample Image - GroupableListView.png


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


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 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

PraiseGreate example Pin
PetrosPetrosean9-Jan-16 1:39
MemberPetrosPetrosean9-Jan-16 1:39 
QuestionBad Pin
Selçuk Dursun26-Apr-15 14:58
MemberSelçuk Dursun26-Apr-15 14:58 
GeneralVery nice extension! Pin
Sam Jacobs17-Mar-10 12:40
MemberSam Jacobs17-Mar-10 12:40 
Generalwrite text in Grouping Pin
Rohit Mahajan21-May-08 21:39
MemberRohit Mahajan21-May-08 21:39 
GeneralVista Style Group/Sort Pin
Alexandru Stanciu27-Feb-08 17:54
MemberAlexandru Stanciu27-Feb-08 17:54 
QuestionNo scroll bar Pin
Bill W15-Nov-07 4:29
MemberBill W15-Nov-07 4:29 
QuestionStrange Issue Pin
-_-_-_-_-_22-Aug-07 21:10
Member-_-_-_-_-_22-Aug-07 21:10 
AnswerRe: Strange Issue Pin
-_-_-_-_-_22-Aug-07 21:18
Member-_-_-_-_-_22-Aug-07 21:18 
QuestionWidth-Problem when ListView in LargeIcon-Mode Pin
Stephan Mantel17-Jun-07 3:21
MemberStephan Mantel17-Jun-07 3:21 
GeneralGrouping Sorted Pin
chancer10116-May-07 4:56
Memberchancer10116-May-07 4:56 
GeneralSeem a bug Pin
dvptUml3-Apr-07 16:05
MemberdvptUml3-Apr-07 16:05 
Sorry, i'm not a programmer, just a financial manager !! and i never wrote an article (not enough time to learn programming...).
But i think i've found that bug : when you set a group and then unset it (and nor category is set), the border line still appear.
May I suggest to add in the void tsb_Click(object sender, EventArgs e) method after HeaderGroup.Remove... the following:
if (HeaderGroup.Count == 0)
  return; // nothing to ask 'GroupBy'

I hope i'm not wrong since nobody told this ....

Not a programmer but DvptUml is my credo
Generaluse combobox instead of a toolstrip... [modified] Pin
TheCardinal17-Oct-06 21:04
MemberTheCardinal17-Oct-06 21:04 
QuestionExpand/Collapse Group Pin
User 28640483-Oct-06 22:06
MemberUser 28640483-Oct-06 22:06 
AnswerRe: Expand/Collapse Group Pin
sonofdaedalus24-Jan-07 8:23
Membersonofdaedalus24-Jan-07 8:23 
QuestionRe: Expand/Collapse Group Pin
Soxiz11-Jun-07 11:32
MemberSoxiz11-Jun-07 11:32 
QuestionA Question Pin
phonefans15-Sep-06 7:41
Memberphonefans15-Sep-06 7:41 
AnswerRe: A Question Pin
Kel_15-Sep-06 9:47
MemberKel_15-Sep-06 9:47 
GeneralRe: A Question Pin
phonefans15-Sep-06 16:52
Memberphonefans15-Sep-06 16:52 
GeneralThanks but there is no group if listview is full dock Pin
jvhow5-Sep-06 20:59
Memberjvhow5-Sep-06 20:59 
GeneralRe: Thanks but there is no group if listview is full dock Pin
Kel_5-Sep-06 22:00
MemberKel_5-Sep-06 22:00 
GeneralI think its great. Pin
ilovthecov29-Aug-06 1:16
Memberilovthecov29-Aug-06 1:16 
GeneralRe: I think its great. Pin
Kel_29-Aug-06 1:24
MemberKel_29-Aug-06 1:24 
Questiontree diagram produced by ? Pin
BillWoodruff26-Aug-06 1:11
mveBillWoodruff26-Aug-06 1:11 
AnswerRe: tree diagram produced by ? Pin
Kel_26-Aug-06 5:51
MemberKel_26-Aug-06 5:51 
QuestionWhy this code ???? Pin
DotNET7422-Aug-06 7:04
MemberDotNET7422-Aug-06 7:04 

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.