This samples demonstrates how to sort groups by the value of a summary.
Showing summaries in group caption cells
Sorting by caption summaries
Implementation
This code declares the SummaryRowDescriptor and sets its properties.
GridSummaryRowDescriptor summaryRow1 = new GridSummaryRowDescriptor(); summaryRow1.Name = "Caption"; summaryRow1.Visible = false;
summaryRow1.SummaryColumns.Add(summaryColumn1);
GridTableDescriptor ordersDescriptor = this.gridGroupingControl1.TableDescriptor; ordersDescriptor.ChildGroupOptions.ShowCaptionSummaryCells = true; ordersDescriptor.ChildGroupOptions.CaptionSummaryRow = "Caption"; // Let's see you hide / show the second row in childgroups. ordersDescriptor.ChildGroupOptions.ShowSummaries = false;
this.gridGroupingControl1.Appearance.GroupCaptionCell.BackColor = this.gridGroupingControl1.Appearance.RecordFieldCell.BackColor; this.gridGroupingControl1.Appearance.GroupCaptionCell.Borders.Top = new GridBorder(GridBorderStyle.Standard); this.gridGroupingControl1.Appearance.GroupCaptionCell.CellType = "Static"; this.gridGroupingControl1.TableOptions.CaptionRowHeight = this.gridGroupingControl1.TableOptions.RecordRowHeight;
this.gridGroupingControl1.InvalidateAllWhenListChanged = true; // Specify the group sort order behaviour when adding SortColumnDescriptor to GroupedColumns. this.gridGroupingControl1.TableDescriptor.GroupedColumns.Clear(); SortColumnDescriptor gsd = new SortColumnDescriptor("ShipCountry"); // Code to specify our own Comparer. gsd.GroupSortOrderComparer = new ShipViaComparer(summaryColumn1.GetSummaryDescriptorName(), "Average"); // or specify a summary name and the property (values will be determined using reflection) gsd.SetGroupSummarySortOrder(summaryColumn1.GetSummaryDescriptorName(), "Average"); this.gridGroupingControl1.TableDescriptor.GroupedColumns.Add(gsd);
private void TableModel_QueryCoveredRange(object sender, GridQueryCoveredRangeEventArgs e) { GridTable thisTable = this.gridGroupingControl1.Table; if (e.RowIndex < thisTable.DisplayElements.Count) { Element el = thisTable.DisplayElements[e.RowIndex]; switch (el.Kind) { case DisplayElementKind.Caption: { // Cover some cells of the caption bar (specified with captionCover). IGridGroupOptionsSource gs = el.ParentGroup as IGridGroupOptionsSource; if (gs != null && gs.GroupOptions.ShowCaptionSummaryCells) { int startCol = el.GroupLevel+1; if (!gs.GroupOptions.ShowCaptionPlusMinus) startCol--; if (e.ColIndex >= startCol && e.ColIndex <= startCol+this.captionCoverCols) { e.Range = GridRangeInfo.Cells(e.RowIndex, startCol, e.RowIndex, startCol+this.captionCoverCols); e.Handled = true; } } break; } } } }
public class ShipViaComparer : IComparer { string summaryDescriptorName; string propertyName; public ShipViaComparer(string summaryDescriptorName, string propertyName) { this.summaryDescriptorName = summaryDescriptorName; this.propertyName = propertyName; } #region IComparer Members public int Compare(object x, object y) { Group gx = (Group) x; Group gy = (Group) y; bool strongTyped = false; if (strongTyped) { // strong typed (propertyName is ignored ...) DoubleAggregateSummary dasx = (DoubleAggregateSummary) gx.GetSummary(summaryDescriptorName); DoubleAggregateSummary dasy = (DoubleAggregateSummary) gy.GetSummary(summaryDescriptorName); int v = dasx.Average.CompareTo(dasy.Average); // Console.WriteLine("Compare {0} to {1}: {2}", dasx.Average, dasy.Average, v); return v; } else { // using reflection (slower but more flexible using propertyName) object vx = gx.GetSummaryProperty(summaryDescriptorName, propertyName); object vy = gy.GetSummaryProperty(summaryDescriptorName, propertyName); return ((IComparable) vx).CompareTo(vy); } } #endregion }
This sample displays the summaries in group captions. Click the grouped column header to get the records sorted by the summary values.