Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Empty ListViewGroup is not shown in ListView

I could not find any remarks on MSDN ListView.Groups Property that empty ListViewGroup will be hidden. Is it by design, or I am missing something? My sample code below will show only "group 2" with item "item1".

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
      Handles MyBase.Load
    '
    Dim gr = New ListViewGroup("group 1")
    ListView1.Groups.Add(gr)
    '
    Dim gr2 = New ListViewGroup("group 2")
    ListView1.Groups.Add(gr2)
    '
    Dim lvi As New ListViewItem("item1")
    ListView1.Items.Add(lvi)
    '
    gr2.Items.Add(lvi)
End Sub

Updated: Is there are any way to show ListViewGroup without adding dummy item

For now the only workaround idea I have is to use collapsible listview (Vista and up)

like image 645
walter Avatar asked Mar 05 '12 03:03

walter


1 Answers

this method makes sure the ListViewGroup shows

void MakeSureListViewGroupHeaderShows(ListView lv)
{
    foreach (ListViewGroup lvg in lv.Groups)
    {
        if (lvg.Items.Count == 0)
        {
            // add empty list view item
            ListViewItem lvi = new ListViewItem(string.Empty);
            lvi.Group = lvg;
            lv.Items.Add(lvi);
        }
        else
        {
            // remove our dummy list view item
            foreach (ListViewItem lvi in lvg.Items)
            {
                if (lvi.Text == string.Empty)
                {
                    lvi.Remove();
                }
            }
        }
    }
}
like image 74
Jayson Ragasa Avatar answered Sep 24 '22 00:09

Jayson Ragasa