Search code examples
c#listviewlistviewitem

Assign ListViewItem to a group?


{
    Profile p=new Profile("Name","Var1","Var2",True,False,110102);
    ...
    ...
    if(true)
    {
       var item = new ListViewItem{Text = p.Name, Tag = p};
       ListView1.Items.Add(item);
       ListView2.Items.Add((ListViewItem)item.Clone());
    }
}

I have 2 groups declaired in my ListView, I added them to the groups collection in visual studio's Designer. The groups are 'Expired Profiles' and 'Active Profiles' My question is how do I assign these Items into groups as I add to the ListView? I'd like all Profiles to go into the listview as automatically 'Active Profiles' Grouping


Solution

  • First, grab the group you need:

    var myGroup = ListView1.Groups["NameOfTheGroup"];
    

    then, assign it to the Group property when creating your ListViewItem:

    var item = new ListViewItem { Text = p.Name, Tag = p, Group = myGroup };