On Fri, 30 Sep 2005 13:52:02 -0700, "AMercer"
<AM*****@discus sions.microsoft .com> wrote:
The best performance I have gotten from a ListView is via
ListView.Items. AddRange(x)
where x is an array of ListViewItems. With the data volume you have,
I think the way to go is a worker thread that creates an array of
ListViewItem s, loads data into the array, and from time to time calls
AddRange and reinitializes. Be aware of the need to execute AddRange on gui
thread rather than on the worker thread.
I do agree that using AddRange is the fastest way. I will however
disagree about using a worker thread since the gain is minimal. In my
experience the majority of time will still be spent in the AddRange
method and not building the array.
Here is some modified sample code from one of my projects to use as a
base. Displaying 9600 items takes 1.5 seconds (which is acceptable for
me as a user of the program) on my Athlon XP 1700.
private void RefreshListView ()
{
listView.BeginU pdate();
listView.Items. Clear();
AddItemsToListV iew();
listView.EndUpd ate();
}
private void AddItemsToListV iew()
{
List<ListViewIt em> items = new List<ListViewIt em>();
foreach (Episode episode in filter.Execute( episodeList.Epi sodes))
items.Add(Creat eListViewItem(e pisode));
listView.Items. AddRange(items. ToArray());
}
private static ListViewItem CreateListViewI tem(Episode episode)
{
ListViewItem item = new ListViewItem(
new string[]
{
episode.Origina lAirdate.HasVal ue?
episode.Origina lAirdate.Value. ToShortDateStri ng():"",
episode.Show.Na me,
episode.Season. ToString(),
episode.Episode Number.ToString (),
episode.Title
});
item.ForeColor = EpisodeForeColo r(episode);
item.Tag = episode;
return item;
}
Even though I create lots of ListViewItems which contain a decent
amount of subitems(5). I still spend 90% of the time in the AddRange
method.
--
Marcus Andrén