Crystal Reports Online Training

Learn Online, Anytime, Anywhere

Step-by-step online tutorials.

20.09 Selecting Areas and Sections

Selecting a Report Area and Section

As you learned earlier in the book, a report consists of areas and within each area there can be one or more sections. Each report object has to be located within a specific section. When using the RAS SDK to dynamically add a report object, you have to specify the section that the new report object belongs in.

Adding a report object to the report requires first identifying which area to put it in and which section within that area. Both the areas and sections are stored in a collection that is zero based. So the first area/section has an index of zero and the next will have an index of one. When specifying the section number, it is usually zero because most areas only have one section.

To specify the area number, you could use a number if the number of areas is fixed. But this is more prone to error since it changes based upone how many groups are in a report. Adding a group will re-order the index numbers of all the areas. It is more reliable to use the area name to find its current index number. In the code sample below, the Areas collection is iterated through and the Name property is tested to see if it matches the given area name. If so, it uses the current index number to get a reference to that Area object. To make this work in your own application, you need to know how Crystal Reports names each area. The naming convention is to use the area name followed by the word, “Area”, followed by the number 1. The exception to the rule is groups. Since there can be more than one group in a report, each group uses the next consecutive number available. The first group will use the number 1 and the second group will use the number 2, and so on. The following is a list of the area names in a typical report. It is important to note that these names are case sensitive and that there are no spaces in the name.

ReportHeaderArea1

PageHeaderArea1

GroupHeaderArea1 (optional)

GroupHeaderArea2 (optional)

DetailArea1

GroupFooterArea2 (optional)

GroupFooterArea1 (optional)

PageFooterArea1

ReportFooterArea1

Listing 20-7 is a method that accepts the name of an area and a section number and returns a Section object. This object is used to tell the RAS where you want to put a new report object. The code loops through the Areas collection looking for the area with a matching name. When found, it uses the section number to get a reference to the correct section object within that area.

Listing 20-7. Get a reference to a section object using the area name and section number.
[VB.NET]
Public Shared Function GetSection(ByVal AreaName As String, ByVal SectionNumber As Integer, ByVal rcd As CrystalDecisions.ReportAppServer.ClientDoc.ISCDReportClientDocument) As CrystalDecisions.ReportAppServer.ReportDefModel.Section
Dim mySection As CrystalDecisions.ReportAppServer.ReportDefModel.Section = Nothing
For AreaIndex As Integer = 0 To rcd.ReportDefinition.Areas.Count - 1
If rcd.ReportDefinition.Areas(AreaIndex).Name = AreaName Then
mySection = rcd.ReportDefinition.Areas(AreaIndex).Sections(SectionNumber)
End If
Next
Return mySection
End Function
[C#]
public static CrystalDecisions.ReportAppServer.ReportDefModel.Section GetSection(string AreaName, int SectionNumber, CrystalDecisions.ReportAppServer.ClientDoc.ISCDReportClientDocument rcd)
{
CrystalDecisions.ReportAppServer.ReportDefModel.Section mySection = null;
for (int AreaIndex = 0; AreaIndex < rcd.ReportDefinition.Areas.Count; AreaIndex++)
{
if (rcd.ReportDefinition.Areas[AreaIndex].Name == AreaName)
{
mySection = rcd.ReportDefinition.Areas[AreaIndex].Sections[SectionNumber];
}
}
return mySection;
}