Crystal Reports Online Training

Learn Online, Anytime, Anywhere

Step-by-step online tutorials.

15.04 Programming the ReportDefinition Objects

Programming the ReportDefinition Objects

The ReportDefinition classes are used for modifying all the report objects. With every object, you can modify the formatting properties to change its appearance. Some of the objects, such as the TextObject and FieldObject, can have their content modified. This isn’t the case for every report object. This section shows you how to get a reference to the report object and how to modify its properties.

Before learning the details of the ReportDefinition object, you need to understand the limitations. First of all, the object model is designed so that you can modify the objects, but not add new ones. In other words, none of the collections have an Add() method. For example, if your report has two text objects, you are free to change what they display, but you can’t add any additional text objects.

Secondly, not all properties can be modified. Flip back at Figures 15-2 and 15-3. The properties with a + next to them are read-write. The properties with a – are read-only. As you can see, most properties are read-only. Although you can’t modify everything, many important properties can be changed.

Referencing the Report Objects

To modify any of the objects, declare an object variable of the proper data type and have it reference the report object. Referencing the report object is done by passing the object name to the Item property of the collection and assigning this to the object variable. Of course, use the appropriate collection to get a reference to the object you want. Don’t try to reference a Section object with the Areas() collection.

Listing 15-5. Get a reference to the ReportFooter section.
[VB.NET]
Dim myReport As New CrystalReport1()
Dim mySection As CrystalDecisions.CrystalReports.Engine.Section
mySection = myReport.ReportDefinition.Sections.Item("ReportFooter")
[C#]
CrystalReport1 myReport = new CrystalReport1();
CrystalDecisions.CrystalReports.Engine.Section mySection;
mySection = myReport.ReportDefinition.Sections["ReportFooter"];

Report object variables can be declared as either the ReportObject base class or the classes for the specific object (e.g. TextObject, BoxObject, etc.) The benefit to using the object’s specific class is that you have access to its unique properties. The ReportObject class has generic properties that apply to all the report objects, but not to the specific ones.

Listing 15-6. Get a reference to the report object State using the ReportObject data type.
[VB.NET]
Dim myReport As New CrystalReport1()
Dim myObject As CrystalDecisions.CrystalReports.Engine.ReportObject
myObject = myReport.ReportDefinition.ReportObjects.Item("State")
[C#]
CrystalReport1 myReport = new CrystalReport1();
CrystalDecisions.CrystalReports.Engine.ReportObject myObject;
myObject = myReport.ReportDefinition.ReportObjects["State"];
Listing 15-7. Get a reference to the State object using the object’s specific data type.
[VB.NET]
Dim myReport As New CrystalReport1()
Dim myField As CrystalDecisions.CrystalReports.Engine.FieldObject
myField = CType(myReport.ReportDefinition.ReportObjects.Item("State"), _
CrystalDecisions.CrystalReports.Engine.FieldObject)
[C#]
CrystalReport1 myReport = new CrystalReport1();
CrystalDecisions.CrystalReports.Engine.FieldObject myField;
myField = (CrystalDecisions.CrystalReports.Engine.FieldObject) myReport.ReportDefinition.ReportObjects["State"];

Referencing a section can be a little tricky because you might use the wrong name for it. When you look at the report in design mode, a section appears to have two names. The first name is a description of the section (e.g. Page Header, Details, etc.) but this isn’t the name that it’s referenced by. It’s simply a description so that you know what the purpose of the section is. Right after the description is the name enclosed in parentheses: Page Header (Section2). In this example, Section2 is the name that you should use when referencing the section from the Sections collection. You will also see the section name listed in the properties window under the Name property.