This is a migrated thread and some comments may be shown as answers.

Sort Nulls To The End Of The List

3 Answers 232 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Steven
Top achievements
Rank 1
Steven asked on 18 Mar 2013, 08:46 AM
Hi,

I have a RadGridView with a large number of columns which are bound to decimal values. Currently when you sort the columns null values are always sorted to have lowest value.

However, our client would like for nulls to always be sorted to the end of the list when the column is sorted in both ascending and descending modes.

I was wondering if there was a way to achieve this with having to do something for each column individually?

Thanks,

Steven

3 Answers, 1 is accepted

Sort by
0
Accepted
Rossen Hristov
Telerik team
answered on 18 Mar 2013, 09:02 AM
Hello,

I am afraid that we use the .NET Framework to do the sorting and we cannot change its sorting logic.

The only possible way I can think of would be to use some custom class instead of decimal and to implement the IComparable or IComparable<T> interface on this class so that the .NET Framework uses it for sorting.

Greetings,
Rossen Hristov
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Steven
Top achievements
Rank 1
answered on 19 Mar 2013, 03:56 PM
Thanks for the quick reply.

Steven
0
Peter
Top achievements
Rank 1
Iron
Iron
answered on 29 Apr 2021, 12:43 PM

This was my approach for the same in normal winform-datagridview, wired to the SORTCOMPARE event. But telerik's radgrid doenst have such a thing. I hope it will have in new versions, because i need it too

 

    Private Sub DG_RM_SortCompare(sender As Object, e As DataGridViewSortCompareEventArgs) Handles DG_RM.SortCompare

        '''' <summary>Gets or sets a value indicating the order in which the compared cells will be sorted.</summary>
        '''' <returns>Less than zero if the first cell will be sorted before the second cell; zero if the first cell and second cell have equivalent values; greater than zero if the second cell will be sorted before the first cell.</returns>

        If e.Column.Index > 2 Then
            Try
                If e.CellValue1 Is Nothing OrElse e.CellValue1.Equals(DBNull.Value) Then
                    If e.CellValue2 Is Nothing OrElse e.CellValue2.Equals(DBNull.Value) Then
                        e.SortResult = 0
                    Else
                        e.SortResult = 1
                    End If
                Else
                    If e.CellValue2 Is Nothing OrElse e.CellValue2.Equals(DBNull.Value) Then
                        e.SortResult = -1
                    Else
                        e.SortResult = DirectCast(e.CellValue1, IComparable).CompareTo(DirectCast(e.CellValue2, IComparable))
                    End If
                End If
                e.Handled = True

            Catch ex As Exception
                MsgBox(Err.Description)
                Close()
            End Try
        End If

    End Sub

Martin Ivanov
Telerik team
commented on 03 May 2021, 03:55 PM

I can suggest you to take a look at the Custom Sorting article.
Tags
GridView
Asked by
Steven
Top achievements
Rank 1
Answers by
Rossen Hristov
Telerik team
Steven
Top achievements
Rank 1
Peter
Top achievements
Rank 1
Iron
Iron
Share this question
or