Ticket T490052
Visible to All Users

How to print or export select records immediately when using the built-in ShowInReport Action

created 8 years ago

[DevExpress Support Team: CLONED FROM T486847: How to show report with simple action?]
https://www.devexpress.com/Support/Center/Example/Details/E5146

I tried this case, but found a problem: when I choose a data, the print is still all the data.
I now need to select a piece of data, and then only print the report of this data. So I would like to add an Action in the DetailView, click on the data source will be passed to the report. Please change how to do?

Answers approved by DevExpress Support

created 8 years ago (modified 5 months ago)

Hello He,

I suggest you accomplish this task using a customized ShowInReport Action along with a modified approach shown at How to: Print a Report Without Displaying a Preview.
Consider the following implementation plan:

ASP.NET WebForms:

  1. In the YourSolutionName.Module.Web project, create a custom ViewController and handle the DevExpress.ExpressApp.ReportsV2 > ReportServiceController > CustomShowPreview  event and take the WebPrintContactsController class code from the How to: Print a Report Without Displaying a Preview article as a base. There, you will need to additionally pass the Criteria parameter to the client-side script:
C#
using System; using DevExpress.ExpressApp; using DevExpress.ExpressApp.ReportsV2; using DevExpress.ExpressApp.Web; namespace MainDemo.Module.Web.Controllers { public class T490052_Web : ViewController { protected override void OnActivated() { base.OnActivated(); Frame.GetController<ReportServiceController>().CustomShowPreview += OnCustomShowPreview; } private void OnCustomShowPreview(object sender, CustomShowPreviewEventArgs e) { e.Handled = true; string script = string.Format(@" if(!ASPx.Browser.Edge) {{ var iframe = document.getElementById('reportout'); if (iframe != null) {{ document.body.removeChild(iframe); }} iframe = document.createElement('iframe'); iframe.setAttribute('id', 'reportout'); iframe.style.width = 0; iframe.style.height = 0; iframe.style.border = 0; document.body.appendChild(iframe); document.getElementById('reportout').contentWindow.location = 'InstantPrintReport.aspx?reportContainerHandle={0}&selectedRecordsCriteriaForReport={1}'; }} else {{ window.open('InstantPrintReport.aspx?reportContainerHandle={0}&selectedRecordsCriteriaForReport={1}', '_blank'); }} ", Uri.EscapeDataString(e.ReportContainerHandle), Uri.EscapeDataString(e.Criteria.ToString())); ((WebWindow)WebApplication.Instance.MainWindow).RegisterStartupScript( "InstantPrintReport", script, overwrite: true); } } }
  1. In the YourSolutionName.Web project, add the InstantPrintReport .aspx web form file and modify its code as follows:
C#
using System; using System.IO; using DevExpress.Data.Filtering; using DevExpress.ExpressApp.ReportsV2; using DevExpress.XtraPrinting; using DevExpress.XtraReports.UI; namespace MainDemo.Web { public partial class InstantPrintReport : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string reportDataHandle = Request.QueryString["reportContainerHandle"]; string criteria = Request.QueryString["selectedRecordsCriteriaForReport"]; ReportsModuleV2 module = ReportsModuleV2.FindReportsModule( ApplicationReportObjectSpaceProvider.ContextApplication.Modules); if(!String.IsNullOrEmpty(reportDataHandle) && module != null) { XtraReport report = null; try { report = ReportDataProvider.ReportsStorage.GetReportContainerByHandle(reportDataHandle).Report; module.ReportsDataSourceHelper.SetupBeforePrint(report, null, CriteriaOperator.Parse(criteria), true, null, false); using(MemoryStream ms = new MemoryStream()) { report.CreateDocument(); PdfExportOptions options = new PdfExportOptions(); options.ShowPrintDialogOnOpen = true; report.ExportToPdf(ms, options); ms.Seek(0, SeekOrigin.Begin); byte[] reportContent = ms.ToArray(); Response.ContentType = "application/pdf"; Response.Clear(); Response.OutputStream.Write(reportContent, 0, reportContent.Length); Response.End(); } } finally { if(report != null) report.Dispose(); } } } } }

Take special note that here we additionally read the Criteria parameter passed at the previous step and modified the SetupBeforePrint method call.

WinForms:
In the YourSolutionName.Module.Win project, create a custom ViewController and handle the DevExpress.ExpressApp.ReportsV2 > ReportServiceController > CustomShowPreview event and take the WinPrintContactsController class code from the How to: Print a Report Without Displaying a Preview article as a base. There, you will also need to modify the SetupBeforePrint method call to pass criteria from the CustomShowPreview event arguments:

C#
using DevExpress.ExpressApp; using DevExpress.ExpressApp.ReportsV2; using DevExpress.XtraReports.UI; namespace MainDemo.Module.Win.Controllers { public class T490052_Win : ViewController { protected override void OnActivated() { base.OnActivated(); Frame.GetController<ReportServiceController>().CustomShowPreview += OnCustomShowPreview; } private void OnCustomShowPreview(object sender, CustomShowPreviewEventArgs e) { e.Handled = true; IReportContainer reportContainer = ReportDataProvider.ReportsStorage.GetReportContainerByHandle(e.ReportContainerHandle); XtraReport report = reportContainer.Report; ReportsModuleV2 reportsModule = ReportsModuleV2.FindReportsModule(Application.Modules); if(reportsModule != null && reportsModule.ReportsDataSourceHelper != null) { reportsModule.ReportsDataSourceHelper.SetupBeforePrint(report, null, e.Criteria, true, null, false); report.PrintDialog(); } } } }
    Show previous comments (1)
    Dennis Garavsky (DevExpress) 5 years ago

      @Adrian D I
      Having the report handle of an existing IReportDataV2 object, you can use the following code to obtain the object instance:

      C#
      IReportDataV2 reportData2 = (IReportDataV2)objectSpace.GetObjectByHandle(reportContainerHandle);
      L L
      Luis Alvarado Day 5 years ago

        Hi Dennis, I have implemented this a couple of years ago and it was working perfectly unitil last Chrome update.I did install the hot fix and it is working from previews, but not here. Any suggestions?

        Dennis Garavsky (DevExpress) 5 years ago

          Hello,

          I've created a separate ticket on your behalf (T820621: Web - Printing a report does not work with the latest Chrome update). It has been placed in our processing queue and will be answered shortly.

          Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

          Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.