Search code examples
.netvb.netinterfacejanusgridex

Trying to implement custom grouping in the Janus GridEx control


I am trying to implement custom grouping in the Janus GridEx control. I have a column that has data that is DateTime, but when I group on that column, I want the data to group based on only the Date portion of that data.

Reading the Janus documentation, it looks like adding a GroupComparer to a Column should accomplish this:

' Code that sets up my Janus GridEx
...
...
grdResults.RootTable.Columns("DateDue").GroupComparer = New GroupByDateComparer()
...
...

My IComparer class...

Public Class GroupByDateComparer
    Implements IComparer
    Public Function Compare(a As Object, b As Object) As Integer _
                                      Implements IComparer.Compare
        Select Case DateDiff(DateInterval.Day, a.Date, b.Date)
            Case Is < 0
                Return -1
            Case 0
                Return 0
            Case Is > 0
                Return 1
        End Select
        Return 0
    End Function
End Class

My code builds. The line of code where I assign a new instance to the GroupComparer runs. But the Compare() function is never called.

Has anyone managed to implement this feature of the Janus GridEx control?


Solution

  • Okay, it took me longer than it should have. It turns out the Comparer was working perfectly, but it wasn't 'sticking' to the column. I found that if I check if it exists in the RootTableChanged event and re-attach it if it isn't it all works fine...

    Private Sub grdResults_RootTableChanged(sender As Object, e As EventArgs) _ 
                                            Handles grdResults.RootTableChanged
    
        If grdResults.RootTable.Columns.Contains("DateDue") AndAlso _ 
               grdResults.RootTable.Columns("DateDue").GroupComparer Is Nothing Then
            grdResults.RootTable.Columns("DateDue").GroupComparer = New GroupByDateComparer()
        End If
    End Sub