Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Groupable ListView

0.00/5 (No votes)
15 Sep 2006 2  
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.

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here