Monday, December 12, 2011

Changing Crystal Report Sort Order Programatically

Problem : - To sort crystal report programatically

Solution : -
Every report can have many types of fields that are used to generate the report, but don't have to appear directly on the report. Some examples are grouping fields, parameter fields, and formula fields. Even though these fields may not be shown directly on the report, they are updateable during runtime and can be used to change the report’s appearance. For example, you can change the sorting order of the report so that the report sorts and summarizes in a new way. The DataDefinition class manages the collections that control these aspects of the report.

You must already have the report sorted by something, or you will get error "Invalid sort number". If you haven't already, store a default sort order within your report, using Record Sort Expert from the Report menu


CrRptSrc is the Crystal Report Source

// Load Crystal Report
CrRptSrc.ReportDocument.Load(Report Path)

// Set parameters if any

// Set Record Selection Formula if any
CrRptSrc.ReportDocument.RecordSelectionFormula = "RSF"

// Sorting
Dim sortfield As CrystalDecisions.CrystalReports.Engine.FieldDefinition

sortfield = CrRptSrc.ReportDocument.Database.Tables("TableName").Fields("FieldName")

CrRptSrc.ReportDocument.DataDefinition.SortFields(index).Field = sortfield

CrRptSrc.ReportDocument.DataDefinition.SortFields(index).SortDirection = CrystalDecisions.Shared.SortDirection.AscendingOrder

// Report Logon information
CrRptSrc.ReportDocument.SetDatabaseLogon(userid , password)

// Print Report as PDF
CrRptSrc.ReportDocument.ExportToHttpResponse(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, Response, False, "")

Digg Google Bookmarks reddit Mixx StumbleUpon Technorati Yahoo! Buzz DesignFloat Delicious BlinkList Furl

0 comments : on " Changing Crystal Report Sort Order Programatically "