Crystal Reports Online Training

Learn Online, Anytime, Anywhere

Step-by-step online tutorials.

15.01 Changing Record Selection

This chapter builds on the basic report customization shown in Chapter 14. It focuses on working with the DataDefinition and ReportDefinition classes. The DataDefinition class controls Record selection, Grouping and Sorting, and Parameters/Formulas.  The ReportDefinition class handles the functionality for report sections, grouping/sorting and running totals. With respect to the number of objects contained within it, an object variable of the ReportDefinition type is the largest report object by far. It manages the collection objects for all the areas, sections, and report objects within a report.

If you learn best by seeing sample code, this chapter is for you!

Modifying the Record Selection

There are two types of record selection formulas. The first type selects records based upon data that is available during the first-pass stage. This consists of raw data and fields/formulas that don’t use summary information. The second type of formula is a group selection formula that is based on second-pass data (e.g. summary fields and subtotals). Selecting records during the first-pass is done with the RecordsSelectionFormula property. Figure 15-1 shows the properties that modify the record selection formulas.





Figure 15-1. Properties used in selecting records.

The record selection formula is modified in two places: the ReportDocument class and the DataDefinition class. The properties are identical and you can use either class. Setting the group selection is done with the GroupSelectionFormula and it is only found in the DataDefinition class.

The CrystalReportViewer class has a SelectionFormula property that is equivalent to the RecordSelectionFormula property. The viewer doesn’t have a property for setting the group selection formula.

Modifying the record selection formulas is one of the easiest runtime changes to make to a report. The selection formula is a string and all you have to do is assign a new string to it. The formula syntax must be Crystal syntax. Basic syntax isn’t allowed for selection formulas. The following code listings set the formulas to filter on records that are within a certain date range. The listings demonstrate making the change with the DataDefinition class as well as the CrystalReportViewer class.

Listing 15-1. Changing the selection formula via the ReportDocument.
[VB.NET]
Dim myReport As New CrystalReport1()
myReport.DataDefinition.RecordSelectionFormula = "{Orders.Order Date} in Date (1996, 02, 19) to Date (1996, 03, 28)"
CrystalReportViewer1.ReportSource = myReport
[C#]
CrystalReport1 myReport = new CrystalReport1();
myReport.DataDefinition.RecordSelectionFormula = "{Orders.Order Date} in Date (1996, 02, 19) to Date (1996, 03, 28)";
CrystalReportViewer1.ReportSource = myReport;
Listing 15-2. Changing the selection formula with the viewer control.
[VB.NET]
Dim myReport As New CrystalReport1()
CrystalReportViewer1.ReportSource = myReport
CrystalReportViewer1.SelectionFormula = "{Orders.Order Date} in Date (1996, 02, 19) to Date (1996, 03, 28)"
[C#]
CrystalReport1 myReport = new CrystalReport1();
CrystalReportViewer1.ReportSource = myReport;
CrystalReportViewer1.SelectionFormula = "{Orders.Order Date} in Date (1996, 02, 19) to Date (1996, 03, 28)"

I frequently see people mistake how to set the selection formula for string constants. You need to include quotes around any string data that is passed to the selection formula. Since the formula itself is a string, this makes it a little confusing. The easiest way to make this work is to either use single quotes within the string or use the Chr(39) function. I prefer using Chr(39) because putting a single quote next to a double quote makes your code hard to read. Here is an example.

Listing 15-3. Assigning a string constant via the ReportDocument object
[VB.NET]
Dim myReport As New CrystalReport1()
myReport.DataDefinition.RecordSelectionFormula = "{Customer.LastName}=" & Chr(39) & Name & Chr(39)
CrystalReportViewer1.ReportSource = myReport
[C#]
CrystalReport1 myReport = new CrystalReport1();
myReport.DataDefinition.RecordSelectionFormula = "{Customer.LastName}=" + Chr(39) & Name + Chr(39);
CrystalReportViewer1.ReportSource = myReport;