FRNetProgrammerManual en
FRNetProgrammerManual en
Table of contents
Chapter I General information 6
6 6 7 7
10
10 11 12 13 14 14 15 15 16 18 19 20 21 21 21 24 24
26
26 27 28 28 29
Table of contents
Chapter
I
General information
General information
in the dialog, press the "Browse..." button and choose FastReport.dll, FastReport.Web.dll files (they are located in the "C:\Program Files\FastReports\FastReport.Net" folder); close the dialog with OK button. After this, you will see FastReport.Net components in a chosen tab:
Report; PreviewControl; DesignerControl; EnvironmentSettings; WebReport (this component will be visible in ASP.NET project only).
Troubleshooting
If you face a problem when working with report designer (for example, some toolbars or tool windows are damaged), you should delete the configuration file. This file is created when you start FastReport.Net. It is located in the following folder: Windows XP: C:\Documents and Settings\user_name\Local Settings\Application Data\FastReport\FastReport.config
General information
Windows Vista: C:\Users\user_name\AppData\Local\FastReport\FastReport.config The following information is stored in the config: sizes and locations of dialog windows; toolbar settings; recent used data connections; email settings (if you use the "Send Email" feature in the preview).
Deployment
You may redistribute the following files along with your application: FastReport.dll - the main FastReport.Net library; FastReport.Web.dll - the library that contains ASP.Net WebReport component; FastReport.Bars.dll - the toolbars and docking windows library; FastReport.Editor.dll - the code editor with syntax highlight. This library is not needed if you don't provide an end-user designer; FastReport.xml - comments for classes, properties and methods used in FastReport. This file is used by the script editor, and also by the hint panels in the "Data" and "Properties" windows. It's not obligatory to distribute this file. You may distribute the User's Manual which is contained in the FRNetUserManual.chm file. This file can be shown from the report designer, if you select the "Help|Contents..." menu item. If your reports are stored in files, you have to deploy them as well.
Update the references to other FastReport.Net assemblies. To do this: expand the "FastReport\References" item in the Solution Explorer;
General information
remove the "FastReport.Bars", "FastReport.Editor" references; right-click the "References" item and choose the "Add Reference..." item; add references to the "FastReport.Bars.dll", "FastReport.Editor.dll" and "System.Windows. Forms.DataVisualization.dll" files. These files are located in the "C:\Program Files\FastReports\FastReport.Net" folder.
General information
Chapter
II
Working with Windows.Forms
To create a report, perform the following steps: put the "Report" component on a form:
right-click it (or click on a smart tag button) and select the "Design Report..." item:
10
create your report. Read more about this in the User's Manual; close the report designer; add a "Button" control on your form; double-click it and write the following code in the button_Click event handler:
report1.Show(); save the project and run it. When you click on a button you will see the prepared report.
The following example demonstrates how to do this: using (Report report = new Report()) { report.Load("report1.frx"); report.RegisterData(dataSet1, "NorthWind"); report.Show(); } We will consider these steps in details in the following sections of this manual.
11
12
Registering data
If your report uses data from an application (for example, the typed dataset or a businessobject), you have to register such data in a report. This can be done using the RegisterData method of the Report object. When you use the Report as described in the "Using the Report component in Visual Studio" section, you don't need to register the data. FastReport.Net does it automatically (it adds the RegisterData call in the InitializeComponent method of your form). The RegisterData method must be called after you have loaded the report: report1 = new Report(); report1.Load("report.frx"); report1.RegisterData(dataSet1, "NorthWind"); The RegisterData method is overloaded and allows to register the following data: Method void RegisterData( DataSet data) Description Registers the dataset. This method registers all tables, views and relations as well. Attention: if you register more than one dataset, use the RegisterData(DataSet data, string name) method instead. void RegisterData( DataSet data, string name) void RegisterData( DataTable data, string name) void RegisterData( DataView data, string name) void RegisterDataAsp( IDataSource data, string name) void RegisterData( DataRelation data, string name) Registers the dataset. Specify any name in the name parameter (it must be persistent and unique if you register more than one dataset). Registers the data table.
void RegisterData( Registers the business object. Specify what items (properties, IEnumerable data, fields) should be used, in the flags parameter. Specify the string name, maximum nesting level in the maxNestingLevel parameter BOConverterFlags flags, int maxNestingLevel) (typically you need no more than 3 levels). Several nested objects may slow down the report.
13
Running a report
To run a report, use one of the following methods of the Report object: Method void Show() Description Runs a report and shows it in the preview window. This method is equal to: if (Prepare()) ShowPrepared(); bool Prepare()
Runs a report. If the report was prepared successfully, returns true. After this method, you need to call one of the following methods: ShowPrepared, PrintPrepared, SavePrepared, Export: if (Prepare()) ShowPrepared();
Runs a report. If the append parameter is set to true, the prepared report will be added to the existing one. So you can build several reports and display them in the preview as one report: report1.Load("report1.frx"); report1.Prepare(); report1.Load("report2.frx"); report1.Prepare(true); report.ShowPrepared();
void ShowPrepared() Shows a prepared report in the preview window. The report must be either prepared using the Prepare method, or loaded from the .FPX file using the LoadPrepared method: if (Prepare()) ShowPrepared();
14
void ShowPrepared( Shows a prepared report in the preview window. The modal parameter bool modal) determines whether the preview should be shown modally. void ShowPrepared( The same as the previous method. The owner parameter determines a bool modal, window that owns the preview window. Form owner) void ShowPrepared( The same as the previous method. The mdiParent parameter Form mdiParent) determines the main MDI window.
Designing a report
You can use the report designer in your application. This is possible for all FastReport.Net editions except the Basic. To do this, use the Design method of Report object: report1 = new Report(); report1.Load("report1.frx"); report1.Design(); The Design method is overloaded: Method bool Design() Description Shows the designer. Shows the designer. The modal parameter determines whether it is necessary to show the designer modally.
bool Design( Shows the designer. The mdiParent parameter defines the main MDI Form mdiParent) window.
Exporting a report
The prepared report can be exported to one of the supported formats. At this moment, the following formats can be used: PDF HTML RTF Excel XML (Excel 2003+) Excel 2007 CSV TXT OpenOffice Calc Pictures (Bmp, Png, Jpeg, Gif, Tiff, Metafile)
The export is performed by the export filter. To do this: prepare a report using the Prepare method; create an instance of export filter and set up its properties;
15
call the Export method of the Report object. The following example exports a prepared report in the HTML format: // prepare a report report1.Prepare(); // create an instance of HTML export filter FastReport.Export.Html.HTMLExport export = new FastReport.Export.Html.HTMLExport(); // show the export options dialog and do the export if (export.ShowDialog()) report1.Export(export, "result.html"); In this example, export settings are made using the dialog window.
Language DefaultLanguage default script language for new reports. The bool ShowProgress bool ShowPerformance Determines whether it is necessary to show the progress window. Determines whether to show the information about the report performance (report generation time, memory consumed) in the lower right corner of the preview window.
The EnvironmentSettings.DesignerSettings property contains some designer-related settings: Property Icon Icon Font DefaultFont Description The icon for the designer window. The default font used in a report.
PreviewButtons ButtonsSet of buttons that will be visible in the preview's toolbar. int PagesInCache The number of prepared pages that can be stored in the memory cache during preview. Determines whether the preview window is displayed in the Windows taskbar. Determines whether the preview window should be displayed as a topmost form.
bool ShowInTaskbar
bool TopMost
16
The icon for the preview window. The text for the preview window. If no text is set, the default text "Preview" will be used.
The EnvironmentSettings.EmailSettings property contains email account settings. These settings are used in the "Send Email" feature in the preview window: Property string Address string Name Description Sender address (e.g. your email address). Sender name (e.g. your name).
string MessageTemplateThe message template that will be used to create a new message. For example, "Hello, Best regards, ...". string Host int Port string UserName, string Password bool AllowUI SMTP host address. SMTP port (25 by default). User name and password. Leave these properties empty if your server does not require authentication. Allows to change these settings in the "Send Email" dialog. Settings will be stored in the FastReport.Net configuration file.
UI style settings are available in the following properties of EnvironmentSettings component: Property UIStyle UIStyle Description The style of designer and preview form. 6 styles are available VisualStudio2005, Office2003, Office2007Blue, Office2007Silver, Office2007Black, VistaGlass. The default style is Office2007Black. bool UseOffice2007FormThis property affects the designer and preview form. It determines whether the Office2007-style form should be used if one of the following styles is selected: Office2007Blue, Office2007Silver, Office2007Black, VistaGlass. Default value is true. Besides these properties, the EnvironmentSettings component has some events. Using such events, you may do the following: replace standard "Open file" and "Save file" dialogs in the designer; replace standard progress window; pass own connection string to a connection defined in the report. These tasks will be described in the following sections of this manual.
17
Occurs when the report designer is about to show the "Save" dialog. In the event handler, you must display a dialog window to allow user to choose a report file. If dialog was executed successfully, you must return e.Cancel = false and set the e.FileName to the selected file name. The following example demonstrates how to use this event: private void CustomSaveDialog_Handler( object sender, OpenSaveDialogEventArgs e) { using (SaveFileDialog dialog = new SaveFileDialog()) { dialog.Filter = "Report files (*.frx)|*.frx"; // get default file name from e.FileName dialog.FileName = e.FileName; // set e.Cancel to false if dialog // was succesfully executed e.Cancel = dialog.ShowDialog() != DialogResult.OK; // set e.FileName to the selected file name e.FileName = dialog.FileName; } }
18
CustomOpenReport
Occurs when the report designer is about to load the report. In the event handler, you must load the report specified in the e.Report property from the location specified in the e.FileName property. The latter property contains the name that was returned by the CustomOpenDialog event handler. It may be the file name, the database key value, etc. The following example demonstrates how to use this event: private void CustomOpenReport_Handler( object sender, OpenSaveReportEventArgs e) { // load the report from the given e.FileName e.Report.Load(e.FileName); }
CustomSaveReport
Occurs when the report designer is about to save the report. In the event handler, you must save the report specified in the e.Report property to the location specified in the e.FileName property. The latter property contains the name that was returned by the CustomSaveDialog event handler. It may be the file name, the database key value, etc. The following example demonstrates how to use this event: private void CustomSaveReport_Handler( object sender, OpenSaveReportEventArgs e) { // save the report to the given e.FileName e.Report.Save(e.FileName); }
Progress
FinishProgress
19
progress window. The Progress event takes e parameter of ProgressEventArgs type. It has the following properties: Property Description
string Message The message text. int Progress int Total The index of current report page being handled. The number of total pages in a report. This parameter may be 0 when preparing a report, because the number of total pages is unknown.
In most cases, you need to display the text from the e.Message property, in the Progress event handler. Other parameters may be useful if you want to display a progress bar.
20
Keep in mind that the DatabaseLogin event is global, it works with all reports.
21
Report report = new Report(); // register the "Products" table report.RegisterData(dataSet1.Tables["Products"], "Products"); // enable it to use in a report report.GetDataSource("Products").Enabled = true; // create A4 page with all margins set to 1cm ReportPage page1 = new ReportPage(); page1.Name = "Page1"; report.Pages.Add(page1); // create ReportTitle band page1.ReportTitle = new ReportTitleBand(); page1.ReportTitle.Name = "ReportTitle1"; // set its height to 1.5cm page1.ReportTitle.Height = Units.Centimeters * 1.5f; // create group header GroupHeaderBand group1 = new GroupHeaderBand(); group1.Name = "GroupHeader1"; group1.Height = Units.Centimeters * 1; // set group condition group1.Condition = "[Products.ProductName].Substring(0, 1)"; // add group to the page.Bands collection page1.Bands.Add(group1); // create group footer group1.GroupFooter = new GroupFooterBand(); group1.GroupFooter.Name = "GroupFooter1"; group1.GroupFooter.Height = Units.Centimeters * 1; // create DataBand DataBand data1 = new DataBand(); data1.Name = "Data1"; data1.Height = Units.Centimeters * 0.5f; // set data source data1.DataSource = report.GetDataSource("Products"); // connect databand to a group group1.Data = data1; // create "Text" objects // report title TextObject text1 = new TextObject(); text1.Name = "Text1"; // set bounds text1.Bounds = new RectangleF(0, 0, Units.Centimeters * 19, Units.Centimeters * 1); // set text text1.Text = "PRODUCTS"; // set appearance text1.HorzAlign = HorzAlign.Center; text1.Font = new Font("Tahoma", 14, FontStyle.Bold); // add it to ReportTitle page1.ReportTitle.Objects.Add(text1); // group TextObject text2 = new TextObject(); text2.Name = "Text2"; text2.Bounds = new RectangleF(0, 0, Units.Centimeters * 2, Units.Centimeters * 1); text2.Text = "[[Products.ProductName].Substring(0, 1)]"; text2.Font = new Font("Tahoma", 10, FontStyle.Bold);
22
// add it to GroupHeader group1.Objects.Add(text2); // data band TextObject text3 = new TextObject(); text3.Name = "Text3"; text3.Bounds = new RectangleF(0, 0, Units.Centimeters * 10, Units.Centimeters * 0.5f); text3.Text = "[Products.ProductName]"; text3.Font = new Font("Tahoma", 8); // add it to DataBand data1.Objects.Add(text3); // group footer TextObject text4 = new TextObject(); text4.Name = "Text4"; text4.Bounds = new RectangleF(0, 0, Units.Centimeters * 10, Units.Centimeters * 0.5f); text4.Text = "Count: [CountOfProducts]"; text4.Font = new Font("Tahoma", 8, FontStyle.Bold); // add it to GroupFooter group1.GroupFooter.Objects.Add(text4); // add a total Total groupTotal = new Total(); groupTotal.Name = "CountOfProducts"; groupTotal.TotalType = TotalType.Count; groupTotal.Evaluator = data1; groupTotal.PrintOn = group1.Footer; // add it to report totals report.Dictionary.Totals.Add(groupTotal); // run the report report.Show(); The prepared report looks as follows:
23
24
Chapter
III
Working with ASP.NET
in the "smart tag" menu, select the "Select Data Source..." item and choose one or several data sources which you want to use in a report:
in the "smart tag" menu, select the "Design Report..." item to run the report designer:
26
create a report. Read more about this in the User's Manual; close the designer; save the changes in your project and run it. You will see a window with a prepared report.
27
include that file into your project. It's better to save it in the "App_Code" folder; clear both ReportResourceString and ReportFile properties of the WebReport component. This method has the following pros and cons: + you can work with the report as a regular class; + you can debug the report in the Visual Studio; + it's the only way to use a report in the "Medium Trust" mode; - you cannot edit such a report. To do this, you need the original .FRX file. To work with a report, create the WebReport.StartReport event handler. In this handler, you should do the following: create an instance of your report class; register the data; set the report to the Report property of the WebReport component. Example of the StartReport event handler: SimpleListReport report = new SimpleListReport(); report.RegisterDataAsp(your_data, "your_data_name"); WebReport1.Report = report;
Registering data
If you select the data source using the "smart tag" menu of the WebReport component, you don't need to register the data manually. In this case, FastReport.Net stores the names of data sources in the ReportDataSources property of the WebReport component. In case you don't want to use such method of registering data, you need to do it manually. It can be done by using the StartReport event of the WebReport component. In this event handler, you can call the RegisterData and RegisterDataAsp methods of the report. The report can be accessed through the WebReport.Report property: webReport1.Report.RegisterData(myDataSet); Read more about registering data in this section.
28
To work with a report in this mode, you need to store a report as a C#/VB.NET class, as described in the "Storing and loading a report" section. In this case, the report compilation is not required. Besides that, it is necessary to add System.Windows.Forms.DataVisualization.dll assembly into the GAC. This assembly is a part of Microsoft Chart Control and is used in FastReport to draw charts. Consult with your shared-hosting provider regarding adding this assembly into the GAC.
29