Sorting by FormulaField in Crystal Reports

Viewed 157

I've begun the task of bringing our CR to the 21st century. The templates were created more than 10 years ago, as well as the report generator application written in Visual Basic that allows a user to pick different options before they click a button for generating a report.

So far the process hasn't been too bad but I've stumbled upon something that I can't figure out.

In VB, there's a line of code that sorts the report by a database table field:

Report.RecordSortFields.Add Report.Database.Tables(1).fields.GetItemByName("Name"), crAscendingOrder

I've successfully got that same logic working using the lines below

DatabaseFieldDefinition databaseFieldDefinition = Report.Database.Tables["Bureau"].Fields["Name"];

SortField sortField = Report.DataDefinition.SortFields[0];

sortField.Field = databaseFieldDefinition;

sortField.SortDirection = SortDirection.AscendingOrder;

The other type of sorting is what I'm struggling with. The problem is if that DataDefinition only contains a single SortField using a database tables field, I can't seem to make it order using a formula field. If you iterate through the formula fields you can see multiple fields like @Name, @XX, however if you look in the sort field it looks like it's specifying just a table like 'Assets.Name'

In the original code, this was done simply using the this VB line of code:

Report.RecordSortFields.Add Report.FormulaFields.GetItemByName("AssetName"), crAscendingOrder

For report files that already have a sort field with an @XXX, it works because I'm just replacing the SortField with this new FormulaDefinition that I've got from the below code (that also uses the @XXX), however if the sort field is only using a 'Table.Field' it doesn't work

Report.DataDefinition.FormulaFields["AssetName"];

My obvious first guess is that I need to modify the report file to get this working, however the report files are still allowing sorting via formulafield even in our old VB app, meaning if they could do it back 10+ years ago in vb I should be able to do it in C# without modifying the report.

0 Answers
Related