Devexpress. xtrareports report, which dynamically sets the report layout

Source: Internet
Author: User

The dynamic development report method described in this article has been encapsulated into DLL and is now published externally. Please click here.

Introduction
I was responsible for the report last time and said that I could no longer make reports like my previous project. I used to write more than 300 reports, one painting, and one writing and storing process in the past, which caused a huge workload, of course, there are not so many project reports. In this case, my job is to save the workload. After analysis, I come to two conclusions: first, the data source of the report must beProgramMembers can get it by themselves. It is impossible to "CREATE" data sources based on complicated businesses as they wish to be a report designer in a short time. Second, the layout of reports is complex and changeable, even if the workload is reduced, it cannot avoid assigning values to a complex variable.
Start thinking
Each report consists of three parts: the paging header data, the detailed data, and the footer data. Both the header data and footer data are a record. The value of each field is displayed after its title, for example, name: ×××. There are many detailed data, and the display format is as follows:

Title 1 Title 2 Title 3
1 A Zhang
2 B Li

The number of columns displayed in each row can be customized. If the number of columns of the defined page header data is 3, and the page header data has five fields, the number of columns displayed in the next row must be displayed; at the same time, the number of columns occupied by each field can also be customized. If the page header data has five fields, each field occupies 1, 2, 1, 1, and 1 columns, three columns are displayed in each row:

Name: XXX Address :*****************
Gender: male Nationality: ×× Date of birth: ×××

/// <Summary>
/// Number of page header columns (3 by default)
/// </Summary>
PrivateInt mheadertablecolumncount = 3;
/// <Summary>
/// Number of columns in the detail table (10 by default)
/// </Summary>
PrivateInt mdetailtablecolumncount = 10;
/// <Summary>
/// The number of footer columns (5 by default)
/// </Summary>
PrivateInt mfootertablecolumncount = 5;
I think the most difficult problem is the location and size of each field to be displayed and the layout problem such as what data structure is used for storage. I used a three-dimensional array, which is described as follows:
/// <Summary>
/// Array indicating the report layout [I] [J] [k]
/// I = 0: Page head layout, I = 1: Details layout, I = 2: footer Layout
/// J: field displayed in sequence (j = 0: report title field)
/// K = 0: field name, k = 1: title, K = 2: column span, K = 3: Format String (optional)
/// </Summary>
Public String [] [] [] reportlayout = NULL;
All data is stored in one dataset. The first table in the dataset is the page header data, the second is the detailed data, and the third is the footer data.
/// <Summary>
/// Report data source, 0-page header Table 1 List table 2 footer table
/// </Summary>
Public dataset MDS = NULL;
Start work
The next step is to define each Xrtable (table ),Xrtablerow (ROW ),Xrtablecell (column), and set their location and size attributes based on the value of the reportlayout array. The layout of detailed data is put here.Code(Always useReportlayout [1]Here, 1 refers to the details, if the page header data is 0 ).

If (Mreportlayout. Length >   1 )
{
Columnwidth = Pagewidth / Mdetailtablecolumncount;
Tempheight =   0 ;
For (Tempcount =   0 , I =   0 ; I < Reportlayout [ 1 ]. Length; I ++ )
Tempcount + = Convert. toint32 (reportlayout [ 1 ] [I] [ 2 ]);
Xrtable detailtable =   New Xrtable ();
Detailtable. Location =   New System. Drawing. Point ( 0 , Tempheight );
Detailtable. Size =   New System. Drawing. Size (pagewidth,
Mdetailrowheight * (Tempcount + Mdetailtablecolumncount -   1 ) / Mdetailtablecolumncount );
Detailtable. Borders = (Devexpress. xtraprinting. borderside) (devexpress. xtraprinting. borderside. Left | Devexpress. xtraprinting. borderside. Right) | Devexpress. xtraprinting. borderside. Bottom );
For (J =   0 , I =   0 ; J < (Tempcount + Mdetailtablecolumncount -   1 ) / Mdetailtablecolumncount; j ++ )
{
Xrtablerow R =   New Xrtablerow ();
R. Size =   New System. Drawing. Size (pagewidth, mdetailrowheight );
For ( Int K =   0 ; I < Mreportlayout [ 1 ]. Length && (K + Convert. toint32 (reportlayout [ 1 ] [I] [ 2 ]) <= Mdetailtablecolumncount; k + = Convert. toint32 (reportlayout [ 1 ] [I] [ 2 ]), I ++ )
{
Xrtablecell C =   New Xrtablecell ();
C. Size =   New System. Drawing. Size (columnwidth * Convert. toint32 (reportlayout [ 1 ] [I] [ 2 ]), Mdetailrowheight );
C. Font =   New System. Drawing. Font ( " " , 9f );
C. textalignment = (Reportlayout [ 1 ] [I] [ 0 ]. Substring ( 0 , 1 ) =   " N "   | Reportlayout [ 1 ] [I] [ 0 ]. Substring ( 0 , 1 ) =   " N " ) ? Devexpress. xtraprinting. textalignment. middleright: devexpress. xtraprinting. textalignment. middleleft;
C. databindings. addrange ( New Devexpress. xtrareports. UI. xrbinding [] {
New Devexpress. xtrareports. UI. xrbinding ( " Text " , MDS. Tables [ 1 ], Reportlayout [ 1 ] [I] [ 0 ], Reportlayout [ 1 ] [I]. Length > =   4   ? Reportlayout [ 1 ] [I] [ 3 ]: String . Empty )} );
R. cells. addrange ( New Devexpress. xtrareports. UI. xrtablecell [] {C} );
C. Tag = Reportlayout [ 1 ] [I] [ 2 ];
}
Detailtable. Rows. addrange ( New Devexpress. xtrareports. UI. xrtablerow [] {R} );
}
Mreport. Detail. Controls. addrange ( New Devexpress. xtrareports. UI. xrtable [] {Detailtable} );
}

Here is data binding (C. databindings). If it is in the page header, it should be:
C. Text =Mreportlayout [0] [I] [1] + ":" + MDS. Tables [0]. Rows [0] [mreportlayout [0] [I] [0];
For details, you should add an xrtable header in the header to place the title. This is not to mention. According to the above Code, it is easy to understand how to do it.
Groups and totals are also used to store their la s:
/// <Summary>
/// Indicates the layout of the statistics field [I] [J]
/// I: fields displayed in sequence
/// J = 0: field name, j = 1: column span, j = 2: Summary function, j = 3: Format String (optional)
/// </Summary>
Public String [] [] totallayout = NULL;
/// <Summary>
/// Indicates the group Field Layout [I] [J] [k]
/// I: Group I
/// J: field to be displayed in each group
/// K = 0: field name, k = 1: column span, K = 2: Summary function, K = 3: Format String (optional)
/// </Summary>
Public String [] [] grouplayout = NULL;
According to the notes, the above section of code only needs to add the following code to each xrtablecell:
If (grouplayout [I] [J]. length> = 3)
{
Xrsummary summary = new xrsummary ();
Summary. func = (devexpress. xtrareports. UI. summaryfunc) enum. parse (typeof (devexpress. xtrareports. UI. summaryfunc), grouplayout [I] [J] [2], true );
Summary. formatstring = grouplayout [I] [J]. length> = 4? Grouplayout [I] [J] [3]: String. empty;
Summary. Running = summaryrunning. Group; // if it is a total, it should be summaryrunning.Report
C. Summary = summary;
}

in the process of doing this, we also found that the cell line is not aligned, and there is always a difference of 1 pixel. Later, we were advised by our colleagues, every time an xrtable is defined, the size of its position is calculated and set it, just like the code above, instead of setting the xrtablerow and xrtablecell attributes in the xrtable, set the xrtable attributes, it seems that the location and size attributes of xrtablerow and xrtablecell are based on xrtable. The size of the newly defined xrtable should be PX, the xrtablerow and xrtablecell bound to it will scale proportionally to PX based on their own size. Then you can set the xrtable size to PX, they are scaled to 10 in proportion to the original size. 00px, so there will be a bit of error when you go to the second place. Haha, this is also very reasonable.
Implementation
after the report is made, we only need to define an instance (RPT) of my class (for example, the class name is Report ), define parameters for the constructor at the same time (I have defined such constructor in this class, the parameters include all the variables we want to layout the report and the data source dataset ), then an Rpt. showpreview (); the report is displayed. (This method is available in my class, this method calls the preceding code for setting the report layout, and then uses devexpress. the showpreview () to display the Report ) of the xtrareports report is OK. The figure below is ps.

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.