This section shows how you can organize the data in your report by grouping and sorting data, using running sums, and creating aggregate expressions.
Grouping Data:
After designing the basic layout, you may decide that grouping the records by certain fields or other criteria would make the report easier to read. Grouping allows you to separate groups of records visually and display introductory and summary data for each group. The group break is based on a grouping expression. This expression is usually based on one or more recordset fields but it can be as complex as you like.
You can group the data in your reports using the C1ReportDesigner application or using code:
Adding grouping and sorting using C1ReportDesigner
Groups are also used for sorting the data, even if you don't plan to show the Group Header and Footer sections. You can add groups to your report using the C1ReportDesigner application.
To add or edit the groups in the report, complete the following steps:
Here's what the Sorting and Grouping dialog box should look like at this point:
If you add more fields, you can change their order using the arrow buttons on the right of the Groups list. This automatically adjusts the position of the Group Header and Footer sections in the report. To delete a field, use the Delete button.
Once you are done arranging the fields, click OK to dismiss the dialog box and see the changes in the Designer. There are two new sections, a Header and Footer for the new group. Both have height zero at this point, you can expand them by dragging the edges with the mouse. Notice that the Group Header section is visible, and the footer is not. This is because the Group Header button in the dialog box has been checked, and the Group Footer button was left unchecked. Invisible sections are displayed with a hatch pattern to indicate they are invisible.
On the tan bars that mark the top of the new sections there are labels that contain the section name and the value of the group's GroupBy property.
To see how groups work, click the Text button , select Country from the dropdown menu and mark an area in the newly created Group Header Section. Click the new field to select it and change its Font property to make the new field stand out a little.
Adding grouping and sorting using code
Useful reports don't simply show data, they show it in an organized manner. C1Report uses groups to group and sort data. To demonstrate how this works, return to the Creating a Report Definition topic's code and group the employees by country.
The following code creates a group object that sorts and groups records by country:
To write code in Visual Basic
Visual Basic |
Copy Code
|
---|---|
If chkGroup.Checked Then ' group employees by country, in ascending order Dim grp As Group grp = c1r.Groups.Add("GrpCountry", "Country", SortEnum.Ascending) ' format the Header section for the new group With grp.SectionHeader .Height = 500 .Visible = True f = .Fields.Add("CtlCountry", "Country", 0, 0, c1r.Layout.Width, 500) f.Calculated = True f.Align = FieldAlignEnum.LeftMiddle f.Font.Bold = True f.Font.Size = 12 f.BorderStyle = BorderStyleEnum.Solid f.BorderColor = Color.FromArgb(0, 0, 150) f.BackStyle = BackStyleEnum.Opaque f.BackColor = Color.FromArgb(150, 150, 220) f.MarginLeft = 100 End With ' sort employees by first name within each country c1r.Groups.Add("GrpName", "FirstName", SortEnum.Ascending) End If |
To write code in C#
C# |
Copy Code
|
---|---|
if (chkGroup.Checked) { // group employees by country, in ascending order Group grp = c1r.Groups.Add("GrpCountry", "Country", SortEnum.Ascending); // format the Header section for the new group s = grp.SectionHeader; s.Height = 500; s.Visible = true; f = s.Fields.Add("CtlCountry", "Country", 0, 0, c1r.Layout.Width, 500); f.Calculated = true; f.Align = FieldAlignEnum.LeftMiddle; f.Font.Bold = true; f.Font.Size = 12; f.BorderStyle = BorderStyleEnum.Solid; f.BorderColor = Color.FromArgb(0, 0, 150); //f.BackStyle = BackStyleEnum.Opaque; f.BackColor = Color.Transparent; f.BackColor = Color.FromArgb(150, 150, 220); f.MarginLeft = 100; // sort employees by first name within each country c1r.Groups.Add("GrpName", "FirstName", SortEnum.Ascending); } |
Every group has Header and Footer sections. These are invisible by default, but the code above makes the Header section visible to show which country defines the group. Then it adds a field with the country. The new field has a solid background color.
Finally, the code adds a second group to sort the employees within each country by their first name. This group is only used for sorting, so the Header and Footer sections remain invisible.
The changes are now complete. To render the new report, you need to finish the routine with a call to the method. Enter the following code in the btnEmployees_Click event handler:
To write code in Visual Basic
Visual Basic |
Copy Code
|
---|---|
' render the report into the PrintPreviewControl ppv.Document = c1r |
To write code in C#
C# |
Copy Code
|
---|---|
// render the report into the PrintPreviewControl ppv.Document = c1r; |
Here's an example of a report using a group object:
Sorting Data:
You can sort data in reports the following two ways:
Group sorting is done using the DataView.Sort property, which takes a list of column names only (not expressions on column names). So if your grouping expression is DatePart("yyyy", dateColumn), the control will actually sort on the dates in the dateColumn field, not on the years of those dates as most would expect.
To sort based on the dates, add a calculated column to the data table (by changing the SQL statement), and then group/sort on the calculated column instead. See the Sort property for an XML discussion of this, including a sample.
This is what the Sorting and Grouping editor looks like in the C1ReportDesigner application. Note the fields where you can specify group sorting:
If you use both approaches, the sorting set in the report groups will prevail (it is applied after the data has been retrieved from the database).