Reputation: 22759
is it possible to get the text values of specific column in a listview in form of array without using a loop function? lets say I have a listview contains 2 columns and 5000 records. what I want is to get all the texts under column 2 only but without using a loop. I know I can accomplish this with a loop but it takes ages if I had 5000+ records..
any ideas?
Upvotes: 2
Views: 2733
Reputation: 34240
I feel your pain and I've personally experienced how slow a ListViewItemsCollection
can be.
You can access the second column of a ListView
without a for loop like this:
string[] column1 = this.listView1.Items.Cast<ListViewItem>().Select(item => item.SubItems[1].Text).ToArray();
but this isn't any faster than a for loop. In fact it's a little bit slower! Other tricks like trying to use ListViewItemsCollection.CopyTo
also fail miserably.
A major problem with the ListView
is that is pathologically slow when used as a data structure. The property ListView.Items
looks like a data structure and you can use it like a data structure but any way you slice it:
So if you follow alexD's advice you will find that a real data structure will outperform a ListViewItemCollection
by orders of magnitude. The moral of the story is if you want to query the data in a ListViewItemCollection
fast, you have no alternative than to keep a fast copy outside of the ListView
. Practically any data structure will do, e.g.
string[][]
string[,]
List<List<string>>
List<string[]>
or evenList<Tuple<string, string>>
.These can all store the same data as a two-column ListView
and will be instantaneously fast by comparison to using the ListView
as a data structure. It is inconvenient but that is the price we must pay for speed.
Upvotes: 2
Reputation: 2364
Don't read the data directly from the text file into the list view...store it in a data structure like an ArrayList
which will maintain the order in which you insert elements.
Upvotes: 0