Crystal Reports Online Training

Learn Online, Anytime, Anywhere

Step-by-step online tutorials.

14.05 Where to Put Your Code

Locating the Report Code

The previous code listings only show you the steps for previewing/printing the reports in a Windows form. But they don’t show you where you should put the code in your form. You can put this code anywhere in the form and it will work fine. For example, if you want to show the report when the form opens, put the code in the Load() event. There are times that you are going to call this code after the user has entered various data that specifies how the report should be customized. If that’s the case, you could put the code in response to the click event of an OK button that confirms they are finished inputting data. The following code sample demonstrates putting it in the Load() event.

Listing 14-5. A template for modifying reports.
[VB.NET]
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim myReport As New CrystalDecisions.CrystalReports.Engine.ReportDocument()
myReport.Load("CrystalReport1.rpt")
CrystalReportViewer1.ReportSource = myReport
End Sub
[C#]
private void Form1_Load(object sender, EventArgs e)
{
CrystalDecisions.CrystalReports.Engine.ReportDocument() myReport = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
myReport.Load("CrystalReport1.rpt");
myReport;.PrintToPrinter(2, True, 0, 0);
}

All report customization must be done prior to previewing or printing the report. No changes are allowed once the report is generated. For example, you can’t use .NET to change the way a field is formatted depending upon the current group value. Making dynamic report changes while the report is running requires writing formulas and using conditional formatting (discussed in Chapters 7, 8, and 9).