I have a data set whose elements are displayed as rows in a DataGrid. The sort order for the rows changes in response to external events.
My initial thought was to store the rows as an ObservableCollection and resort the collection after updates. However I ran into two problems: 1) the ObservableCollection does not have a Sort() method 2) if I try to sort the elements myself, I get an exception whenever I try to assign an element to a new position, for example in a swap function like
class MyCollection : ObservableCollection<T> { void swap( int i, int j ) { T tmp = this[i]; this[i] = this[j]; // THROWS A NOT SUPPORTED EXCEPTION this[j] = tmp; } }
So the question is … how to populate a DataGrid whose row order needs to update dynamically?
I did finally get one answer working, I’ll describe it below.
I got this to work by implementing INotifyCollectionChanged explicitly (instead of using ObservableCollection). Furthermore, I found that using the Update action resulted in the same ‘not supported’ error, but that I could use the Add and Remove actions. So my swap function ends up looking like this:
The dynamic changes are fairly local, so fortunately using a slower handwritten sort in response to changes is working OK for me. In other words, when updates arrive, I invoke another member function (in the same collection) that looks something like this:
Don’t forget that it’s also necessary to fire an ‘Add’ notification when adding elements to the collection! I sort the initial list and then add in sorted order, which lets me use a more efficient library sort when I first populate the data.