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

F

forest demon

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).
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
M

Marc Gravell

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
 
M

Marc Gravell

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

Marc
 
F

forest demon

thanks for the input guys...what would i do without USENET input?

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top