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
close icon

How to get sorted items from SfDataGrid?

Hello, dear developers!
I wanna ask, how can I get sorted and/or grouped items from SfDataGrid after user's sorting?
In other words, can I get items from grid in it's current visible order?

Thanks for advance.

3 Replies

MK Muthukumar Kalyanasundaram Syncfusion Team September 11, 2017 05:20 PM UTC

Hi Maltsev, 

Thank you for contacting Syncfusion support. 

We have checked your query. Pease find the response from below table. 


Query 1: 
How can I get sorted and/or grouped items from SfDataGrid after user's sorting? 

You can able to get the records of the group by passing the appropriate index to DisplayElements of View.TopLevelGroup as like the below code snippet,  

Code Snippet: 
 
// to get the toplevel groups items 
if (this.DataGrid1.View.TopLevelGroup != null) 
{                 
    var record = this.DataGrid1.View.TopLevelGroup.DisplayElements[this.DataGrid1.SelectionController.CurrentCellManager.CurrentRowColumnIndex.RowIndex]; 
} 

// to get the list of view records in sorted order 
var visibleOrder = this.DataGrid1.View.Records.ToList(); 

 

Query 2: 
Can I get items from grid in it's current visible order? 


You can achieve your requirement by using View.Records property as like below code, 

Code Snippet: 
// to get the list of view records 
var visibleOrder = this.DataGrid1.View.Records.ToList(); 
 


Please let us know if you have any other question. 

Regards, 
Muthukumar K 



MN Michal Nekvasil February 17, 2022 12:33 PM UTC

Hello,

what I'm trying to achive is to have two grids and after sorting first, i want to generate items in second. I try this aproach and event SortColumnsChanged. But if I use var visibleOrder = this.DataGrid1.View.Records.ToList() it does not return correct order, more likely i get "old sorting", not actual.


example .. i got grid with two rows, Values 1 and 2. I click sort. In event i get order 1 - 2. But in view I see 2 - 1. It looks like I was using SortColumnsChanging, becouse as i said, in this.DataGrid1.View.Records.ToList() items are not sorted right in a time event is called .. can you fix this?



VS Vijayarasan Sivanandham Syncfusion Team February 18, 2022 01:30 PM UTC

Hi Michal Nekvasil, 

Your requirement to display the sorted record list into another SfDataGrid can be achieved by customization  SortColumnsChanged event like below mentioned code snippet, 

private void OnSortColumnsChanged(object sender, GridSortColumnsChangedEventArgs e) 
{ 
            //get the list 
            IEnumerable<object> OrderedSource = sfDataGrid.View.Records.Select(x => x.Data).AsEnumerable();           
 
            foreach (var sortColumn in this.sfDataGrid.View.SortDescriptions) 
            { 
                var columnName = sortColumn.PropertyName; 
                
                if (sortColumn.Direction == ListSortDirection.Ascending) 
                    OrderedSource = OrderedSource.OrderBy(source => GetOrderSource(source, columnName)); 
 
                else 
                    OrderedSource = OrderedSource.OrderByDescending(source => GetOrderSource(source, columnName)); 
            } 
            //assign to another DataGrid 
            sfDataGrid1.ItemsSource = OrderedSource; 
} 
 
private object GetOrderSource(object source, string name) 
{ 
            var propInfo = source.GetType().GetRuntimeProperty(name); 
 
            if (propInfo != null) 
 
                // get the current sort column value 
                return propInfo.GetValue(source); 
 
            return null; 
} 

Regards, 
Vijayarasan S 


Loader.
Live Chat Icon For mobile
Up arrow icon