D
Danny Tuppeny
Hi All,
I'm trying to find the best way to select an item in a ListView using the
Virtual mode. My "items" are business classes, and I have a reference to
once I want to select (passed from another dialog). One way is to spin
through the items and compare the .Tag property to my business object (code
below), however I fear the casts are probably costly (there could be a lot
of servers). I also thought about spinning through the servers list and
comparing directly, then using the index to set the selected item, however
to use foreach(), I don't have the index number, and I'm not sure of the
performance of a for(int i...) loop in comparison to a foreach. My code
currently works, but I'd like to make sure this is as fast as possible
(it'll be in many places, with very big lists soon!)
List<Server> servers = Server.GetAll();
listServers.VirtualListSize = servers.Count;
if (server != null)
{
foreach(ListViewItem lvi in listServers.Items)
{
if (server == (Server)lvi.Tag)
{
lvi.Selected = true;
break;
}
}
}
I'm trying to find the best way to select an item in a ListView using the
Virtual mode. My "items" are business classes, and I have a reference to
once I want to select (passed from another dialog). One way is to spin
through the items and compare the .Tag property to my business object (code
below), however I fear the casts are probably costly (there could be a lot
of servers). I also thought about spinning through the servers list and
comparing directly, then using the index to set the selected item, however
to use foreach(), I don't have the index number, and I'm not sure of the
performance of a for(int i...) loop in comparison to a foreach. My code
currently works, but I'd like to make sure this is as fast as possible
(it'll be in many places, with very big lists soon!)
List<Server> servers = Server.GetAll();
listServers.VirtualListSize = servers.Count;
if (server != null)
{
foreach(ListViewItem lvi in listServers.Items)
{
if (server == (Server)lvi.Tag)
{
lvi.Selected = true;
break;
}
}
}