1
votes

Does anyone know how I can sort a Datagrid when the Itemsource of the grid changes? i.e. if I have a DataGrid of products, and another grid of product versions, when I select a product, the ItemSource of the product version DataGrid will change so I would like to do a sort before the data appears inside the Product Versions DataGrid.

The way I do the sorting at the moment is.

<CollectionViewSource x:Key="cvsProductVersions" 
     Source="{Product.ProductVersions, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
     Filter="CollectionViewSource_Filter">
     <!--<CollectionViewSource.SortDescriptions>
        <scm:SortDescription PropertyName="Build1"  Direction="Descending"/>
     </CollectionViewSource.SortDescriptions>-->
  </CollectionViewSource>

I think I need to use

CollectionViewSource.GetDefaultView(dgProductVersion.ItemsSource).Refresh();

However, I need an event which basically means "Run this code once a new itemsource is applied to the table.

Cheers

1
How do you set ItemSource of version DataGrid? Can you show XAML of this DataGrid?shfire

1 Answers

1
votes

At some stage, I'm guessing that you set a collection as the ItemsSource property value for your CollectionViewSource. Why don't you just sort it using LinQ before you assign it to the CollectionViewSource?:

SomeCollection = new ObservableCollection(SomeCollection.OrderBy(i => i.Build));

You can refresh a CollectionViewSource directly, but it isn't generally what users want because it does a complete refresh... you could try something like this:

(dgProductVersion.ItemsSource as ListCollectionView).Refresh();

A third option can be found in the ItemsControl: 'E' is for Editable Collection on the Dr. WPF website.