Question

I have a Silverlight 2.0 DataGrid that contains a list of items that needs to be refreshed in an interval to display the up-to-the-minute information. There are items in this grid that may be added and may be removed while the screen is being displayed to the current user. For Example:

If grid looks like the following, then after a minute the data needs to be refreshed because another user has added a 4th entry. (notice the sort is assending by Last Name)

Data Grid http://img16.imageshack.us/img16/1667/datagrid.jpg

Then when I get the new data set and set it to the datagrid's DataSource property, it resorts the array based on my first column like so:

Data Grid2 http://img19.imageshack.us/img19/1294/datagridb.jpg

Is there any way to reapply the sort after the data source has been updated? I'd like to save the fact that the data grid is sorted by last name, then update the data source, and then reapply the sort to the data grid. So, in the end the datagrid would look like the following:

Data Grid3 http://img13.imageshack.us/img13/4636/datagrid2.jpg

These screen shots are of course not a Silverlight data grid, but this is for simplicity in explaining the situation.

Was it helpful?

Solution 3

After researching how to do this, I have figured it out. I tried to implement what the others said to try, but could not get it to work in Silverlight 2.0. Thus, I searched StackOverflow and Google for more answers. ScottLogic and CodeProject seemed to be what I was looking for but I couldn't get them to work fully. So with a bit of frustration and playing around with code, I finally arrived on the solution.

If the collection that you're using for your DataGrid's ItemsSource is ObservableCollection(Of T), you can get the ability of sorting for the object, plus you can update the collection dynamically at runtime, causing the grid to refresh with the item you insert. I've created a Google Code project that is GPL for you to checkout and download. Please note that I'm using the Microsoft Silverlight 2.0 DataGrid December 2008 Release for this project. I have linked to this download on the project page as well. (The dll's required are referenced from within a lib folder inside the project).


Enjoy!


Silverlight 2.0 DataGrid Sort Project on Google Code

OTHER TIPS

Probably the best way to go about it is to use PagedCollectionViews, although I have not tested this out myself, these classes contain a property that stores the different sorting methods applied to them. I believe that you can probably grab the current sorting element from one collection, connect to the new collection, and apply the sorting to the new collection.

For a more in depth discussion on how this all works, you can follow the explanation here

I tried using PagedCollectionViews as well as CollectionViewSources (I used the new collection to overwrite the existing source collection).

My solution isn't the most elegant, but it works. When updating the source of the DataGrid I did my own change detection.

In other words, instead of blindly overwriting the source collection I iterated through the new collection, compared each item to the existing collection, and updated the existing items where possible.

Hope that helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top