Home Forums WPF controls Xceed DataGrid for WPF Sorting at runtime

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #21690 |

    According to the information in the documentation, sorting is enabled by default and clicking a column header will cause the grid to sort by that column. Unfortunately, this doesn’t seem to be working for me.

    I am binding to a generic collection that inherits from BindingList<T> and ITypedList and have the AutoCreateColumns property set to true. I am using the following code to bind the grid:

    RootDocumentCollection documents = ClientContext.Current.ExecuteQuery(query);
    if (documents.Count > 0)
    {
    this.ActionTestDataGrid.Columns.Clear();
    Binding bind = new Binding();
    bind.Source = documents;
    this.ActionTestDataGrid.DataContext = documents;
    this.ActionTestDataGrid.SetBinding(DataGridControl.ItemsSourceProperty, bind);
    }

    All of this seems to work flawlessly and I am able to display the same data as I would see binding to a normal DataGridView in Windows Forms.

    I was hoping this was all I would need to do. I am able to group by columns properly and all of the column headers are displayed corectly.

    Thanks,

    Scott.

    Imported from legacy forums. Posted by Scott (had 3308 views)

    Xceed Support
    Member
    Post count: 5658

    This is because BindingList<T>.SupportsSorting always returns “false” (through the SupportsSortingCore property).

    You can try to encapsulate the collection in a ListCollectionView instead:

    <code>
    this.ActionTestDataGrid.Columns.Clear();
    Binding bind = new Binding();
    bind.Source = new ListCollectionView( documents );
    this.ActionTestDataGrid.SetBinding(DataGridControl.ItemsSourceProperty, bind);
    </code>

    But since ListCollectionView does not implement IBindingList, insertion would not be supported.

    An alternative would be to override SupportsSortingCore and ApplySortCore from the BindingList<T> class and provide your own sorting logic (you can look into Array.Sort(), which can sort an array based on a comparer ).

    In the mean time, we’re going to look into this a little more, since we are surprised that no implementation of IBindingList except DataView implements sorting.

    We are going to follow-up in this thread whenever we find something.

    Imported from legacy forums. Posted by Marcus [Xceed] (had 376 views)

    Xceed Support
    Member
    Post count: 5658

    After performing further verification, I have to admit I was wrong…

    If you encapsulate your BindingList<T> implementation into a ListCollectionView, we still detect that the underlying data collection is a IBindingList and use it.

    Therefore, all you need to do is as described above…

    Sorry…

    Imported from legacy forums. Posted by Marcus [Xceed] (had 390 views)

    User (Old forums)
    Member
    Post count: 23064

    Excellent news. Your follow up post couldn’t have been timed any better. I was just getting ready to start discussing the plan for implementing the custom sorting and overriding SupportsSortingCore and ApplySortCore with the developer responsible for the collection.

    I made the changes you described about encapsulating the collection in a ListViewCollection and the sorting is working properly.

    Thanks!

    Imported from legacy forums. Posted by Scott (had 3484 views)

    User (Old forums)
    Member
    Post count: 23064

    Hi

     I also use IBindingList (I override BindingList<T>) and return true for SupportsSortingCore. The method ApplySortCore is not called like this. I found the solution in Beatriz Costa’s blog comment (http://www.beacosta.com/blog/?p=24). It appears that we must also implement ITypedList. I tryed it and ApplySortCore is called (in this method I call Array.Sort with my own comparator). Like this there is no need to wrap the IBindingList with the ListCollectionView (didn’t try if it also works that way).

    PS: the normal sorting way (reflection) does not work in my case because my data objects are not strongly typed.

    Best regards

    Stefan

    Imported from legacy forums. Posted by ステファン (had 1266 views)

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.