|
Say you have a column with values for 0 to 50 and you want to have them grouped so that values less than 10 are in one group, values 10 to 20 are in another, values 20 to30 in another, and so on. One way you can do this is to add an Expression column to your GridGroupingControl where the expression accepts a value and then returns a groupID value that identifies values less than 10 with the same ID, values 20 to 20 with another ID, and so on. Here is code that does this: Values less than 10 get ID equal to 1, values 10 to 20 get ID equal to 2, etc. C# string exp = "([Col2] < 10) * 1 " + "+ (([Col2] >= 20) * ([Col2] < 30)) * 2 " + "+ (([Col2] >= 30) * ([Col2] < 40)) * 3 " + "+ ([Col2] >= 40) * 4"; ExpressionFieldDescriptor fd = new ExpressionFieldDescriptor("picker", exp); this.gridGroupingControl1.TableDescriptor.ExpressionFields.Add(fd);
A more general way of categorizing records is to add your own custom Categorizer object to the SortColumnDescriptor that defines the group. You can also add a custom Comparer object to the SortColumnDescriptor. When a column is grouped, it is first sorted. The Comparer object allows you to control how the sorting is done on your column. Once the column is sorted, the custom Categorizer is used to determine which adjacent records in the sorted column belong to the same group. To create custom Comparer and Categorizer objects, you define classes that implement either IComparer (one method) or ICategorizer (two methods). C# public class CustomCategorizer : Syncfusion.Grouping.IGroupByColumnCategorizer { //defines a group and returns a group category object (here returns 1 through 5) public static int GetCategory(int i) { int ret = 0; if(i < 10) ret = 1; else if(i >= 10 && i < 20) ret = 2; else if(i >= 20 && i < 30) ret = 3; else if(i >= 30 && i < 40) ret = 4; else ret = 5; return ret; } public object GetGroupByCategoryKey(SortColumnDescriptor column, bool isForeignKey, Record record) { return GetCategory(int.Parse( record.GetValue(column).ToString())); } public int CompareCategoryKey(SortColumnDescriptor column, bool isForeignKey, object category, Record record) { return GetCategory( int.Parse(record.GetValue(column).ToString())) - (int)category; } } VB Public Class CustomCategorizer Implements Syncfusion.Grouping.IGroupByColumnCategorizer 'defines a group and returns a group category object (here returns 1 through 5) Public Shared Function GetCategory(ByVal i As Integer) As Integer Dim ret As Integer = 0 If i < 10 Then ret = 1 ElseIf i >= 10 AndAlso i < 20 Then ret = 2 ElseIf i >= 20 AndAlso i < 30 Then ret = 3 ElseIf i >= 30 AndAlso i < 40 Then ret = 4 Else ret = 5 End If Return ret End Function 'GetCategory Public Function GetGroupByCategoryKey(ByVal column As SortColumnDescriptor, ByVal isForeignKey As Boolean, ByVal record As Record) As Object Implements IGroupByColumnCategorizer.GetGroupByCategoryKey Return GetCategory(Integer.Parse(record.GetValue(column).ToString())) End Function 'GetGroupByCategoryKey Public Function CompareCategoryKey(ByVal column As SortColumnDescriptor, ByVal isForeignKey As Boolean, ByVal category As Object, ByVal record As Record) As Integer Implements IGroupByColumnCategorizer.CompareCategoryKey Return GetCategory(Integer.Parse(record.GetValue(column).ToString())) - Fix(category) End Function 'CompareCategoryKey End Class 'CustomCategorizer Sample: http://websamples.syncfusion.com/samples/kb/grid.windows/GGCCustomGrouping/main.htm |