WinapiZone.net




Tutorials > WinAPI > Listview > Groups

How to use groups in a listview

Remember that the listview grouping feature is available only under Windows XP and higher.


The first time i saw that the MSDN docs weren't good enough was when i tried to use the "group in listview" feature in one of my softwares. It seemed easy and well documented (although without any example) but when i started using it, it didn't work at all. That's why i'm making this tutorial: but, of course, i've managed to do it! But let's start from the beginning.


How to add a group

Before you add a group, you must meet these requirements: Now, before adding a group, you must enable the group view mode by sending LVM_ENABLEGROUPVIEW or by calling the ListView_EnableGroupView macro. It's somewhat like switching between list and details view.

We're ready now. Add a group is easy, the same as adding a listview item: you just need to fill a structure (the LVGROUP structure) and pass it to the listview by sending the LVM_INSERTGROUP message or by calling the ListView_InsertGroup macro. A group should have an ID, that is necessary to add items to it and can be used to retrieve and set informations about a group (you can't do it through index). Here's an example:
LVGROUP lg = {0};
lg.cbSize = sizeof(LVGROUP);

//we specify the text and the id of the group
lg.mask = LVGF_HEADER|LVGF_GROUPID;
//The group text must be an Unicode string (you must start it with L)
lg.pszHeader = L"First group";
//This is the id you will use to add items to this group
lg.iGroupId = 101;

//Let's add it at the end of the list by using -1
ListView_InsertGroup(listView, -1, &lg);

The comments should explain everything. Now, if you try and compile, you'll see no group. Why? Well, because a group will not be visible until you add items to it. This is at the same time a good feature and a bad feature. It's a good feature because you may insert a lot of groups, but it's not sure you will fill all of them (for example, what if each group is a letter?): you'll agree that empty groups should be hidden. But, it's a bad feature because, especially in the beginning, how do you know that you're doing everything right? You'll see this when we will try to add items to a group.
As of now, i assure you have correctly added a group.

How to add items to a group

Here comes the problematic part.
You should already know how to add an item to a listview: through the LVITEM structure and the LVM_INSERTITEM message or the ListView_InsertItem macro. Looking to the LVITEM structure documentation, the only thing that seems related to groups is the member variable iGroupID and the LVIF_GROUPID flag for the mask variable.
Let's set it then:
LVITEM li = {0};

li.mask = LVIF_TEXT|LVIF_GROUPID;
li.iItem = 0;
li.iSubItem = 0;
li.pszText = _T("I should be inside a group");
//Let's fill the new variable with the group id we used before
li.iGroupId = 101;
ListView_InsertItem(listView, &li);

Compile and run. Still no group visible? Now you see what was the problem before. I went looking to the code to insert a group and tried different solutions, but the problem was not there.

The solution, although i don't quite understand it (!), is to add the LVIF_COLUMNS flag to the mask flags, without even filling the associated member variable, cColumns. This will bring up the group and all its items.
So the "correct" code (well, the WORKING code) to add an item to a group is:
LVITEM li = {0};

li.mask = LVIF_TEXT|LVIF_GROUPID|LVIF_COLUMNS;
li.iItem = 0;
li.iSubItem = 0;
li.pszText = _T("I AM inside a group");
//Let's fill the new variable with the group id we used before
li.iGroupId = 101;
ListView_InsertItem(listView, &li);

We're done!



Tada!
You should also know that, like a group is hidden when it has no items, an item is hidden when it has no group.

See the example sources for a complete example with more than one group and item.







Comments

JugglerRick wrote:

Thank you, you just saved me a flat spot on my head.
(22.04.2009, 20:18)

jean wrote:

Hi
Can a group contain sub groups ?
(31.12.2008, 14:51)

Eoin wrote:

Thank you, thank you, thank you! That had been driving me crazy.
(21.10.2008, 00:02)

thewasp wrote:

Finaly! I couldn't find any reasonable explanation, why those f*cking groups are invisible. Thanks a lot!
(30.06.2008, 12:12)

Your comment:

Name:
E-mail or homepage:
Captcha:
captcha

Script by Alex



This page was last modified 34 months, 2 weeks, 4 days and 21 hours ago