Custom sorting on RADGRIDVIEW

2 Answers 645 Views
GridView
Peter
Top achievements
Rank 1
Iron
Iron
Peter asked on 29 Apr 2021, 10:10 AM

Dear Team,

In old times i used this code to have a custom sort on a datagridview:

    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

Now i need to achieve the same in WPF radgridview.. I have columns, with positive integers, zeroes and NULLs too.

Basically if i order : NULLs come first , then zeroes, then numbers ASC.

In my case customer needs to see in this way: zeroes, positive numbers asc, and nulls at the end.

The datasource is a datatable passed to radgridview as itemsource, and the column names (and number of them) are always dynamic, so i cannot predefine them in the XAML section. Please help

Thanks!

Peter B. (Hungary)

 

2 Answers, 1 is accepted

Sort by
0
Martin Ivanov
Telerik team
answered on 03 May 2021, 02:35 PM

Hello Peter,

I suggest you check the Custom Sorting help article which shows how to achieve your requirement with RadGridView for WPF.

Regards,
Martin Ivanov
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Peter
Top achievements
Rank 1
Iron
Iron
answered on 06 May 2021, 01:19 PM

Hello!

I tried to solve, based on the article you sent... without success :( How can i achieve the same if the itemsource is a datatable?

Peter

Martin Ivanov
Telerik team
commented on 11 May 2021, 06:51 AM

You can use the Sorting event in order to cancel the default sorting. Then in the event handler, you can sort the DataTable which is assigned to the ItemsSource of RadGridView. After this, you can re-set the sorted DataTable to the ItemsSource of the control. 

The following resources show how to sort a DataTable's rows.

https://www.c-sharpcorner.com/blogs/sorting-data-in-c-sharp-datatable1

https://stackoverflow.com/a/9108171/9024863

 

Tags
GridView
Asked by
Peter
Top achievements
Rank 1
Iron
Iron
Answers by
Martin Ivanov
Telerik team
Peter
Top achievements
Rank 1
Iron
Iron
Share this question
or