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?

有帮助吗?

解决方案 2

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

其他提示

I've been using Janus for a while now (10 years I think...) but I never had to use that method for sorting a date column (I use the format dd/MM/yyyy). First point, if the data type is "Date", sorting will be done automatically whatever the format mask you use to display that date. However, if the data type of the column is "String", then the sort will be made by using the alphabetical comparison.

Second point the property GroupComparer is used to sort groups and not items. Assume you have a complex grouping rule with the group caption that is not relevant for the sort, then you can create a GroupComparer to sort the way you want.

Maybe can you start double checking the data type of your column?

Hope this helps Serge

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top