Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

.net Crystal Report adding sorting dynamically

Hi,

Can anyone help me with this issue. I want to remove the old sort fields and add new sort fields. After searching alot in google I found out that there isn't a direct way to add new sort field in .net. So I have used Reflection.

Below is my code

public class ReportSortingGrouping

{

public int Index { get; set; }

public string FieldName { get; set; }

public SortDirection SortDirection { get; set; }

}

Removing Sorting and adding new sorting.

public static void ReportSorting(ReportDocument cryRpt, List<ReportSortingGrouping> sortByParams, bool resetSorting)

{

if (null != sortByParams)

{

SortFields mySortFields;

MethodInfo getRasSorts;

object rasSorts;

MethodInfo removeSort;

MethodInfo addSort;

Assembly rasAssembly;

ConstructorInfo ciRasSort;

object rasSort;

MethodInfo setSortField;

object[] aiParam = new object[1];

FieldDefinition fieldDef;

if (resetSorting)

{

int totalSortFields = cryRpt.DataDefinition.SortFields.Count;

for (int i = 0; i < totalSortFields; i++)

{

mySortFields = cryRpt.DataDefinition.SortFields;

getRasSorts = mySortFields.GetType().GetMethod("get_RasSorts", BindingFlags.NonPublic | BindingFlags.Instance);

rasSorts = getRasSorts.Invoke(mySortFields, System.Type.EmptyTypes);

removeSort = rasSorts.GetType().GetMethod("Remove");

rasAssembly = getRasSorts.ReturnType.Assembly;

ciRasSort = rasAssembly.GetType("CrystalDecisions.ReportAppServer.DataDefModel.SortClass").GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, System.Type.EmptyTypes, null);

rasSort = ciRasSort.Invoke(System.Type.EmptyTypes);

setSortField = rasSort.GetType().GetMethod("set_SortField", BindingFlags.Public | BindingFlags.Instance);

aiParam[0] = 0;

removeSort.Invoke(rasSorts, aiParam);

}

}

foreach (var param in sortByParams)

{

mySortFields = cryRpt.DataDefinition.SortFields;

fieldDef = cryRpt.DataDefinition.FormulaFields[param.FieldName]; //cryRpt.Database.Tables[0].Fields[param.FieldName];

getRasSorts = mySortFields.GetType().GetMethod("get_RasSorts", BindingFlags.NonPublic | BindingFlags.Instance);

rasSorts = getRasSorts.Invoke(mySortFields, System.Type.EmptyTypes);

addSort = rasSorts.GetType().GetMethod("Add");

rasAssembly = getRasSorts.ReturnType.Assembly;

ciRasSort = rasAssembly.GetType("CrystalDecisions.ReportAppServer.DataDefModel.SortClass").GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, System.Type.EmptyTypes, null);

rasSort = ciRasSort.Invoke(System.Type.EmptyTypes);

setSortField = rasSort.GetType().GetMethod("set_SortField", BindingFlags.Public | BindingFlags.Instance);

addSort.Invoke(rasSorts, new object[] { rasSort });

cryRpt.DataDefinition.SortFields[param.Index].Field = fieldDef;

cryRpt.DataDefinition.SortFields[param.Index].SortDirection = param.SortDirection;

}

}

}

The removing portion of sorting is working. But when I add new sortfield then those old sort fields are added back. Or is there any other alternative for this problem.

Tags:
Former Member
replied

Please see if the following will help:

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

KBA: 1591686 - How to add sort fields using the Inproc RAS SDK

- Ludek

Senior Support Engineer AGS Product Support, Global Support Center Canada

 

Follow us on Twitter

0 View this answer in context

Helpful Answer

by
Not what you were looking for? View more on this topic or Ask a question