By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,890 Members | 1,226 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,890 IT Pros & Developers. It's quick & easy.

How can I index an IList/Collection associated with a ListView, after the ListView has been sorted?

P: n/a
I have an IList/Collection that contains items in a ListView.

If i click on an item in the ListView, i can capture the index
(lv.SelectedItems[0].Index) and reference the correct item in the
associated IList/Collection .

Once I've sorted the ListView, obviously the indices do not match up
anymore. So, should i sort the IList/Collection when I sort the
ListView (which seems overkill to me) or some how sort the indices
associated with the ListView?

I'm buffaloed at this point.

Any input is appreciated. Thanks guys (and gals).

Apr 9 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
You should have a separate Dictionary which is keyed by the ListViewItem
to the object that you are visualizing in the list view. Then, you can
easily retrieve the item that the ListViewItem is referencing.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"forest demon" <me********@gmail.comwrote in message
news:11**********************@n59g2000hsh.googlegr oups.com...
>I have an IList/Collection that contains items in a ListView.

If i click on an item in the ListView, i can capture the index
(lv.SelectedItems[0].Index) and reference the correct item in the
associated IList/Collection .

Once I've sorted the ListView, obviously the indices do not match up
anymore. So, should i sort the IList/Collection when I sort the
ListView (which seems overkill to me) or some how sort the indices
associated with the ListView?

I'm buffaloed at this point.

Any input is appreciated. Thanks guys (and gals).

Apr 9 '07 #2

P: n/a
A different approach; I quite like observer-based patterns, so I use a
data-bound ListView subclass, and bind to a list that implements
IBindingList (SupportsSorting/SortProperty/SortDirection) and
IBindingListView (SupportsAdvancedSorting/ApplySort). I then ask the
list to sort itself, and let the UI manage itself. This can be quite a
handful from scratch, but from what I have seen so far the .Net 3.0
classes may offer some improved list classes (ObservableCollection etc
- haven't checked to see if it supports sorting though), and WPF
offers more data-bound UI options for displaying data.
Finally, if you are using the Details view (of ListView), consider
switching to DataGridView, as again this is data-bound without needing
to code anything.

Marc
Apr 9 '07 #3

P: n/a
Additional: have checked, and unless I missed it, ObservableCollection
does not appear to support sorting. Disappointing.

Marc

Apr 9 '07 #4

P: n/a
thanks for the input guys...what would i do without USENET input?

i ended up hashing it and i have it working as expected.

Apr 9 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.