I needed code for dynamically selecting the TopN/Sort Group Expert in crystal report. I am using VB.net and SQL. My report has its data coming from a stored procedure written.
This will be of great help as i needed to give the option for my top sales report.

I've got the same problem here. I've been looking into doing the same thing and haven't found any solutuions. I've got 3 different fields that I want the user to be able to sort by. I also would like them to have the option of entering a parameter to limit the results displayed. Of course this can all be easily done during design time with 'TopN/Sort Group Expert', but how do you sort a Sum field during runtime.

I've tried using sort fields but I can't get beyond an "Invalid field name" error.

Dim crSortField As SortField
        Dim fieldDef As DatabaseFieldDefinition
        For Each crSortField In crReportDocument.DataDefinition.SortFields
            If crSortField.Field.Name.ToString = strField Then
                fieldDef = crReportDocument.Database.Tables(0).Fields(crSortField.Field.Name.ToString) 'where error occurs 
                crSortField = crReportDocument.DataDefinition.SortFields(0)
                crSortField.Field = fieldDef
                crSortField.SortDirection = SortDirection.DescendingOrder
            Else
               'used for debuging
                Label1.Text += " " & crSortField.Field.Name.ToString & ", " 
            End If
        Next
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.