ListView hide or collapse selected group

sczdavos picture sczdavos · Sep 16, 2012 · Viewed 22.4k times · Source

How can I hide or collapse some group in ListView?

I just add some items

contactListView.Items.Add(new ISIMlistViewItem(contact));
if (contact.availability == 6)
    contactListView.Items[contact.identificator].Group = contactListView.Groups["offlineGroup"];
else
    contactListView.Items[contact.identificator].Group = contactListView.Groups["onlineGroup"];

And I want to sometimes hide the offlineGroup.

if (hideOffline == true)
{
    // something like
    contactListView.Groups["offlineGroup"].Hide();
    // or
    contactListView.Groups["offlineGroup"].Visible = false;
}

But I don't know how can I do that. Can I just collapse it and don't draw it or is there any possibility to hide it?

Answer

Uwe Keim picture Uwe Keim · Sep 16, 2012

It seems that the .NET version of the ListViewGroup class does not provide a Collapse or Expand method.

Luckily, the native ListView control does support it and one guy provided an extension to enable expand and collapse.

Using his code you can then have a function to set the expand/collapse state with:

private void SetGroupCollapse(GroupState state)

For hiding a complete group I would simply remove all the items in this group.