Report Generator FastReport.Net Programmer's Manual: How to replace the "Open" and "Save" dialogs

FastReport .Net is a full-featured report solution for Windows Forms, ASP.NET, MVC and .NET Core. It can be used in Microsoft Visual Studio 2005-2019. Support .Net Framework 2.0-4.x, .NET Core 3.0 and above.

In the new version of FastReport .NET 2021.1, we have implemented support for .NET 5. Added a new barcode-Deutsce Post Leitcode. The algorithm for converting RTF into report objects has been significantly improved. And also added a new function for converting numbers.

Click to download the latest version of FastReport.NET v2021.1 now

Replace the "Open" and "Save" dialogs

If you decide to store the report in the database, you may need to change the designer to enable it to open and save the report from/to the database. In other words, you need to replace the standard "Open" and "Save" dialogs with your own dialogs. To do this, use the EnvironmentSettings component (see the previous section). The component has the following events.

CustomOpenDialog

Occurs when the report designer is about to display the "Open" dialog box. In the event handler, you must display a dialog window to allow the user to select a report file. If the dialog is successfully executed, e.Cancel = false must be returned, and e.FileName must be set to the selected file name.
The following example demonstrates how to use this event.

private void CustomOpenDialog_Handler(
Object sender, OpenSaveDialogEventArgs e)
{
    
    
using (OpenFileDialog dialog = new OpenFileDialog())
{
    
    
dialog.Filter = "Report files (*.frx)|*.frx"// 如果对话框中的 "报告文件",则将e.Cancel设置为false
// 已成功执行
e.Cancel = dialog.ShowDialog() != DialogResult.OK。
//将e.FileName设置为选定的文件名。
e.FileName = dialog.FileName。
}
}

Custom save dialog

Occurs when the report designer is about to display the "Save" dialog box. In the event handler, you must display a dialog window to allow the user to select a report file. If the dialog is successfully executed, e.Cancel = false must be returned, and e.FileName must be set 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";
//从e.FileName中获取默认文件名。
dialog.FileName = e.FileName;
// 如果 dialog.FileName = e.FileName; 
// 如果 dialog.FileName = e.Cancel,则设置 e.Cancel 为 false
e.Cancel = dialog.ShowDialog() != DialogResult.OK;
//将e.FileName设置为选定的文件名。
e.FileName = dialog.FileName;
}
}

Custom open report

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 attribute contains the name returned by the CustomOpenDialog event handler. It may be a file name, database key value, etc.
The following example demonstrates how to use this event:

private void CustomOpenReport_Handler(
Object sender, OpenSaveReportEventArgs e)
{
    
    
//从给定的e.FileName中加载报告。
e.Report.Load(e.FileName);
}

Custom save report

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 returned by the CustomSaveDialog event handler. It may be a file name, database key value, etc.
The following example demonstrates how to use this event.

private void CustomSaveReport_Handler(
Object sender, OpenSaveReportEventArgs e)
{
    
    
// 将报告保存到给定的e.FileName中。
e.Report.Save(e.FileName);
}

Replace standard progress window

During the following operations, a progress window will be displayed.

  • Run report
  • printing
  • Output

You can turn off the progress by setting the ReportSettings.ShowProgress property of the environment settings component to false. In addition, you can also replace the standard progress window with your own progress window. To do this, use the following events of the EnvironmentSettings component (see the section "Configuring the FastReport.Net Environment").

StartProgress

Occurs once before the operation. In this case, you must create your own progress window and display it.

schedule

Occurs every time the current report page is processed. In this case, you must display the progress status in the window.

FinishProgress

Occurs once after the operation. In this event, you must destroy the progress window.

The parameters of the Progress event are of type ProgressEventArgs. It has the following parameter attributes.
string Message | Message text
int Progress | The index of the report page currently being processed.
int Total | Total number of pages of the report. When preparing a report, this parameter may be 0 because the total number of pages is unknown.
In most cases, you need to display the text from the e.Message property in the Progress event handler. If you want to display a progress bar, other parameters may be useful.

Pass your own connection string

If you use a data source defined in a report, you may need to pass an application-defined connection string to the report. This can be done in three ways.

The first method: You directly pass the connection string to the Connection object in the report.
Perform the following operations.
report1.Load(...);
// Operate after loading the report and before running the report
//Assuming we only have one connection in the report
report1.Dictionary.Connections[0].ConnectionString = my_connection_string;
report1.Show();

The second method: You pass a connection string with the report parameter. Perform the following operations.
Run the report designer
In the "Data" window, create a new report parameter (for example, the name "MyParameter"). For more details, please refer to the "User Manual".
In the "Data" window, select the "Connection" object that contains the data source.
Switch to the "Properties" window and set the ConnectionStringExpression property to the following.
[M yParam eter]

Pass the connection string to the MyParameter parameter.


```javascript
report1.SetParameterValue("MyParameter", my_connection_string);

第三种方法:使用EnvironmentSettings组件的DatabaseLogin事件(参见 "配置FastReport.Net环境 "部分)。该事件在每次FastReport打开连接时发生。下面是这个事件处理程序的一个例子。

```javascript
private void environmentSettings1_DatabaseLogin(
Object sender, DatabaseLoginEventArgs e)
{
e.ConnectionString = my_connection_string;
}

Remember, the DatabaseLogin event is global and it applies to all reports.

Pass custom SQL

The report may contain data sources added using the Data Wizard (via the "Data|Add Data Source..." menu). Sometimes you need to pass custom SQL from your application to the data source. To do this, use the following code.

using FastReport.Data.Report1.Load(...)
report1.Load(...);
//在加载报表之后,运行报表之前进行操作
// 通过表的别名找到表
TableDataSource table = report1.GetDataSource("MyTable") as TableDataSource.SelectCommand = "new SQL text";
table.SelectCommand = "new SQL text";
report1.Show();

Reference to report object

When you use the report as a class (please refer to the "Storing and Loading Reports" section), you can directly reference the report object. The following example demonstrates how to change the font of the "Text1" object contained in the report.

SimpleListReport report = new SimpleListReport();
report.Text1.Font = new Font(“Arial”, 12);

In other cases, if you need to get a reference to an object, you must use the FindObject method of the Report object.
TextObject text1 = report1.FindObject(“Text1”) as TextObject;
text1.Font = new Font(“Arial”, 12);

To reference the data source defined in the report, use the GetDataSource method of the Report object. This method takes the alias of the data source as a parameter.
DataSourceBase ds = report1.GetDataSource("Products");

Guess you like

Origin blog.csdn.net/RoffeyYang/article/details/114025092