SAP Knowledge Base Article - Public

1638023 - How to add sort field using RAS .NET?


  • How to add sort field through using RAS .NET?


  • Crystal Reports 2008
  • BOE 3.1
  • .NET Framework 2.0


  • The snippet code below in c#, demonstrates how to add sort field using RAS .NET:

 Fields resultFields = dataDefController.DataDefinition.ResultFields;
 Field sortField = (Field)resultFields.FindField("<fieldName>", CrFieldDisplayNameTypeEnum.crFieldDisplayNameLong, CeLocale.ceLocaleEnglishUS);

SortController sortController = dataDefController.SortController

       if (sortController.CanSortOn(sortField))
            Sort newSort = new SortClass();
            newSort.SortField = sortField;
            newSort.Direction = CrSortDirectionEnum.crSortDirectionAscendingOrder;
            sortController.Add(3, newSort);

For more information about the SDK, refer to:

  • Developer Guide:

  • API Reference

For Crystal Reports Developer for .NET ( version 13 and 14 runtime ) you can also add a formula sort on string or field using the following:

CrystalDecisions.ReportAppServer.ObjectFactory.ObjectFactory objFactory = new CrystalDecisions.ReportAppServer.ObjectFactory.ObjectFactory();
CrystalDecisions.ReportAppServer.DataDefModel.FormulaField Formula = (CrystalDecisions.ReportAppServer.DataDefModel.FormulaField)objFactory.CreateObject("CrystalReports.FormulaField");

// crFieldValueTypeStringField must nmatch type
Formula.Type = CrystalDecisions.ReportAppServer.DataDefModel.CrFieldValueTypeEnum.crFieldValueTypeStringField;
Formula.Syntax = CrystalDecisions.ReportAppServer.DataDefModel.CrFormulaSyntaxEnum.crFormulaSyntaxCrystal;

// NOTE: if the formula is a string only then you must add whilereadingrecords to the formula text so CR can detect it's a valid formula.
Formula.Text = @"whilereadingrecords; ""A""";
Formula.Name = "TestDon";

//Verify the text is valid
String FormulaMessage = rptClientDoc.DataDefController.FormulaFieldController.Check(Formula);
if (FormulaMessage == null)
    btnReportObjects.Text += "There are errors in the formula: " + FormulaMessage.ToString() + "\n";

 Note: No Sort is achived by not adding the field/formula to the sort object list. If you want to change to no sort then remove the field/formula from the collection.

To validate if a formula can be sorted on this will confirm it if IsRecurring returns false:

if (rptClientDoc.DataDefController.SortController.CanSortOn(RASSortField.SortField))
    MessageBox.Show("true " + RASSortField.SortField.IsRecurring.ToString());
    MessageBox.Show("False " + RASSortField.SortField.IsRecurring.ToString());


Add sort field in .NET; sort field in .NET; sort field; .NET , KBA , BI-DEV-NET , BI Software Development Kits (SDKs) - .NET or Other , BI-DEV , Business Intelligence Software Development Kits (SDKs) , Problem


Crystal Reports 2008 V0 ; SAP Crystal Reports, developer version for Microsoft Visual Studio