[IT168 technical documentation]
Cross-report is a common report type, and it is also cumbersome to develop. In ActiveReport, it provides sufficient flexibility for cross-report, this allows you to cope with various complex business logic. This article briefly introduces how to create a cross report.
A common application of cross-Report is to display sales reports. For example, to show sales of multiple chain stores per month within one year, the month is usually displayed as a column, each store is represented by a row:
| Store name |
January |
February |
March |
............... |
| AC |
500 |
200 |
10000 |
............... |
| BC |
511 |
85245 |
4545 |
............... |
However, the following methods are often used for database storage:
| Sales |
Month |
Shop |
| 12312 |
1 |
AB |
| 243423 |
2 |
AB |
| 323232 |
3 |
AB |
| 1231312 |
1 |
BC |
| 1232 |
2 |
BC |
In this way, we need to process the data before the display and convert the sales volume and month to the column. We can use SQL to perform these operations when retrieving data. Here, to demonstrate the use of activeReport, place the conversion into the report. To simplify the example, we only show the monthly sales for the first quarter.
1. Data retrieval:
Use SQL statements such as Select Sales, Month, Shop from CrossReport Order by Shop to directly retrieve data without any aggregate or conversion processing.
2. Conversion:
Let's define a simple object to indicate the records to be displayed:
Public class Sale {public decimal money1; // public decimal money2 in December January; // public decimal money3 in December February; // public string shopname in December March ;}
At the same time, define a Sale set sales to save the converted data.
Since each store in the table corresponds to multiple records, to merge multiple records into one, perform the following conversion:
// Stores the computed store and ensures that each store has only one record, ArrayList shopname = new ArrayList (); while (dr. Read () {if (! Shopname. contains (dr. getString (2) // The first record of the store {Sale s = new Sale (); s. shopname = dr. getString (2); // name shopname. add (s. shopname); if (dr. getInt32 (1) = 1) // January {s. money1 = dr. getDecimal (0);} else if (dr. getInt32 (1) = 2) // February {s. money2 = dr. getDecimal (0);} else if (dr. getInt32 (1) = 3) // March {s. money3 = dr. getDecimal (0);} sales. add (s);} else // not the first record of the store {Sale s = (Sale) sale [sales. count-1]; if (dr. getInt32 (1) = 1) {s. money1 = dr. getDecimal (0);} else if (dr. getInt32 (1) = 2) {s. money2 = dr. getDecimal (0);} else if (dr. getInt32 (1) = 3) {s. money3 = dr. getDecimal (0 );}}}
3. indicates:
The above is to convert the records retrieved from the database to the format to be displayed on the report. The data in the Sales set will be displayed in the report. We can follow the methods described in the previous articles:
Place the control on the interface and set its FiledName Field
Set the Filed set in the DataInitialize event of the report to retrieve the data:
This. fields. add ("money1"); this. fields. add ("money2"); this. fields. add ("money3"); this. fields. add ("shopname"); this. getReportData (); // retrieves and converts data.
Set a flag to indicate whether the last record is displayed:
int index = 0;
Display Data in the Sales set in the FetchData event:
If (index = sales. count) // if the last record is reached, the {eArgs. EOF = true; return;} else {eArgs. EOF = false;} Sale s = (Sale) sales [index]; this. fields ["shopname"]. value = s. shopname; this. fields ["money1"]. value = s. money1; this. fields ["money2"]. value = s. money2; this. fields ["money3"]. value = s. money3; index + = 1;
According to the above steps, the main code has been completed. Of course, you need to display the code on the form, add a Viewer, and specify the loaded report:
ActiveReports1 rpt = new ActiveReports1();rpt.Run();this.viewer1.Document = rpt.Document;
If you are not satisfied with the display effect, you can add a line frame to the report to display it as a table.
Summary:
The code in this example is repeated, but it is not optimized to illustrate the conversion process. In addition, we can see that ArrayList is used in the Code and the action of packing and unpacking appears, therefore, there is still room for optimization of performance.
The conversion from table data to display data can be performed in SQL statements. However, when the business logic is complex, SQL statements cannot help you. For example, you can display the monthly data, as well as the income and expenditure, if you add unpredictable business logic such as tax, discount, loss, rent, and comparison with the same period of the previous year, if you write SQL in the Code and debug it into a problem, if you write it as a stored procedure, the encapsulation is damaged. Therefore, the conversion in the Code is complicated, but it is flexible.