Search code examples
c#listviewgroupinglistviewitem

Add a ListViewItem to multiple groups


I'm trying to add all Items of a ListView to groups, so that an item can be in multiple groups. Items in the ListView correspond to a List where Something has a member called Genre which is a List so an item can have multiple genres. I want to make groups for every Genre present in the list and make all items be in their respective groups. I wanted to make a thing like that :

ListView1.Items.All(x => { x.Groups.Add("groupname"); x.Groups.Add("groupname 2"); return true; });

Is it possible or do I have to make my own ListView ?


Solution

  • Try this it work for me

    listView1.Columns.Add("Col1");
    listView1.Columns.Add("Col2");
    
    string[] strArrGroups = new string[3] { "FIRST", "SECOND", "THIRD" };
    string[] strArrItems = new string[4] { "uno", "dos", "twa", "quad" };
    for (int i = 0; i < strArrGroups.Length; i++)
     {
        int groupIndex = listView1.Groups.Add(new ListViewGroup(strArrGroups[i],HorizontalAlignment.Left));
    for (int j = 0; j < strArrItems.Length; j++)
    {
        ListViewItem lvi = new ListViewItem(strArrItems[j]);
        lvi.SubItems.Add("Hasta la Vista, Mon Cherri!");
        listView1.Items.Add(lvi);
        listView1.Groups[i].Items.Add(lvi);
    }
    

    }