views:

136

answers:

2

Hi,

I have several datagrid where I need to update the informations. Things is, since more than one person works on the system at the same time, the datagrid need to be refreshed on a regular basis. When I refresh, I lose the sorting that user had.

Is there a way to keep it?

thanks

+1  A: 

Note that this is untested, but could you do something like this?

ListCollectionView lcv = (ListCollectionView)CollectionViewSource.GetDefaultView(myDataGrid.ItemsSource);
IComparer mySort = lcv.CustomSort;  // assumes you've already set it beforehand
... // stuff happens
lcv.CustomSort = mySort;

I am still learning WPF myself, but hope this is some help... -Matt.

StrayPointer
To cover all your bases you may also want to copy across the SortDescriptions. That way you will retain sort order no matter how sorting is selected. Relevant code would be something like this: var sd = lcv.SortDescriptions.ToList(); [...] if(!lcv.SortDescriptions.SequenceEqual(sd) { lcv.SortDescriptions.ClearItems(); foreach(d in sd) lcv.SortDescriptions.Add(d); }
Ray Burns
+1  A: 

Just update the contents of the bound collection - don't replace the collection itself. Then you will not get a new CollectionView so your sorting won't be affected.

Ray Burns
I tried it but can't seem to make it work.I'll have to check that out some other times
David Brunelle