We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

SfDataGrid programatic ordering does not order data

Hi

I am trying to order data by DateTime in a DataGrid as a default ordering when the control loads. I have tried the XAML and C# approaches from the documentation (https://help.syncfusion.com/uwp/sfdatagrid/sorting?cs-save-lang=1&cs-lang=xaml#programmatic-sorting) but they did not work. 

The result is that I can see the column header with a chevron (as it were sorted) and when I open the filter window I can see ordering from Z to A as active, but the data is not sorted. When I click the chevron twice the data is sorted in the order that is required.

I get my data from a SQLite database and it is loaded in the view model when the page is navigated to. The DataGrid binds to an observable collection that is based on the database list.

Is there a step that I am missing in order to get the data sorted when the collection binding updates?

Thanks

PH

9 Replies

BR Balamurugan Rajaraman Syncfusion Team April 12, 2017 10:22 AM UTC

Hi Piotr Hosa 

Thank you for contact Syncfusion support. 

We have analyzed your query “Programmatic sorting doesn’t take place correctly” in our latest Syncfusion product version 15.1.0.41. We are not able to reproduce the reported issue, its working fine in our side. We have attached the tested sample for your reference. Could you please share your product version or revert us with modifying the attached sample to reproduce the reported issue. It will helpful for us to analyze further. 

You can able to download the sample from the below location 


Regards, 
Balamurugan R  



PV Peter Verbo December 6, 2017 08:10 PM UTC

It is reproducible (I have same problem - I'm not using sql). Problem is when collection is updated (eg. we add some new item to collection) - then dataGrid is not refreshed and user needs to click twice on column header to re-sort all items again.

I'm working with sfDataGrid with binded ObservableCollection and whenever I add new item then this new item is added at last row and sorting is not updated. Is it possible to force dataGrid to update or to follow sorting during adding new items?


GT Gnanasownthari Thirugnanam Syncfusion Team December 7, 2017 08:57 AM UTC

Hi Peter, 

By Default in SfDataGrid view is not updated with sorting order while add the record at runtime. You can add the record at run time with sorting order by setting LiveDataUpdateMode as AllowDataShaping like below code example. 

XAML 
<syncfusion:SfDataGrid Name="sfDataGrid" 
                               AutoGenerateColumns="False"   
                               LiveDataUpdateMode="AllowDataShaping" 
                               ItemsSource="{Binding Orders}"> 


We have prepared the simple sample based on your given requirement, you can download it from below mentioned location. 


Please refer the below UG link for more details about AllowDataShaping. 

Regards, 
Gnanasownthari T. 



PV Peter Verbo December 7, 2017 09:42 AM UTC

GREAT! This solved my really ugly workaround. Thanks! :)


GT Gnanasownthari Thirugnanam Syncfusion Team December 7, 2017 09:53 AM UTC

Hi Peter, 

Thank you for your update. 

Please let us know if you need further assistance on this. 

Regards, 
Gnanasownthari T. 
 



PV Peter Verbo December 7, 2017 10:32 AM UTC

Maybe I have: I'm working on use case when user should be able to copy to clipboard data displayed in table. I'm doing this by myself, and I'm working with my source collection which is not sorted of course. So now I need to copy sorted data.

What I have so far is that I'm trying to use dataGrid.SortColumnsChanged event but it seems that when I try to get dataGrid.View.Records then in this moment records are not sorted yet. IS there another event which should I observe or maybe I'm doing something wrong.

My code:

            this.dataGridCommands.SortColumnsChanged += DataGridCommands_SortColumnsChanged;

private void DataGridCommands_SortColumnsChanged(object sender, Syncfusion.UI.Xaml.Grid.GridSortColumnsChangedEventArgs e)
        {
            foreach(var rec in this.dataGridCommands.View.Records)
            {
                if(rec.Data is Command cmd)
                {
                    Debug.WriteLine(cmd.Arrival);
                }
            }

            Debug.WriteLine("sorting done");
        }

After this I see from output that sorted items are opposite from what is displayed in dataGrid (displayed is correct).


GT Gnanasownthari Thirugnanam Syncfusion Team December 8, 2017 11:54 AM UTC

Hi Peter, 
You can get the sorted record from View.Records in SortColumnsChanged event using dispatcher like below code example. 
private async void SfDataGrid_SortColumnsChanged(object sender, Syncfusion.UI.Xaml.Grid.GridSortColumnsChangedEventArgs e) 
{ 
    var dispatcher = Windows.ApplicationModel.Core.CoreApplication.GetCurrentView().CoreWindow.Dispatcher; 
    await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => 
    { 
        foreach (var item in this.sfDataGrid.View.Records) 
        { 
            var employeeId = ((item as RecordEntry).Data as Employee).EmployeeID; 
            //Print the sorted record in output window. 
            System.Diagnostics.Debug.WriteLine(employeeId.ToString()); 
        } 
        System.Diagnostics.Debug.WriteLine("Sorting done"); 
    }); 
} 
 
We have modified the sample based on your requirement, you can download it from below mentioned location. 
You can also sort underlying collection mentioned way in below KB article. 
 Regards, 
Gnanasownthari T. 



PV Peter Verbo December 8, 2017 03:44 PM UTC

Awsome!!! Working perfectly - Thanks a lot! :)


GT Gnanasownthari Thirugnanam Syncfusion Team December 8, 2017 06:45 PM UTC


Hi Peter, 

Thank you for your update. 

Please let us know if you need further assistance on this. 

Regards, 
Gnanasownthari T. 


Loader.
Up arrow icon