Writing Code
 Crystal Reports Forum : Crystal Reports for Visual Studio 2005 and Newer : Writing Code
Message Icon Topic: Sorting reports dynamically Post Reply Post New Topic
Author Message
Ontario
Newbie
Newbie


Joined: 27 Sep 2007
Online Status: Offline
Posts: 3
Quote Ontario Replybullet Topic: Sorting reports dynamically
    Posted: 27 Sep 2007 at 2:28am
My application, written in VB.NET 2005, uses reports designed in CR XI.
 
The user should be able to re-sort certain areas of the reports. Thus opens up 2 important questions:
 
1. How can I add SortFields programatically? The SortFields-Collection doesn't provide any Add-method...
 
2. Using predefined sortfields (defined in CR XI) in subreports reveals another problem: The subreports are resorted in background but aren't updated in the CRViewer. How do I force the subreports to be updated?
 
I tied something like this:
 

Dim rptSubBericht As ReportDocument = rptBericht.OpenSubreport(rptBericht.Subreports(intReport).Name)

If rptSubBericht.Database.Tables(0).Location.Substring(rptSubBericht.Database.Tables(0).Location.LastIndexOf(".") + 1) = View Then

For intFeld As Integer = 0 To 2

Dim sfdFeld As SortField = rptSubBericht.DataDefinition.SortFields(intFeld)

With sfdFeld

If Not SortierFelder(intFeld) Is Nothing Then

.Field = rptSubBericht.Database.Tables(0).Fields(SortierFelder(intFeld))

End If

.SortDirection = SortDirection.AscendingOrder

End With

Next

CrystalReportViewer1.ReportSource = rptBericht

This does not(!) update the subreports within the Viewer.
 
Thx
Ontario
IP IP Logged
hilfy
Admin Group
Admin Group
Avatar

Joined: 20 Nov 2006
Online Status: Offline
Posts: 3702
Quote hilfy Replybullet Posted: 27 Sep 2007 at 2:43pm

When I need this type of functionality, I set up a parameter that says which field is sorted.  This can be a plain text parameter that gets translated in a formula.  I then sort on the formula in the report.  It looks something like this:

If {?SortOrder} = 'Loan Number' then
  {LOAN.LOAN_NUMBER}
else if {?SortOrder} = 'Posting Date' then
  ToText({LOAN.LOAN_POSTING_DATE}, 'yyyyMMdd')
else if {?SortOrder} = 'Loan Rematch Date' then
  ToText({LOAN.REMATCH_DATE}, 'yyyyMMdd')
else //default value
  {LOAN.LOAN_NUMBER}

If your users are going to be able to specifiy ascending or descending order, you'll have to set the SortDirection in your code, though.
 
-Dell
IP IP Logged
Ontario
Newbie
Newbie


Joined: 27 Sep 2007
Online Status: Offline
Posts: 3
Quote Ontario Replybullet Posted: 01 Oct 2007 at 1:03am
Thanks for that idea, but this wont work in my case. Users should be able to select up to 3 out of 99 properties to sort. Neither your mentioned if block nor a case structure would be suitable for that. And in addition my reports contain up to 3 subreports - I would get lost in handling all those parameters.
 
Isn't there any way to get a subreport rebuild after binding it to a Viewer.ReportSource? (If I sort the subreport before(!) setting the ReportSource property it's perfectly sorted, though.)
IP IP Logged
Ontario
Newbie
Newbie


Joined: 27 Sep 2007
Online Status: Offline
Posts: 3
Quote Ontario Replybullet Posted: 01 Oct 2007 at 7:48am
Ok, the workaround I found myself to rebuild subreports during runtime as well is to use a second, new report temprorarily:
 
Dim rptTmp As New ReportDocument
rptTmp.Load(strReportPath & "\ReportWithSubReports.rpt")
 
'Code to configure the report and its subreports goes here
 
rptDisplayedReport = rptTmp
CrystalReportViewer1.ReportSource = rptDisplayedReport
 
This isn't a nice coding style but helps for the moment.
IP IP Logged
Joe_G
Newbie
Newbie


Joined: 01 Feb 2008
Location: Canada
Online Status: Offline
Posts: 2
Quote Joe_G Replybullet Posted: 03 Feb 2008 at 9:06pm

' VB 2005/Crystal XI 2008

' my reports has three fields Lastname, Firstname, address

' in my report I have added the address as my default sort field

' the following code allows me to change the sort field to another field.

' if I try to sort on address I get an error "sort field already exists.

' if I remove the default sort filed my code gives me an error.

' my problem now is how can I sort on multiple fields ????

 

 

Dim crReportDocument As New CrystalDecisions.CrystalReports.Engine.ReportDocument

Dim sDatabaseFile As String = "c:\Data\DatabaseFile.mdb"

Dim sTableName As String = "myTable"

Dim crDatabaseFieldDefinition As CrystalDecisions.CrystalReports.Engine.DatabaseFieldDefinition

crReportDocument.Load("C:\reports\myReport.rpt")

crReportDocument.DataSourceConnections.Item(0).SetConnection(sDatabaseFile, sTableName, False)

crDatabaseFieldDefinition = crReportDocument.Database.Tables.Item(0).Fields.Item("LastName")

crReportDocument.DataDefinition.SortFields.Item(0).Field = crDatabaseFieldDefinition

crReportDocument.DataDefinition.SortFields(0).SortDirection = CrystalDecisions.Shared.SortDirection.DescendingOrder

CrystalReportViewer1.ReportSource = crReportDocument

IP IP Logged
Post Reply Post New Topic
Printable version Printable version

Forum Jump
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum



This page was generated in 0.027 seconds.