Skip to main content
Email Password   helpLost your password?

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

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

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
Generalwrite text in Grouping Pin
Rohit Mahajan
22:39 21 May '08  
GeneralVista Style Group/Sort Pin
Alexandru Stanciu
18:54 27 Feb '08  
QuestionNo scroll bar Pin
Bill W
5:29 15 Nov '07  
QuestionStrange Issue Pin
-_-_-_-_-_
22:10 22 Aug '07  
AnswerRe: Strange Issue Pin
-_-_-_-_-_
22:18 22 Aug '07  
QuestionWidth-Problem when ListView in LargeIcon-Mode Pin
Stephan Mantel
4:21 17 Jun '07  
GeneralGrouping Sorted Pin
chancer101
5:56 16 May '07  
GeneralSeem a bug Pin
dvptUml
17:05 3 Apr '07  
Generaluse combobox instead of a toolstrip... [modified] Pin
TheCardinal
22:04 17 Oct '06  
QuestionExpand/Collapse Group Pin
riz332
23:06 3 Oct '06  
AnswerRe: Expand/Collapse Group Pin
sonofdaedalus
9:23 24 Jan '07  
QuestionRe: Expand/Collapse Group Pin
Soxiz
12:32 11 Jun '07  
QuestionA Question Pin
phonefans
8:41 15 Sep '06  
AnswerRe: A Question Pin
Kel_
10:47 15 Sep '06  
GeneralRe: A Question Pin
phonefans
17:52 15 Sep '06  
GeneralThanks but there is no group if listview is full dock Pin
jvhow
21:59 5 Sep '06  
GeneralRe: Thanks but there is no group if listview is full dock Pin
Kel_
23:00 5 Sep '06  
GeneralI think its great. Pin
ilovthecov
2:16 29 Aug '06  
GeneralRe: I think its great. Pin
Kel_
2:24 29 Aug '06  
Generaltree diagram produced by ? Pin
BillWoodruff
2:11 26 Aug '06  
GeneralRe: tree diagram produced by ? Pin
Kel_
6:51 26 Aug '06  
GeneralWhy this code ???? Pin
DotNET74
8:04 22 Aug '06  
GeneralRe: Why this code ???? Pin
Steven Roebert
12:07 22 Aug '06  
GeneralReturn of Groupable ListView ....? Pin
Mahesh Sapre
21:29 20 Aug '06  
GeneralRe: Return of Groupable ListView ....? Pin
davepermen
22:01 20 Aug '06  


Last Updated 15 Sep 2006 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009