I have the following piece of code which moves ListViewItem
‘s from one ListView
to another.
private void MoveListViewItems(ListView source, ListView target) { foreach (ListViewItem item in source.SelectedItems) { ListViewItem tmpItem = (ListViewItem)item.Clone(); source.Items.Remove(item); target.Items.Add(tmpItem); } }
This works fine, except the assignment of the group.
I’ve created the same group (let’s say “group_A”) in both ListView
‘s.
If I clone one ListViewItem
from the source-ListView
that resides in “group_A” and add it to the target-ListView
, then it won’t appear in the “group_A” of the target-ListView
.
So I’ve added an additional line of code which re-assigns the group to the ListViewItem
by using the group’s Name
-property and this also works fine:
private void MoveListViewItems(ListView source, ListView target) { foreach (ListViewItem item in source.SelectedItems) { ListViewItem tmpItem = (ListViewItem)item.Clone(); source.Items.Remove(item); target.Items.Add(tmpItem); // re-assign the group tmpItem.Group = target.Groups[tmpItem.Group.Name]; } }
I think I understand why this is happening, as the source-group-object isn’t the same as the target.
My question is: Am I doing this in a way that it was not intended to do?
Or isn’t there a more elegant solution?