ListView & Copying of

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I have a windows form, in C#. It has a ListView on it. For a couple of
reasons I now need to be have multiple sets of Data that can be dynamically
Copied into and out of the ListView. But I'm having realy trouble with
it....and feeling pretty frustrated.

I firstly tried copying over listView.Items - which is ok to read out of,
but its a read only property and can be copied back over.

Also tried listView.Items.CopyTo (lviArray) and tried a
listView2.Items.AddRange (lviArray), but I get an exception about data
already existing else where either remove it first or clone it. Tried
cloning it but that doesn't seem to work either.


Feel I am missing something. Any help would be appreciated.

Thanks,
Laura
 
A ListView.ListViewItemCollection that contains all the items in the ListView
control.

Why not create a few of these Collections and assign them at runtime?

You could clear() the ListView items first, then use a foreach loop to
iterate through the substitute ListViewItemCollection calling the
ListView.Items.Add() method for each item to be placed into the ListView.
 
Why not holding multiple listviews in memory? You only show one at a time
and you can then simply exchange them which is extremely fast.
 
John said:
A ListView.ListViewItemCollection that contains all the items in the ListView
control.

Why not create a few of these Collections and assign them at runtime?

This doesn't work, unless I'm using it wrongly. When you construct a
ListViewItemCollection is takes a single parameter of a ListView, which is
its owner.

Basically it then turns out that the ListViewItemCollection is then just a
reference to the ListView.Items data and any operation done on one is
reflected on the other.
You could clear() the ListView items first, then use a foreach loop to
iterate through the substitute ListViewItemCollection calling the
ListView.Items.Add() method for each item to be placed into the ListView.

So Clear ( ) on the ListView results in the clear of the
ListViewItemCollection.

I think I will have to duplicate the ListView objects and swap then in and
out at runtime as Cody suggested. But this is a pretty poor solution - I
thought I just wanted to do something simple with the data!

Thanks for the help, good to know I wasn't missing anything obvious.
 
Back
Top