假设网格视图的“Order Sum”列包含数值。 当按照该列进行分组时,该列中取值介于 0 至 99 之间的行应被组合为一个组,取值介于 100 至 199 之间的行应被组合为另一个组,等等。 为了提供自定义分组逻辑,应该把列的 GridColumn.SortMode 属性设置为 ColumnSortMode.Custom ,并接管 GridView.CustomColumnGroup 事件来实现算法。 在下面的示例中,如果应放置在相同组中的两行被比较,则事件的 Result 参数被设置为 0

接管了 GridView.CustomDrawGroupRow 事件来替换在分组行中显示的默认文本。

数据的自定义分组结果在下面显示:

C#CopyCode image复制代码
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 gridView1_CustomDrawGroupRow(object sender, RowObjectCustomDrawEventArgs e) {
   GridGroupRowInfo info = e.Info as GridGroupRowInfo;
   if(info == null) return;
   if(info.Column.FieldName == "Order Sum") {
      string interval = IntervalByValue(gridView1.GetGroupRowValue(info.RowHandle));
      // Gets the summary text.
      string sumText = gridView1.GetGroupSummaryText(info.RowHandle);
      // Replace the group row's default text.
      info.GroupText = "Order Sum: " + interval + sumText;
   }
}

// Gets the text representing the interval that contains the specified value.
string IntervalByValue(object val) {
   double d = Math.Floor(Convert.ToDouble(val) / 100);
   string ret = string.Format("{0:c} - {1:c} ", d * 100, (d + 1) * 100);
   if(d > 14) ret = string.Format(">= {0:c} ", d * 100);
   return ret;
}

Visual BasicCopyCode image复制代码
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_CustomDrawGroupRow(ByVal sender As Object, _
ByVal e As RowObjectCustomDrawEventArgs) Handles GridView1.CustomDrawGroupRow
   Dim info As GridGroupRowInfo = CType(e.Info, GridGroupRowInfo)
   If info Is Nothing Then Return
   If info.Column.FieldName = "Order Sum" Then
      Dim interval As String = IntervalByValue(GridView1.GetGroupRowValue(info.RowHandle))
      ' Get the summary text.
      Dim sumText As String = GridView1.GetGroupSummaryText(info.RowHandle)
      ' Replace the group row's default text.
      info.GroupText = "Order Sum: " + interval + sumText
   End If
End Sub

' Gets the text representing the interval that contains the specified value.
Function IntervalByValue(ByVal val As Object) As String
   Dim d As Double = Math.Floor(Convert.ToDouble(val) / 100)
   Dim ret As String = String.Format("{0:c} - {1:c} ", d * 100, (d + 1) * 100)
   If d > 14 Then ret = String.Format(">= {0:c} ", d * 100)
   Return ret
End Function