To add a new sorting definition

Parent Previous Next

 

Report Application Server .NET SDK Developer Guide

To add a new sorting definition


 







New sorting definitions are added to a report with the CrystalDecisions.ReportAppServer.Controllers.SortController class. A sorting definition is defined by a field and a direction, however, not all fields can be grouped on. The CanSortOn method of the SortController class checks whether the field can be sorted on.

The order in which the sorting definitions appear in the Sorts collection is important as it represents the order by which sorting definitions are applied to records. When a sorting definition is removed, the indexes are re-organized so that the collection is still indexed from zero.

  1. Retrieve the DataDefController object.

Visual Basic

Dim dataDefController As DataDefController = rcd.DataDefController

C#

DataDefController dataDefController = rcd.DataDefController;

  1. Find the a field to sort on.

The DataDefinition.ResultFields property returns all fields that have been added to the report design. This example assumes that the report design contains a Last_Years_Sales field called from a table called Customer.

Visual Basic

Dim resultFields As Fields = dataDefController.DataDefinition.ResultFields
Dim sortField As Field = DirectCast(resultFields.FindField("{Customer.Last_Years_Sales}", CrFieldDisplayNameTypeEnum.crFieldDisplayNameFormula, CeLocale.ceLocaleEnglishUS), Field)

C#

Fields resultFields = dataDefController.DataDefinition.ResultFields;
Field sortField = (Field)resultFields.FindField("{Customer.Last_Years_Sales}", CrFieldDisplayNameTypeEnum.crFieldDisplayNameFormula, CeLocale.ceLocaleEnglishUS);

  1. Retrieve the SortController object.

Visual Basic

Dim sortController As SortController = dataDefController.SortController

C#

SortController sortController = dataDefController.SortController;

  1. Create a new Sort object, set the field to sort on with the SortField property, and set the direction of the sort.

Visual Basic

Dim newSort As Sort = New SortClass()
newSort.SortField = sortField
newSort.Direction = CrSortDirectionEnum.crSortDirectionAscendingOrder

C#

Sort newSort = new SortClass();
newSort.SortField = sortField;
newSort.Direction = CrSortDirectionEnum.crSortDirectionAscendingOrder;

  1. Call the Add method of the SortController class to add the new sort to the report based on the chosen field.

The first parameter of the Add method specifies the location of the new sorting definition in the Sorts collection. A value of -1 indicates that the new sorting definition will be added to the end of the Sorts collection. This means that the new sorting definition will be applied to records last after all other sorting definitions have been applied.

Visual Basic

sortController.Add(0, newSort)

C#

sortController.Add(0, newSort);

The following code adds a new sorting definition to a report based on the field Customer.Last_Years_Sales. This example checks whether the selected field is a valid field to sort on before attempting to add the sorting definition.

Visual Basic

Private Sub AddSort(ByVal rcd As ISCDReportClientDocument)
 Dim dataDefController As DataDefController = rcd.DataDefController
 
 Dim resultFields As Fields = dataDefController.DataDefinition.ResultFields
 Dim sortField As Field = DirectCast(resultFields.FindField("{Customer.Last_Years_Sales}", CrFieldDisplayNameTypeEnum.crFieldDisplayNameFormula, CeLocale.ceLocaleEnglishUS), Field)
 Dim sortController As SortController = dataDefController.SortController
 If sortController.CanSortOn(sortField) Then
  Dim newSort As Sort = New SortClass()
  newSort.SortField = sortField
  newSort.Direction = CrSortDirectionEnum.crSortDirectionAscendingOrder
  sortController.Add(0, newSort)
 End If
End Sub

C#

private void AddSort(ISCDReportClientDocument rcd)
{
 DataDefController dataDefController = rcd.DataDefController;
 
 Fields resultFields = dataDefController.DataDefinition.ResultFields;
 Field sortField = (Field)resultFields.FindField("{Customer.Last_Years_Sales}", CrFieldDisplayNameTypeEnum.crFieldDisplayNameFormula, CeLocale.ceLocaleEnglishUS);
 SortController sortController = dataDefController.SortController;
 if (sortController.CanSortOn(sortField))
 {
  Sort newSort = new SortClass();
  newSort.SortField = sortField;
  newSort.Direction = CrSortDirectionEnum.crSortDirectionAscendingOrder;
  sortController.Add(0, newSort);
 }
}

This list includes the namespaces used by the sample code:

© 2021 SAP AG. All rights reserved.

http://www.sap.com/sapbusinessobjects/

Support services

http://service.sap.com/bosap-support/

Created with the Personal Edition of HelpNDoc: Full-featured EBook editor