Rows won't show when sorting dynamically created columns

0 Answers 168 Views
GridView
Eldoir
Top achievements
Rank 2
Iron
Iron
Iron
Eldoir asked on 28 Oct 2022, 03:16 PM | edited on 28 Oct 2022, 03:22 PM

Hello,

I'm creating my columns dynamically, and when I click on a column header to sort it, it hides the rows instead of sorting them.

When I click again rows are still hidden (the sorting is descending), then when I click again, the sorting is None and the columns reappear.

Here is what I've tried already.

DataType

I've read on this forum that it happens when the DataType of the columns is not set, but I have my custom class of columns inheriting from GridViewDataColumn and I set the DataType to be the type of the value contained in the cell, so I think I'm fine on this side.

IComparable

I made all of my types implement IComparable, but it didn't help.

SortMemberPath

I tried setting SortMemberPath on my columns, doesn't help either. My rows are inheriting from DynamicObject, and I can see that the method TryGetMember(GetMemberBinder binder, out object result) (which I override) is indeed called when the grid starts sorting. So it seems to work well with DynamicObject, I'm not sure that having a DynamicObject could be an issue, but still, it might be worth mentioning.

OnSorting

I tried subscribing to OnSorting on the RadGridView and do my own logic in here, but when reassigning the rows by doing args.DataControl.ItemsSource = myRows.ToList(), it resolves my Linq query and throws an ArgumentException: "At least one object must implement IComparable", which I don't understand because I made sure all of my types implement IComparable.

So I tried blindly implementing IComparable on the classes representing my rows and cells as well, as a shot in the dark, but their implementation didn't get called so I don't know which objects the query really operates on.

IsCustomSortingEnabled

Also, I made sure that I'm setting IsCustomSortingEnabled on all of my columns when I create them.

ColumnSortDescriptor

I started creating my own ColumnSortDescriptor and overriding CreateSortKeyExpression, but I feel like using a simple linq query in OnSorting should be enough for my case, so I'm not sure it will help.

The End(?)

As you can see, I tried many things in very different ways and I'm running out of ideas now.

What can I do to sort my dynamic columns? Thanks!

P.S: I will try to make a simple demo project out of this, but I hope you'll be able to provide me with a simple answer in the meantime. Fingers crossed :)

Dilyan Traykov
Telerik team
commented on 01 Nov 2022, 04:44 PM

Hello Eldoir,

Thank you for the detailed description.

Indeed, you have tried most of the recommendations I would give with such a setup. I suspect that the use of DynamicObject instances might be the culprit. A similar issue has been discussed here: Sorting and filtering failure. Are the columns you wish to sort bound to nested properties of the DynamicObject? Can you try using an ICustomTypeProvider as suggested in the related bug report?

If this does not work for your or resolve the issue, the reason for this undesired behavior may lie elsewhere, but I cannot be certain without replicating the issue at my end. Can you please try isolating it in a sample project so that I can debug it locally?

If you do not find this possible, can you please try enabling the CLR exceptions and check whether any are thrown when performing the sort operation? Are there any binding errors shown in Visual Studio's Output window?

Eldoir
Top achievements
Rank 2
Iron
Iron
Iron
commented on 02 Nov 2022, 11:04 AM | edited

Hello Dilyan, thanks for your answer.

Yes, the columns I wish to sort are bound to a nested property of the DynamicObject: you will find attached a sample project showcasing my setup. I'm binding my RadGridView to an ObservableCollection of RowViewModel, which is a DynamicObject maintaining a collection of CellViewModel, which has a single property Value that can be of any type.

I've ran the SDK example with the ICustomTypeProvider, it looks like a pretty heavy solution but if it's the only one I have, I will consider it. My only concern is about the MyTypeHelper class: from what I understand, I would need all of my types to inherit from MyTypeHelper, in order to be sortable?  If so, that's not a reasonable solution for my project.

I've enabled all CLR exceptions, and didn't get any exception or binding error when performing the sort.

I hope the attached sample project will give you more information about my case. I'll try to implement ICustomTypeProvider for my project, while avoiding to have to inherit some arbitrary class on all of my types. Maybe I'm missing an architectural standpoint here.

Thanks for your help anyway! It is greatly appreciated.

 

EDIT: I also read in the doc WPF DataGrid | Sorting | Telerik UI for WPF that: "If the RadGridView is bound to a collection that inherits ICollectionView that has a CanSort property set to true, the RadGridView's sorting is disabled and the sorting mechanism of the collection is used instead."

It looks promising! Could you elaborate on this?  Do you have a sample project for it? Thanks!

EDIT2: also, do you have an insight on what happened when I tried OnSorting? I described my approach in my first post and didn't get positive results, but maybe I did it wrong. This approach looked promising as well, though.

EDIT3: I think I might have found a solution when stumbling upon this: https://stackoverflow.com/a/1877719/3623965. The sorting seems to work as expected when I implement IComparable in addition to IComparable<T>! Do you acknowledge this behavior?

Dilyan Traykov
Telerik team
commented on 03 Nov 2022, 01:36 PM

Hello Eldoir,

Thank you for the provided sample project and for the updates.

Indeed, with this particular setup, only the CompareTo method of the IComparable interface is invoked.

As a general rule of thumb, however, both the IComparable and IComparable<T> interfaces should be implemented as there are collections and classes that rely on only one of the implementations. This has been discussed in detail in the following forum thread: IComparable and IComparable<T>.

With this said, can you specify whether the IComparable solution you came across works in your original project as it does in the sample project you provided and whether you require any further assistance on the matter?

 

Eldoir
Top achievements
Rank 2
Iron
Iron
Iron
commented on 03 Nov 2022, 03:07 PM

Hello Dilyan, thanks for your reply. Indeed, that's a good practice I didn't know about.

It solves my problem in my original project, so that's fine by me, as long as it also looks like a clean solution to you.

Thanks again for your support!

Dilyan Traykov
Telerik team
commented on 04 Nov 2022, 12:06 PM

Hello Eldoir,

Indeed, this sounds like the best solution I can offer, given your setup. I'm glad to hear that it works for you.

Nonetheless, do let me know if any issues or concerns arise.

No answers yet. Maybe you can help?

Tags
GridView
Asked by
Eldoir
Top achievements
Rank 2
Iron
Iron
Iron
Share this question
or