Back to Devexpress

GridView.CustomColumnGroup Event

windowsforms-devexpress-dot-xtragrid-dot-views-dot-grid-dot-gridview.md

latest9.7 KB
Original Source

GridView.CustomColumnGroup Event

Provides the ability to group data using custom rules.

Namespace : DevExpress.XtraGrid.Views.Grid

Assembly : DevExpress.XtraGrid.v25.2.dll

NuGet Packages : DevExpress.Win.Grid, DevExpress.Win.Navigation

Declaration

csharp
[DXCategory("Sorting")]
public event CustomColumnSortEventHandler CustomColumnGroup
vb
<DXCategory("Sorting")>
Public Event CustomColumnGroup As CustomColumnSortEventHandler

Event Data

The CustomColumnGroup event's data class is CustomColumnSortEventArgs. The following properties provide information specific to this event:

PropertyDescription
ColumnGets the column whose values are being compared.
HandledGets or sets whether a comparison operation is handled and therefore no default processing is required.
ListSourceRowIndex1Gets the index in the data source of the first of the two rows being compared.
ListSourceRowIndex2Gets the index in the data source of the second of the two rows being compared.
ResultGets or sets the result of a custom comparison.
RowObject1Gets the first row object being compared.
RowObject2Gets the second row object being compared.
SortOrderGets the current sort order applied to the column being processed.
Value1Gets the first value being compared.
Value2Gets the second value being compared.

Remarks

Note

This event is not supported in server modes.

The CustomColumnGroup event can be handled to implement custom logic for grouping rows. This event is fired when any column’s GridColumn.SortMode property is set to ColumnSortMode.Custom and the View’s data is grouped by this column.

Each time the event is fired two adjacent rows are compared. If the rows should be combined into the same group, set the CustomColumnSortEventArgs.Result parameter to 0. Otherwise, set it to 1 (or -1 ). The CustomColumnSortEventArgs.Handled parameter must be set to true to indicate that the current comparison was handled and no default processing is required. If this parameter is left set to false the two rows will be compared using the default comparison mechanism.

The column by which grouping is applied is identified by the CustomColumnSortEventArgs.Column parameter. This column’s values in the rows which are being compared are specified by the CustomColumnSortEventArgs.Value1 and CustomColumnSortEventArgs.Value2 parameters.

When providing custom grouping logic via the CustomColumnGroup event, the text displayed by group rows should usually be overridden. To do this, handle the GridView.CustomDrawGroupRow event and supply the new text for each group row via the event’s Info.GroupText parameter.

Note

The CustomColumnGroup event is fired only for adjacent rows according to the current sort order. Sometimes you may need to change the default sorting logic in order to arrange the rows in specific order. To do this, the ColumnView.CustomColumnSort event can be handled.

Example

Assume that a Grid View’s “Order Sum” column contains numbers. When grouping is applied to this column, the rows which have values between 0 and 99 in this column should be combined into a single group, the rows whose values fall between 100 and 199 should be combined into another group, etc. To provide custom grouping logic the column’s GridColumn.SortMode property should be set to ColumnSortMode.Custom and the GridView.CustomColumnGroup event should be handled to implement the algorithm. In the following example the event’s Result parameter is set to 0 if the two rows being compared should be placed within the same group.

The ColumnView.CustomColumnDisplayText event is handled to replace the default text displayed within group rows.

The result of data being custom grouped is shown below:

csharp
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;

// ...
gridView1.Columns["Order Sum"].SortMode = ColumnSortMode.Custom;

private void gridView1_CustomColumnGroup(object sender, CustomColumnSortEventArgs e) {
   if(e.Column.FieldName == "Order Sum") {
      double x = Math.Floor(Convert.ToDouble(e.Value1) / 100);
      double y = Math.Floor(Convert.ToDouble(e.Value2) / 100);
      int res = Comparer.Default.Compare(x, y);
      if(x > 14 && y > 14) res = 0;
      e.Result = res;
      e.Handled = true;
   }
}

private void gridView_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e) {
    GridView view = sender as GridView;
    if(view == null) return;
    if (e.Column.FieldName == "Order Sum" && e.IsForGroupRow) {
        double rowValue = Convert.ToDouble(view.GetGroupRowValue(e.GroupRowHandle, e.Column));
        double val = Math.Floor(rowValue / 100);
        string groupRowInterval = string.Format("{0:c} - {1:c} ", val * 100, (val + 1) * 100);
        if (val > 14)
            groupRowInterval = string.Format(">= {0:c} ", val * 100);
        e.DisplayText = "Order Sum: " + groupRowInterval;
    }
}
vb
Imports DevExpress.XtraGrid
Imports DevExpress.XtraGrid.Views.Base
Imports DevExpress.XtraGrid.Views.Grid
Imports DevExpress.XtraGrid.Views.Grid.ViewInfo

   ' ...
   GridView1.Columns("Order Sum").SortMode = ColumnSortMode.Custom

Private Sub GridView1_CustomColumnGroup(ByVal sender As Object, _
ByVal e As CustomColumnSortEventArgs) Handles GridView1.CustomColumnGroup
   If e.Column.FieldName = "Order Sum" Then
      Dim x As Double = Math.Floor(Convert.ToDouble(e.Value1) / 100)
      Dim y As Double = Math.Floor(Convert.ToDouble(e.Value2) / 100)
      Dim res As Integer = Comparer.Default.Compare(x, y)
      If x > 14 And y > 14 Then res = 0
      e.Result = res
      e.Handled = True
   End If
End Sub

Private Sub GridView1_CustomColumnDisplayText(ByVal sender As Object, _
ByVal e As DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs) Handles GridView1.CustomColumnDisplayText
    Dim view As GridView = sender
    If view Is Nothing Then
        Return
    End If
    If e.Column.FieldName = "Order Sum" AndAlso e.IsForGroupRow Then
        Dim rowValue As Double = Convert.ToDouble(view.GetGroupRowValue(e.GroupRowHandle, e.Column))
        Dim val As Double = Math.Floor(rowValue / 100)
        Dim groupRowInterval As String = String.Format("{0:c} - {1:c} ", val * 100, (val + 1) * 100)
        If val > 14 Then
            groupRowInterval = String.Format(">= {0:c} ", val * 100)
        End If
        e.DisplayText = Convert.ToString("Order Sum: ") & groupRowInterval
    End If
End Sub

See Also

FieldNameSortGroup

GroupInterval

CustomColumnSort

Large Data Sources: Server and Instant Feedback Modes

GridView Class

GridView Members

DevExpress.XtraGrid.Views.Grid Namespace