Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get sorted itemssource from a datagrid

I have a grid with multiple columns and users can sort based on any column. Data, which is bound the grid is a collection of custom entity. I have a print button on the screen and on click of this button I need to do a custom print. Print must show the data in the same order as displayed in the grid (with additional data)

Is there any way to directly get the sorted datasource instead of sorting the datasource in the sorting event?

Thanks Pankaj

like image 906
Pankaj Vohra Avatar asked Sep 07 '11 11:09

Pankaj Vohra


People also ask

How do I sort in DataGrid?

To sort items in a DataGridCreate a SortDescription that specifies the property to sort by. You can specify the property in XAML or in code. In XAML, set the PropertyName to the name of the property to sort by. In code, pass the name of the property to sort by and the ListSortDirection to the constructor.

How do I sort a column in DataGrid WPF?

WPF DataGrid (SfDataGrid) allows you to sort the data against one or more columns either in ascending or descending order. The sorting can be performed by clicking a column header. You can enable/disable the sorting for all the columns in DataGrid by using DataGrid. AllowSorting property.


1 Answers

You have to use the yourDataGrid.Items, Items reflect the currentview of the grid. and you have to convert using the method Cast and after use .ToList();

imagine this

List<MyClass> myListOfMyClass = new List<MyClass>();
myGrid.ItemSource = myListOfMyClass;
List<MyClass> myListOfMyClassSortedByTheUser = myGrid.Items.Cast<MyClass>().ToList();
like image 96
Felipe Santos Avatar answered Oct 23 '22 19:10

Felipe Santos