On Fri, 30 Sep 2005 13:52:02 -0700, "AMercer"
<AM*****@discussions.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
ListViewItems, 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.BeginUpdate();
listView.Items.Clear();
AddItemsToListView();
listView.EndUpdate();
}
private void AddItemsToListView()
{
List<ListViewItem> items = new List<ListViewItem>();
foreach (Episode episode in filter.Execute(episodeList.Episodes))
items.Add(CreateListViewItem(episode));
listView.Items.AddRange(items.ToArray());
}
private static ListViewItem CreateListViewItem(Episode episode)
{
ListViewItem item = new ListViewItem(
new string[]
{
episode.OriginalAirdate.HasValue?
episode.OriginalAirdate.Value.ToShortDateString(): "",
episode.Show.Name,
episode.Season.ToString(),
episode.EpisodeNumber.ToString(),
episode.Title
});
item.ForeColor = EpisodeForeColor(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