[Internal power method of Crystal Report]-formula, function, and total Runtime

Source: Internet
Author: User
The topic of this article is: Functions and formulas, total Runtime.
This article focuses on the basics and will be used in some practical scenarios.

1: Formula
The formula can be said to be the essence of the crystal report, and its importance is a bit like the macro of Excel.
Formulas can be divided into two types: formatting. First, it is an operation class. Of course, formatting formulas also require calculation.
Some built-in functions of Crystal Reports depend on formulas, but are encapsulated.
There are two types of syntax for formulas: crystal syntax and Basic syntax. There is no fundamental difference.
If you have used a Basic language before, this is very easy.
You can download an independent version of the crystal report help document for easy reference.
Cr xi crystal report development official Chinese help document

The format class appears in the following locations:
For example, we need to set the value of 12 in a column to blue, the value of 15 to red, and the rest to black.
Right-click a field and format it,

In this figure (x + 2), there is a formula behind it. If there is no content in it, this icon is the default color.
If there is content in it, the icon turns red.

The sample data in this article continues the previous example.

Instance 1. If the user score is 12, the user score is displayed in blue. If the user score is 15, the user score is displayed in blue. Otherwise, the user score is displayed in black.
Click(X + 2)In the left-side navigation pane.

I grabbed a big image and marked it.
The formula itself is relatively simple and does not need to be explained.
Note the two points in the upper left corner of the preceding figure. x + 2 is used to check whether the formula is correct and stay on the current page after the check.
To save and close the interface, check and save the formula and close the interface, and return to the previous interface.

Okay, let's run the program again, and the effect will come out.

Instance 2: controls the page feed of each 3 Records

Enter expert


Click "details section", select "create page after", and then edit the formula

If the formula is incorrect, a prompt similar to the following will appear. Of course, the prompts vary according to the error.

Preview the page. The data on the first page is displayed on multiple pages. Note that the above 1 + indicates that the number of pages is more than one page.

However, the actual page number is not displayed. This is an optimization mechanism of the crystal report. For multi-page data, only the first page is displayed.
Similar to some of our database paging operations. However, this also brings a lot of criticism.
In fact, there are some solutions.
That's it.

C # code
   CrystalReportViewer1.ReportSource = myReport;CrystalReportViewer1.ShowLastPage();CrystalReportViewer1.ShowFirstPage();

In fact, it also simulates one of our manual actions, that is, first click to jump to the last page, and then switch to the first page.
This action is unacceptable to users.

The preceding formula is the formula of two formatting classes. The following describes the formula of the operation class.

Instance 3. If the score is less than 15, it indicates that the score is unqualified. If the score is equal to 15, it indicates that the score is qualified. If the score is greater than 15, it indicates that the score is excellent.
This is the re-processing of data.

Create a new formula field x1

Drag x1 to the interface and put it behind the original data. The preview will be displayed.

Example 4: Modify the formula in the code
Create a new formula field x2 and put an existing field in it.

The display effect is as follows:

Well, you don't feel anything.
Create a formula field named x2title as the topic of the column x2.


Then control in the Code:

C # code
   MyReport. DataDefinition. FormulaFields ["x2"]. Text = "{RPT_CR_TEST1.Scores}"; myReport. DataDefinition. FormulaFields ["x2title"]. Text = "'score '";

Note that if the original formula returns a string, enclose the quotation marks.
MyReport. DataDefinition. FormulaFields ["x2title"]. Text = "'score status '";

View results

This field that can be changed in real time is of great use.
It should be noted that, when grouping, charts, and cross tabulation, you can use the formula field as the calculation field.
If we control the field changes in the formula in the code, it will naturally achieve dynamic grouping, dynamic charts, and other effects.
For similar applications, see:
Display of arbitrary selection of specified fields in the Crystal Report-template and C # upgraded version
Using formulas to dynamically set the axis data items of a chart

This is a single formula, which can be nested between formulas. Sometimes it can be combined with global variables for multi-formula collaboration.
These will be discussed in some practical application scenarios.

2: Total running time

We know that the report has built-in summary functions such as sum and count. But how can we find only the sum of some data?

Instance 5:
Calculate the sum of all scores greater than 15, the number of persons greater than 15, and the average value.

Create a new runtime total field Rtotal0, which is used to obtain the score and

Note that in the "evaluate" position, edit the following formula:

Create a new runtime total field Rtotal1, which is used to obtain the number of people

Note that this formula is also used, so (x + 2) is red and the formula content is the same as Rtotal0.
Because they are all the same rules.

Create a new formula field x3,

C # code
   // Define a string variable stringvar s1; // set it to a numeric variable numbervar n1; s1: = 'average score of all people whose score is greater than 15 '; // For the If Else logic, the data types returned by each branch logic should be the same // for protection, prevent the denominator from being 0if {# RTotal1} = 0 thens1 + '-' else // a branch logic contains multiple statements, which can be enclosed in parentheses, this makes it clearer (// calculate the average n1 :={# RTotal0}/{# RTotal1}; s1 + cstr (n1, 2 );)

This formula is a typical small model, where variables are used.
Essentially, it is not much different from the variables we use in the program.
Because the denominator may be 0 when the average value is obtained, a protection is implemented.
Note that the default syntax of the formula is Crystal. The value assignment method is x: = 5, and x = 5, which is a logical judgment.
(Sometimes it can be mixed, but it is best to follow the rules)

Let's take a look at the actual running effect.

The total running time is a kind of data processing when the data is displayed from top to bottom,
His data can only go from top to bottom and process some of the data according to certain rules.
It must appear below the data (or in parallel with the data), but not before the data, like other Aggregate functions such as Max and Sum.
Essentially, the total Runtime is a special application of formulas.

3: Functions

The function is actually based on the formula, just like our normal program. If there are many repeated formulas, they can be extracted into functions for reuse.
We have transformed the previous formula x1.
You cannot directly enter the function editing interface on the template. You must first enter the formula editing interface.
Create a function, setTitle


Use the editor first,
Copy the current formula directly, annotate it as a reference, and actually modify it.
When you click Save, an error is reported. Fields cannot be directly used in functions.

Let's make a variable x to pick up the possible input values, which may be fields or other

Then we get rid of formula x1.

You can still achieve the same effect.

On the previous page, we can see another "use extractor" button.
Let's try again.
Create a new function setTitle2 and click "use extract ".


This is to extract the existing formula into a function, which is very convenient.
We select x1 (to facilitate the demonstration, I deleted the function and restored it to the original formula). At this time, the tool automatically sets the database field as a parameter.
Click OK and the function is created. You can edit the function as needed.

Others:

Formulas can be combined with parameters to achieve more complex results.
The function is based on the formula, and the formula can directly call the function.
Functions can call each other.
You cannot use the report parameter field (parameterField) or database Field in a function.
The formula and the total number of running operations can be used.

The formulas in this article are relatively basic and start to enter a more complex stage later.
Mainly grouping, charts, subreports, and cross tabulation. The processing method of these parts is slightly different from the previous one.
In addition to basic applications, some actual scenarios will be combined, and some of the content described earlier will be further discussed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.