vb.netsortingcurrencyradgridviewcolumnsorting

Sorting Currency Column VB.NET in RadGridView


I'm trying to sort the currency column in a radgridview using Custom Sorting.

The only other question I could find that's related was this one: Sorting currency in VB.NET but it doesn't provide a solution.

I tried using the Telerik example https://docs.telerik.com/devtools/winforms/controls/gridview/sorting/custom-sorting which I have implemented below.

Private Sub CustomSort(sender As Object, e As GridViewCustomSortingEventArgs)
    Dim row1 As Decimal
    Dim row2 As Decimal

    If e.Row1.Cells("Pickable").Value.ToString = "" Then
        row1 = 0
    Else
        row1 = Convert.ToDecimal(e.Row1.Cells("Pickable").Value.ToString.Substring(1))
    End If
    If e.Row2.Cells("Pickable").Value.ToString = "" Then
        row2 = 0
    Else
        row2 = Convert.ToDecimal(e.Row2.Cells("Pickable").Value.ToString.Substring(1))
    End If

    If row1 > row2 Then
        e.SortResult = 1
    ElseIf row1 < row2 Then
        e.SortResult = -1
    Else
        e.SortResult = 0
    End If
End Sub

It provides me with the following result when I sort:

Results

But then it won't allow me to sort descending.

Any help would be appreciated, thanks!

Using answer suggested below - I have implemented a Click Event. click event

SOLUTION solution


Solution

  • Assuming that your cells actually contains Strings, try this:

    Dim value1 As Decimal
    Dim value2 As Decimal
    
    Decimal.TryParse(CStr(e.Row1.Cells("Pickable").Value), NumberStyles.Currency, Nothing, value1)
    Decimal.TryParse(CStr(e.Row2.Cells("Pickable").Value), NumberStyles.Currency, Nothing, value2)
    
    e.SortResult = value1.CompareTo(value2)
    

    If the blank cells are actually NULL rather than empty then you can take advantage of the fact that DBNull.ToString returns an empty String:

    Decimal.TryParse(e.Row1.Cells("Pickable").Value.ToString(), NumberStyles.Currency, Nothing, value1)
    Decimal.TryParse(e.Row2.Cells("Pickable").Value.ToString(), NumberStyles.Currency, Nothing, value2)
    

    With regards to sorting in descending order, I don't know how that control indicates the sort direction but, if you're doing custom sorting, I'm guessing that it's up to you to remember it yourself. In that case, you could use a field to specify the current direction:

    Private isDescending As Boolean = True
    

    You can then just toggle that on each sort, e.g. in a Button.Click event handler before calling a Sort method:

    isDescending = Not isDescending
    

    You can then set the comparison result based on that direction:

    Dim value1 As Decimal
    Dim value2 As Decimal
    
    Decimal.TryParse(e.Row1.Cells("Pickable").Value.ToString(), NumberStyles.Currency, Nothing, value1)
    Decimal.TryParse(e.Row2.Cells("Pickable").Value.ToString(), NumberStyles.Currency, Nothing, value2)
    
    e.SortResult = If(isDescending, value2.CompareTo(value1), value1.CompareTo(value2))