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

  • Thread starter Thread starter forest demon
  • Start date Start date
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).
 
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.
 
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
 
Additional: have checked, and unless I missed it, ObservableCollection
does not appear to support sorting. Disappointing.

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

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