How to calculate summaries for both minimum and maximum values of the date field in WinForms GridGroupingControl?
Summaries
There are no
default summary types to find min/max values for a particular column. To find
min/max values for a particular column, create a custom summary. To create a
custom summary, refer to the following solution.
Solution
To create a
custom summary, derive the SummaryBase class and then the Combine method
is used to get the new summary value by comparing it with other summary values.
In the given sample, DateMaxSummary and DateMinSummary are
the two sealed classes derived from the SummaryBase to find
the min/max date summary values.
The following code example demonstrates how the Combine method compares and gets the maximum date value.
public sealed class DateMaxSummary : SummaryBase
{
DateTime _max;
public DateMaxSummary(DateTime max)
{
_max = max;
}
public static readonly DateMaxSummary Empty = new DateMaxSummary(DateTime.MinValue);
// Assign this CreateSummaryDelegate handler method to SummaryDescriptor.CreateSummaryMethod
public static ISummary CreateSummaryMethod(SummaryDescriptor sd, Record record)
{
object obj = sd.GetValue(record);
bool isNull = (obj == null || obj is DBNull);
if(isNull)
return Empty;
else
{
DateTime val = Convert.ToDateTime(obj);
return new DateMaxSummary(val);
}
}
// This will return Max value of this summary
public DateTime Max
{
get
{
return _max;
}
}
public override SummaryBase Combine(SummaryBase other)
{
return Combine((DateMaxSummary)other);
}
// Combines this summary information with another objects summary and returns a new object.
public DateMaxSummary Combine(DateMaxSummary other)
{
// Return the Maximum value
if(Max > other.Max)
return new DateMaxSummary(this.Max);
else
return new DateMaxSummary(other.Max);
}
}Public NotInheritable Class DateMaxSummary
Inherits SummaryBase
Private _max As DateTime
Public Sub New(ByVal max As DateTime)
_max = max
End Sub
Public Shared ReadOnly Empty As New DateMaxSummary(DateTime.MinValue)
' Assign this CreateSummaryDelegate handler method to SummaryDescriptor.CreateSummaryMethod
Public Shared Function CreateSummaryMethod(ByVal sd As SummaryDescriptor, ByVal record As Record) As ISummary
Dim obj As Object = sd.GetValue(record)
Dim isNull As Boolean = (obj Is Nothing OrElse TypeOf obj Is DBNull)
If isNull Then
Return Empty
Else
Dim val As DateTime = Convert.ToDateTime(obj)
Return New DateMaxSummary(val)
End If
End Function
' This will return Max value of this summary
Public ReadOnly Property Max() As DateTime
Get
Return _max
End Get
End Property
Public Overrides Function Combine(ByVal other As SummaryBase) As SummaryBase
Return Combine(CType(other, DateMaxSummary))
End Function
' Combines this summary information with another objects summary and returns a new object.
Public Function Combine(ByVal other As DateMaxSummary) As DateMaxSummary
' Return the Maximum value
If Max > other.Max Then
Return New DateMaxSummary(Me.Max)
Else
Return New DateMaxSummary(other.Max)
End If
End Function
End Class Note:
Similarly, DateMinSummary is also derived from the SummaryBase class.
Use the following steps to create the date summary field.
Step 1: Create GridSummaryColumnDescriptor instance.
GridSummaryColumnDescriptor sd1 = new GridSummaryColumnDescriptor();
sd1.Name = "MaxDate";
sd1.DataMember = "Date";
sd1.DisplayColumn = "Date";
sd1.Format = "{Max}";
sd1.SummaryType = SummaryType.Custom;
//Adding the Custom Summary rows
this.gridGroupingControl1.TableDescriptor.SummaryRows.Add(new GridSummaryRowDescriptor("Row 1", "Max", sd1));Dim sd1 As New GridSummaryColumnDescriptor()
sd1.Name = "MaxDate"
sd1.DataMember = "Date"
sd1.DisplayColumn = "Date"
sd1.Format = "{Max}"
sd1.SummaryType = SummaryType.Custom
'Adding the Custom Summary rows
Me.gridGroupingControl1.TableDescriptor.SummaryRows.Add(New GridSummaryRowDescriptor("Row 1", "Max", sd1))Step 2: Use the QueryCustomSummary event to instantiate the custom summary for maximum and minimum date.
//Hook the QueryCustomSummary event In Form_Load to create summary method while adding the CustomSummary
this.gridGroupingControl1.QueryCustomSummary += gridGroupingControl1_QueryCustomSummary;
private void gridGroupingControl1_QueryCustomSummary(object sender, GridQueryCustomSummaryEventArgs e)
{
switch (e.SummaryColumn.Name)
{
case "MaxDate":
{
e.SummaryDescriptor.CreateSummaryMethod = new CreateSummaryDelegate(DateMaxSummary.CreateSummaryMethod);
break;
}
case "MinDate":
{
e.SummaryDescriptor.CreateSummaryMethod = new CreateSummaryDelegate(DateMinSummary.CreateSummaryMethod);
break;
}
}
}'Hook the QueryCustomSummary event In Form_Load to create summary method while adding the CustomSummary
Addhandler Me.gridGroupingControl1.QueryCustomSummary, AddressOf gridGroupingControl1_QueryCustomSummary
Private Sub gridGroupingControl1_QueryCustomSummary(ByVal sender As Object, ByVal e As GridQueryCustomSummaryEventArgs)
Select Case e.SummaryColumn.Name
Case "MaxDate"
e.SummaryDescriptor.CreateSummaryMethod = New CreateSummaryDelegate(DateMaxSummary.CreateSummaryMethod)
Exit Select
Case "MinDate"
e.SummaryDescriptor.CreateSummaryMethod = New CreateSummaryDelegate(DateMinSummary.CreateSummaryMethod)
Exit Select
End Select
End SubNote:
Hook the QueryCustomSummary event before adding the custom summary fields.
Step 3: Update the summary values.
Using
the CurrentRecordContextChanged event, you can immediately
update the summary values by invalidating the current record.
//Hook the event in Form_Load
this.gridGroupingControl1.CurrentRecordContextChange += gridGroupingControl1_CurrentRecordContextChange;
private void gridGroupingControl1_CurrentRecordContextChange(object sender, CurrentRecordContextChangeEventArgs e)
{
if(e.Action == CurrentRecordAction.EndEditComplete)
{
//Invalidate the Record to Update Summary Values
e.Record.InvalidateCounterBottomUp();
this.gridGroupingControl1.Refresh();
}
}'Hook the event in Form_Load
AddHandler Me.gridGroupingControl1.CurrentRecordContextChange, AddressOf gridGroupingControl1_CurrentRecordContextChange
Private Sub gridGroupingControl1_CurrentRecordContextChange(ByVal sender As Object, ByVal e As CurrentRecordContextChangeEventArgs)
If e.Action = CurrentRecordAction.EndEditComplete Then
'Invalidate the Record to Update Summary Values
e.Record.InvalidateCounterBottomUp()
Me.gridGroupingControl1.Refresh()
End If
End SubThe following
screenshot illustrates the min/max date summary in GridGroupingControl.

Figure 1: Min/Max Date Summary in GridGroupingControl
Samples: