How to get sorted itemssource from a datagrid

后端 未结 3 585
傲寒
傲寒 2021-01-17 18:22

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 scre

相关标签:
3条回答
  • 2021-01-17 18:43

    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();
    
    0 讨论(0)
  • 2021-01-17 18:48

    Yes, there is. In WPF, always a CollectionView is bound, never the collection itself. If you don't specify a collection view yourself, a default one is used. You can access this default collection view like this:

    CollectionViewSource.GetDefaultView(yourCollectionOfCustomEntities);
    

    This is the collection view as shown by the grid with all the sorting and filtering applied.

    0 讨论(0)
  • 2021-01-17 18:53

    You can also just enumerate the Items property of your DataGrid. Unlike the ItemsSource property, the Items property appears to reflect exactly what's on the screen including sorting and filtering. For example:

    foreach (var item in dataGrid.Items)
    {
        // do something
    }
    
    0 讨论(0)
提交回复
热议问题