Use ListView groups in C#


ListView groups

Many developers don’t realize that the ListView control can display items in groups. You can create ListView groups at either design or run time. Then you can add items to the groups. If you add an item without specifying a group, it is displayed in a special “Default” group.

This program uses the following code to create two groups at run time.

// Add some groups to the ListView.
ListViewGroup csharp_group = new ListViewGroup("C# Books");
ListViewGroup vb_group = new ListViewGroup("Visual Basic Books");
lvwBooks.Groups.Add(csharp_group);
lvwBooks.Groups.Add(vb_group);

It then adds several items to the groups. It also adds one item without a group. The following code shows how the program adds an item to the “C# Books” group.

// C# Books.
lvwBooks.Items.Add(new ListViewItem(new string[]
    {   "C# 5.0 Programmer's Reference",
        "http://www.wrox.com/WileyCDA/WroxTitle/C-5-0-" +
            "Programmer-s-Reference.productCd-1118847288.html", 
        "978-1-118-84728-2", 
        "960", "2014"},
    csharp_group));

The program uses similar code to add the other items.

Download the example to see additional details, such as how the program displays different ListView views (large icon, small icon, detail, and so forth).


Download Example   Follow me on Twitter   RSS feed




This entry was posted in controls, user interface and tagged , , , , , , , , , , , , , . Bookmark the permalink.

3 Responses to Use ListView groups in C#

  1. Pingback: Use ListView data to generate HTML TABLE code in C# -

Leave a Reply

Your email address will not be published. Required fields are marked *