Crystal Reports Online Training

Learn Online, Anytime, Anywhere

Step-by-step online tutorials.

15.07 Mapping the Grouping and Sorting Objects

Mapping the Grouping and Sorting Objects

To learn how to use the different sorting and grouping classes, let’s start with some generic procedures that traverse the collections in a report and print out the various properties. The code isn’t very complicated because it primarily consists of For Each loops to traverse the collection objects and print out the properties of each object. The important part is to look at which collections manage which objects and see how the object variables are declared.

Listing 15-11. Mapping the sorting and grouping objects.
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim MyReport As New CrystalReport1()
MapSortFields(myReport.DataDefinition.SortFields, 0)
Output("-----", 0)
MapGroups(MyReport.DataDefinition.Groups, 0)
End Sub
Sub MapSortFields(ByVal SortFields As _
CrystalDecisions.CrystalReports.Engine.SortFields, _
ByVal Indent As Integer)
Dim SortField As CrystalDecisions.CrystalReports.Engine.SortField
Output("Total SortFields: " & SortFields.Count, Indent)
For Each SortField In SortFields
MapSortField(SortField, Indent)
Next
End Sub
Sub MapSortField(ByVal SortField As _
CrystalDecisions.CrystalReports.Engine.SortField, _
ByVal Indent As Integer)
MapFieldDefinition(SortField.Field, Indent)
Output("Direction:" & SortField.SortDirection.ToString, Indent + 2)
Output("Type: " & SortField.SortType.ToString(), Indent + 2)
End Sub
Sub MapGroups(ByVal Groups As _
CrystalDecisions.CrystalReports.Engine.Groups, ByVal Indent As Integer)
Dim Group As CrystalDecisions.CrystalReports.Engine.Group
Output("Total Groups: " & Groups.Count, Indent)
For Each Group In Groups
MapGroup(Group, Indent)
Next
End Sub
Sub MapGroup(ByVal Group As CrystalDecisions.CrystalReports.Engine.Group, _
ByVal Indent As Integer)
MapFieldDefinition(Group.ConditionField, Indent)
End Sub
Sub MapFieldDefinition(ByVal FieldDefintion As CrystalDecisions. _
CrystalReports.Engine.FieldDefinition, ByVal Indent As Integer)
Output("Field Name: " & FieldDefintion.Name, Indent)
Output("Formula:" & FieldDefintion.FormulaName, Indent + 2)
Output("Kind: " & FieldDefintion.Kind.ToString(), Indent + 2)
Output("Value Type: " & FieldDefintion.ValueType.ToString(), Indent + 2)
End Sub
Sub Output(ByVal Line As String, ByVal Indent As Integer)
lstMapping.Items.Add(New String(" "c, Indent) & Line)
End Sub

Mapping the properties of the sort objects is done by instantiating the report object and passing the DataDefinition.SortFields collection to the MapSortFields() procedure. This procedure prints the number of sort fields in the collection and then traverses the collection. It calls MapSortFields() which then calls MapSortField() for each object in the SortFields collection. The MapSortField() procedure calls the MapFieldDefinition() procedure to print out the details of the field object. Then it prints the sort direction and whether this is a group sort or a record sort.

Mapping the properties of the group classes is done by instantiating the report object and passing the DataDefinition.Groups collection to the MapGroups() procedure. This procedure prints the number of groups in the report and then traverses the collection. The MapGroups() procedure then calls the MapGroup() procedure for each group object. The Group object has one property, ConditionField, which is of type FieldDefinition. So the MapGroup() procedure only calls the MapFieldDefinition() procedure to print out the properties of the field.

To see the output created by this program, I created a simple report. The report is a sales report that prints the order detail from the Extreme.mdb database. It prints all the orders for each month. The group field is the Order Date and it groups the date by month. The most recent month is printed first, so a descending sort order is used. Within each group, the details of the order for each product are shown. These records are sorted by order date. Thus, the order date is being used in two different ways. The groups are sorted by month in a descending fashion and the detail records are sorted by the individual order date in an ascending order. Figure 15-8 shows the report designer and Figure 15-9 shows the report preview.



Figure 15-8. The Sales Report in the report designer.


Figure 15-9. The preview of the Sales Report.

The sample application is run using this report and the output is shown in Figure 15-10. You can see that the SortFields collection has two items in it: the group field and the sort field. The last property listed for each field tells whether it belongs to a group or is an individual sort record. After listing the sort objects, the program lists the group objects. In this example, the report only has one group. You can see that there isn’t as much information for the group field. Although it tells you about the field that is being grouped on, it doesn’t tell you what direction the sort is. You can only find this out by browsing the SortFields collection.



Figure 15-10. The output of the mapping application.