To add a new group

New groups are added to a report with the com.crystaldecisions.sdk.occa.report.application.GroupController class. A group is defined by a field, however, not all fields can be grouped on. The canGroupOn method of the GroupController class checks whether the field can be grouped on.
The order in which the groups appear in the Groups collection is important as it represents how the data is grouped in the report. The first group in the collection is the outermost group in the report; similarly, the last group in the collection is the innermost group in the report. When a group is removed, the indexes are re-organized so that the collection is still indexed from zero.

Note: When a new group is added to a report, a new sorting definition is also added. This sorting definition will sort the records according to the group's condition field and group options. However, adding a group does not physically add the field to the group header or footer sections of the report. If you want the field name visibily rendered for the new group, you must programmatically add it to the appropriate section using the ResultFieldController class.
  1. Retrieve the DataDefController object.
    DataDefController dataDefController = rcd.getDataDefController();
  2. Find the a field to group on.
    The IDataDefinition.getResultFields method returns all fields that have been added to the report design. This example assumes that the report design contains a Country field called from a table called Customer.
    Fields<IField> resultFields = dataDefController.getDataDefinition().getResultFields();
    IField groupField = resultFields.findField("{Customer.Country}", FieldDisplayNameType.formulaName, java.util.Locale.ENGLISH);
  3. Retrieve the GroupController object.
    GroupController groupController = dataDefController.getGroupController();
  4. Create a new IGroup object and set the field to group on with the setConditionField method.
    IGroup newGroup = new Group();
    newGroup.setConditionField(groupField);
  5. Call the add method of the GroupController class to add the new group to the report based on the chosen field.
    The first parameter of the add method specifies the location of the new group relative to any existing groups in the report. A value of -1 indicates that the new group should become the innermost group in the report.
    groupController.add(-1, newGroup);
Example: 
The following code adds a new group to a report based on the field Customer.Country. This example checks whether the selected field is a valid field to group on before attempting to create the group.
void addGroup(ReportClientDocument rcd) throws ReportSDKException
{
  DataDefController dataDefController = rcd.getDataDefController();
  Fields<IField> resultFields = dataDefController.getDataDefinition().getResultFields();
  IField groupField = resultFields.findField("{Customer.Country}", FieldDisplayNameType.formulaName, java.util.Locale.ENGLISH);
        
  GroupController groupController = dataDefController.getGroupController();
        
  if(groupController.canGroupOn(groupField))
  {
    IGroup newGroup = new Group();
    newGroup.setConditionField(groupField);    
    groupController.add(-1, newGroup);
  }
}
This list includes the classes used by the sample code:
  • com.crystaldecisions.sdk.occa.report.application.DataDefController
  • com.crystaldecisions.sdk.occa.report.application.GroupController
  • com.crystaldecisions.sdk.occa.report.application.ReportClientDocument
  • com.crystaldecisions.sdk.occa.report.data.IField
  • com.crystaldecisions.sdk.occa.report.data.Fields
  • com.crystaldecisions.sdk.occa.report.data.FieldDisplayNameType
  • com.crystaldecisions.sdk.occa.report.data.Group
  • com.crystaldecisions.sdk.occa.report.data.IGroup
  • com.crystaldecisions.sdk.occa.report.lib.ReportSDKException