Fields within a report represent data mapping between a data source and the template of the report. FlexReport allows you to add an in-built field as well as a custom field. To add fields in FlexReport, you simply need to use the Fields group of Insert tab in FlexReportDesigner, and click the desired field and drop it into the report.
You are not restricted to using VBScript to evaluate expressions in calculated fields. You can also specify scripts that are triggered when the report is rendered, and you can use those to change the formatting of the report.These scripts are contained in event properties. An event property is similar to a Visual Basic event handler, except that the scripts are executed in the scope of the report rather than in the scope of the application that is displaying the report.
For example, you can use an event property to set a field's Font and ForeColor properties depending on its value. This behavior would then be a part of the report itself, and would be preserved regardless of the application used to render it.
Of course, traditional events are also available, and you should use them to implement behavior that affects the application rather than the report. For example, you could write a handler for the StartPage event to update a page count in your application, regardless of which particular report is being rendered.
The following table lists the event properties that are available and typical uses for them:
Object | Property | Description |
---|---|---|
C1FlexReport | OnOpen | Fired when the report starts rendering. Can be used to modify the ConnectionString or RecordSource properties, or to initialize VBScript variables. |
OnClose | Fired when the report finishes rendering. Can be used to perform clean-up tasks. | |
OnNoData | Fired when a report starts rendering but the source recordset is empty. You can set the Cancel property to True to prevent the report from being generated. You could also show a dialog box to alert the user as to the reason why no report is being displayed. | |
OnPage | Fired when a new page starts. Can be used to set the Visible property of sections of fields depending on a set of conditions. The control maintains a Page variable that is incremented automatically when a new page starts. | |
OnError | Fired when an error occurs. | |
Section | OnFormat | Fired before the fields in a section are evaluated. At this point, the fields in the source recordset reflect the values that will be rendered, but the report fields do not. |
OnPrint | Fired before the fields in a section are printed. At this point, the fields have already been evaluated and you can do conditional formatting. |
In the following sections, you can explore the typical uses for these properties in detail.
Formatting a field according to its value is probably the most common use for the Section.OnPrint property. Take for example a report that lists order values grouped by product. Instead of using an extra field to display the quantity in stock, the report highlights products that are below the reorder level by displaying their name in bold red characters.
To highlight products that are below the reorder level by displaying their name in bold red characters, use an event script that looks like this:
C# |
Copy Code
|
---|---|
string script = "if (UnitsInStock& ReorderLevel) then\r\n" + "ProductNameCtl.ForeColor = rgb(255,0,0)\r\n" + "ProductNameCtl.Font.Bold = true\r\n" + "else\r\n" + "ProductNameCtl.ForeColor = rgb(0,0,0)\r\n" + "ProductNameCtl.Font.Bold = false\r\n" + "end if\r\n"; c1FlexReport1.Sections.Detail.OnPrint = script; |
Alternatively, instead of writing the code, you can use the C1FlexReportDesigner application to type the following script code directly into the VBScript Editor of the Detail section's Section.OnPrint property. Complete the following steps:
If UnitsInStock < ReorderLevel Then
ProductNameCtl.ForeColor = RGB(255,0,0)
ProductNameCtl.Font.Bold = True
Else
ProductNameCtl.ForeColor = RGB(0,0,0)
ProductNameCtl.Font.Bold = False
End If
The control executes the VBScript code whenever the section is about to be printed. The script gets the value of the "ReorderLevel" database field and sets the "ProductName" report field's Field.Font.Bold and Field.ForeColor properties according to the value. If the product is below reorder level, its name becomes bold and red.
The following screen capture shows a section of the report with the special effects:
You can change a report field's format based on its data by specifying an expression for the Detail section's OnFormat property.
For example, your Detail section has fields with an image control and when there is no data for that record's image you want to hide the record. To hide the Detail section when there is no data, in this case a record's image, add the following script to the Detail section's OnFormat property:
If isnull(PictureFieldName) Then
Detail.Visible = false
Else
Detail.Visible = true
End If
To hide a section if there is no data, in this case a record's image, for it, use an event script that looks like this:
C# |
Copy Code
|
---|---|
c1FlexReport1.Sections.Detail.OnFormat = "Detail.Visible = notisnull(PictureFieldName)";
|
Alternatively, instead of writing the code, you can use the C1FlexReportDesigner to type the following script code directly into the VBScript Editor of the Detail section's OnFormat property. Complete the following steps:
If isnull(PictureFieldName) Then
Detail.Visible = false
Else
Detail.Visible = true
End If
Detail.Visible = not isnull(PictureFieldName)
Instead of changing the field format to highlight its contents, you could set another field's Visible property to True or False to create special effects. For example, if you inserted a new field Shape field named "Shapefld" around the product name and set its Shape property to True, then you could write the script as follows:
If UnitsInStock < ReorderLevel Then
Shapefld.Visible = True
Else
Shapefld.Visible = False
End If
To highlight products that are below the reorder level by displaying a box, use an event script that looks like this:
C# |
Copy Code
|
---|---|
string script = "if (UnitsInStock < ReorderLevel) then\r\n" + "BoxCtl.Visible = true\r\n" + "else\r\n" + "BoxCtl.Visible = false\r\n" + "end if\r\n"; c1FlexReport1.Sections.Detail.OnPrint = script; |
The code builds a string containing the VBScript event handler, and then assigns it to the section's OnPrint property.
To highlight products that are below the reorder level using FlexReportDesigner:
Alternatively, instead of writing the code, you can use the C1FlexReportDesigner application to type the following script code directly into the VBScript Editor of the Detail section's OnPrint property. Complete the following steps:
If UnitsInStock < ReorderLevel Then
Shapefld.Visible = True
Else
Shapefld.Visible = False
End If
The following screen capture shows a section of the report with the special effects:
The C1FlexReport.Page variable is created and automatically updated by the control. It is useful for adding page numbers to page headers or footers. In some cases, you may want to reset the page counter when a group starts. For example, in a report that groups records by country. You can do this by adding code or using the Designer.
To reset the page counter when a group (for example, a new country) starts, set the PageFooter field's Text property. Enter the following code:
C# |
Copy Code
|
---|---|
c1FlexReport1.Fields["PageFooter"].Text = "[ShipCountry] + [Page]"; |
To reset the page counter when a group (for example, a new country) starts, set the PageFooter field's Text property by completing the following steps:
="Page " & GroupPage(0) & " of " & GroupPages(0) & " for " & Country