Search code examples
c#listviewitem

add a ListViewItem to two or more ListView Items Collection?


List<Profile> listProf = new List<Profile>();
...
...
foreach (Profile p in listProf)
{
   ListViewItem Item = new ListViewItem();
   Item.Text = p.Name;
   Item.Tag = p;
   ListView1.Items.Add(Item);
   ListView2.Items.Add(Item);
}

In this instance how would I get this Item into both ListViews? I just get the error that I need to clone it. how can I do this? I'm not quite sure even the reason why a ListView would want to be so picky either.

How can I add an item to more than one ListViewCollection?


Solution

  • Try this:

    foreach(var p in listProf)
    {
        var item = new ListViewItem{Text = p.Name, Tag = p};
        ListView1.Items.Add(item);
        ListView2.Items.Add((ListViewItem)item.Clone());
    }