logo
small logo
  • Products
  • Buy
  • Support
  • Articles
  • Customer panel Support
    • en
    • pt
    • es
    • de
    • pl
    • JP
    • ZH
  • Home
  • /
  • Articles
  • /
  • PreviewControl.OnPrint and PreviewControl.OnExport Events
  • How to disable printing reports and other items from the menu when viewing the report FastReport .NET

    February 29, 2020

    The main report viewing tool FastReport.Net is Viewer. This viewer has a rich toolkit for

    read more
  • Copying the Bands from One Report to Another with All the Contents

    March 31, 2021

    Sometimes there is a need to use parts of one report in another. In simple

    read more
  • FastReport .NET packages and .NET 5.0

    December 17, 2020

    UPD: Applies to the versions of FastReport .NET before 2022.2. License packages are now available

    read more
  • How to use Online Designer in ASP .NET Core

    January 17, 2018

    One of the novelties by FastReport .NET 2018 was the adaptation of OnlineDesigner to the

    read more
  • How to use WebReport with ASP .NET Web Core application

    January 17, 2018

    Recently FastReport introduced a new library under the .NET - FastReport Core platform. This is

    read more

PreviewControl.OnPrint and PreviewControl.OnExport Events

October 1, 2020

In FastReport 2019.4 added the ability to subscribe to PreviewControl.OnPrint and PreviewControl.OnExport events, which are called directly when the corresponding buttons are pressed.

When viewing a report, a viewer is called with a toolbar that has elements such as a print button and a drop-down list with report exports. Selecting any item in the list will trigger the OnExport event, and pressing the Print button will trigger OnPrint. Let's take an example and see how we can use these events in practice.

You can use the standard handler for these events, which is created for the visual component of PreviewControl:

 OnExport event setted

In this case you just add the necessary actions to the handler.

But if you add the PreviewControl component in the application code, you will have to sign your own handler for the event. For example, your handler may send you an export or print event alert. This could be, for example, a record in a database. Let us consider this example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 private void Button1_Click(object sender, EventArgs e)
 {
 //create report
 Report report = new Report();
 //Load report
 report.Load("App_Data/Master-Detail.frx");
 //create data source
 DataSet data = new DataSet();
 //load data
 data.ReadXml("C:\\Program Files (x86)\\FastReports\\FastReport.Net\\Demos\\Reports\\nwind.xml");
 //register data
 report.RegisterData(data);
 //create preview object
 var prev = new PreviewControl();
 //add preview into the form
 this.Controls.Add(prev);
 prev.Dock = DockStyle.Fill;
 prev.BringToFront();
 //subscribe to the event
 prev.OnExport+= new System.EventHandler<PreviewControl.ExportEventArgs>(ExportAction);
 //assign preview control to the report
 report.Preview = prev;
 //Show the report
 report.Show();
 }

 After creating the report object and registering data in it, we create a PreviewControl, subscribe to the OnExport event our event handler, which we will implement below. Then we assign the PreviewControl object to the preview report. And now we implement a custom event handler for the OnExport event:

1
2
3
4
5
6
7
8
9
 public void ExportAction(object sender, PreviewControl.ExportEventArgs e)
 {
 SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=usersdb;Integrated Security=True");
 SqlCommand command = new SqlCommand("insert into dbo.Status (ReportName, ExportType, ExportDate) Vales ('" + Path.GetFileNameWithoutExtension(e.Report.FileName) + "', '"+ e.Export.BaseName +"', '" + DateTime.Now + "')");
 command.Connection = conn;
 conn.Open();
 command.ExecuteNonQuery();
 conn.Close();
 }

 In this method, we record information about the export event in a database, with the report name, export type and transaction date. This is just one possible example of how to use this event. You can, for example, implement sending an email on this event, or saving an export file to a specific folder.

The OnPrint event is handled in the same way.

These two events are the most frequent operations on reports when viewed, so many people would like to automate their custom operations on the event. Now it is possible, it is easy to take information about the report, export or print settings from event arguments and dispose of it to create your own, additional operations.

about product download buy
avatar
Dmitriy Fedyashov
Technical Writer
Fast Reports Team: Dmitriy Fedyashov - Technical Writer at Fast Reports
.NET FastReport Preview Event

Add comment
logo
  • 800-985-8986 (English, US)
  • +4930568373928 (German)
  • +55 19 98147-8148 (Portuguese)
  • info@fast-report.com
  • 901 N Pitt Str #325 Alexandria VA 22314
  • Buy
  • Download
  • Documentation
  • Testimonials
  • How to uninstall
  • Ticket system
  • FAQ
  • Tutorial Video
  • Forum
  • Articles
  • Our News
  • Press about us
  • Resellers
  • Extended licensing
  • Contact us

© 1998-2022 by Fast Reports Inc.

  • Privacy Policy