Sorting not working with CollectionViewSource

Hi,

I'm using SfDataGrid binded to CollectionViewSource defined in xaml:
                                  Source="{Binding List}"
                                  IsLiveSortingRequested="True"
                                  IsLiveFilteringRequested="True"
                                  Filter="CollectionViewSource_Filter">
               
                    IsCurrent
                    DurationSeconds
               
               
                   
                   
               
           

Binded to something like this:
           
                                SelectedItem="{Binding SelectedAggregatedBreakdown, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                Visibility="{Binding IsAggregatedGridVisible, Converter={StaticResource BoolToVisibilityConverter}}"
                                HeaderStyle="{StaticResource HeaderStyle}"
                                RowStyle="{DynamicResource SfDataGridVirtualizingCellsControlStyle}"
                                CellStyle="{StaticResource CellStyle}"
                                FontSize="22"
                                SelectionUnit="Row"
                                CurrentCellBorderThickness="0"
                                ScrollMode="None"
                                Background="Transparent"
                                HeaderRowHeight="40"
                                RowHeight="30"
                                IsReadOnly="True"
                                AllowResizingColumns="False"
                                AllowSorting="True"
                                AllowFiltering="False"
                                SelectionMode="Single"
                                AutoGenerateColumns="False"
                                EnableDataVirtualization="True"
                                ShowRowHeader="False"
                                ExpanderColumnWidth="0"
                                ScrollViewer.VerticalScrollBarVisibility="Visible"
                                ScrollViewer.HorizontalScrollBarVisibility="Disabled">

I switched from default .Net DataGrid due to performance issues, but sorting was working. Now with SfDataGrid performance is good but sorting is not working :) can you please suggest what I need to set to get sorting working? It is not working even when I set AllowSorting="True" for columns. I also tested LiveDataUpdateMode="AllowDataShaping" - without any effect.

I tested to set CollectionViewSource and CollectionView in ViewModel but no success. Still sorting was not working :/

Note: I don't want to sort manually by clicking on column header also I don't want to have sort "markers" in headers. It should be automatic and it will never be changed by user.

Thanks

5 Replies

PV Peter Verbo September 13, 2018 12:41 PM UTC

Something strange happening in first post with pasted code lines. I hope here it will be complete:

I'm using SfDataGrid binded to CollectionViewSource defined in xaml:
<CollectionViewSource x:Key="Items"
                                  Source="{Binding List}"
                                  IsLiveSortingRequested="True"
                                  IsLiveFilteringRequested="True"
                                  Filter="CollectionViewSource_Filter">
                <CollectionViewSource.LiveSortingProperties>
                    <clr:String>IsCurrent</clr:String>
                    <clr:String>DurationSeconds</clr:String>
                </CollectionViewSource.LiveSortingProperties>
                <CollectionViewSource.SortDescriptions>
                    <scm:SortDescription PropertyName="IsCurrent" Direction="Descending" />
                    <scm:SortDescription PropertyName="DurationSeconds" Direction="Descending" />
                </CollectionViewSource.SortDescriptions>
            </CollectionViewSource>

Binded to something like this:
            <local:MySfDataGrid ItemsSource="{Binding Source={StaticResource Items}, IsAsync=True}"
                                SelectedItem="{Binding SelectedAggregatedBreakdown, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                Visibility="{Binding IsAggregatedGridVisible, Converter={StaticResource BoolToVisibilityConverter}}"
                                HeaderStyle="{StaticResource HeaderStyle}"
                                RowStyle="{DynamicResource SfDataGridVirtualizingCellsControlStyle}"
                                CellStyle="{StaticResource CellStyle}"
                                FontSize="22"
                                SelectionUnit="Row"
                                CurrentCellBorderThickness="0"
                                ScrollMode="None"
                                Background="Transparent"
                                HeaderRowHeight="40"
                                RowHeight="30"
                                IsReadOnly="True"
                                AllowResizingColumns="False"
                                AllowSorting="True"
                                AllowFiltering="False"
                                SelectionMode="Single"
                                AutoGenerateColumns="False"
                                EnableDataVirtualization="True"
                                ShowRowHeader="False"
                                ExpanderColumnWidth="0"
                                ScrollViewer.VerticalScrollBarVisibility="Visible"
                                ScrollViewer.HorizontalScrollBarVisibility="Disabled">




PV Peter Verbo September 14, 2018 10:56 AM UTC

If it is not possible to do by CollectionViewSource, could you please write me how to sort by 2 columns at same time? And always even when new Item is added. In MVVM.

I tested something like this:
                <syncfusion:SfDataGrid.SortColumnDescriptions>
                    <syncfusion:SortColumnDescription ColumnName="IsCurrent" SortDirection="Descending" />
                    <syncfusion:SortColumnDescription ColumnName="Datetime" SortDirection="Descending" />
                </syncfusion:SfDataGrid.SortColumnDescriptions>

                    <syncfusion:GridTextColumn Width="0"
                                               HeaderText="Current"
                                               MappingName="IsCurrent"
                                               IsReadOnly="True"
                                               AllowSorting="True"
                                               IsHidden="True">
                    </syncfusion:GridTextColumn>

                    <syncfusion:GridDateTimeColumn Width="66"
                                                   HeaderText="Čas"
                                                   MappingName="Datetime"
                                                   Pattern="CustomPattern"
                                                   CustomPattern="HH:mm"
                                                   AllowSorting="False"
                                                   TextAlignment="Center">
                    </syncfusion:GridDateTimeColumn>

But it is not working whenever I add Item to collection then new Item is added at the end of all items instead of first place. "IsCurrent" is boolean and I want that current item will be always on first place even if its datetime will be not the newest. I tried to set sorting number on columns and it seemed correct (IsCurrent was 1 and DateTime was 2) but sorting was not correct.


DY Deivaselvan Y Syncfusion Team September 17, 2018 03:58 AM UTC

Hi Peter,

Thank you for contacting Syncfusion support.

We have analyzed your query, and by default CollectionViewSource based sorting will not working in SfDataGrid. But you can achieve the sorting in SfDatagrid by defining the SortColumnDescription. Please refer the below link to define the SortColumnDescription. 

https://help.syncfusion.com/wpf/sfdatagrid/sorting#programmatic-sorting 
 
https://help.syncfusion.com/wpf/sfdatagrid/sorting

We have achieved your requirement with the simple sample to perform the sorting operation on initial loading of SfDataGrid. And we hide the Sort Icon in the header to restrict the user perform the sorting in UI.

 
<syncfusion:SfDataGrid x:Name="datagrid" Grid.Row="0"  
                        AllowEditing="True"  
                        AllowFiltering="True"  
                        AllowSorting="False"  
                        AllowGrouping="True"  
                        <!--To hide the Sort ICon-->  
                        HeaderStyle="{StaticResource GridHeaderCellControlStyle1}"  
                        AutoGenerateColumns="False"  
                        ItemsSource="{Binding GDCSource}"  
                        ShowGroupDropArea="True">  
    <syncfusion:SfDataGrid.SortColumnDescriptions>  
        <syncfusion:SortColumnDescription ColumnName="EmployeeName"SortDirection="Descending"/>  
        <syncfusion:SortColumnDescription ColumnName="EmployeeSalary"SortDirection="Descending" />  
    </syncfusion:SfDataGrid.SortColumnDescriptions>  
    <syncfusion:SfDataGrid.Columns>  
        <syncfusion:GridTextColumn MappingName="EmployeeName" />  
        <syncfusion:GridTextColumn MappingName="EmployeeAge" />  
    </syncfusion:SfDataGrid.Columns>  
</syncfusion:SfDataGrid>  
  

Please download the sample from the below location. 
 
WPF-174899908-27786323.zip 

And in the last update you have add the column as “DateTime” in SortColumnDescription, at the same time you set AllowSorting false for DateTime column definition so that only sorting is not working for second column. Please modify your codes same as below, 
 
<syncfusion:SfDataGrid.SortColumnDescriptions>  
                    <syncfusion:SortColumnDescription ColumnName="IsCurrent" SortDirection="Descending" />  
                    <syncfusion:SortColumnDescription ColumnName="Datetime" SortDirection="Descending" />  
                </syncfusion:SfDataGrid.SortColumnDescriptions>  
  
                    <syncfusion:GridTextColumn Width="0"  
                                               HeaderText="Current"  
                                               MappingName="IsCurrent"  
                                               IsReadOnly="True"  
                                               AllowSorting="True"  
                                               IsHidden="True">  
                    </syncfusion:GridTextColumn>  
  
                    <syncfusion:GridDateTimeColumn Width="66"  
                                                   HeaderText="Čas"  
                                                   MappingName="Datetime"  
                                                   Pattern="CustomPattern"  
                                                   CustomPattern="HH:mm"  
                                                   AllowSorting="True"  
                                                   TextAlignment="Center">  
                    </syncfusion:GridDateTimeColumn>  
  
  
And you can perform the sorting at runtime by enable the LiveDataupdateMode asAllowDataShapping 
  
Please let us know if you have any queries.

Regards, 
 
Deivaselvan 



PV Peter Verbo September 17, 2018 05:37 PM UTC

Hi Deivaselvan,

yes, during weekend I found same solution and I'm happy that you suggested me same solution so I think it should be correct now. 

But one thing: I don't want allow users to change sort direction of particular column (DateTime e.g.). Therefore I've set AllowSorting="False" for it. It seems that sorting is working as declared in SortColumnDescription and user is not allowed to change it. I hope it is correct.

And LiveDataUpdateMode="AllowDataShaping" was magic setting :)

So thank you very much for your support!

Best regards,
Peter


DY Deivaselvan Y Syncfusion Team September 19, 2018 03:59 AM UTC

Hi Peter,

Thank you for the update. We are happy to hear that you have meet your requirement using SortColumnDescription of SfDataGrid control. Please let us know if you need any further assistance.

Regards,
Deivaselvan 


Loader.
Up arrow icon