Sandeep Byagoni
Sandeep Byagoni

Reputation: 41

How to display the print dialog box for crystal report viewer in wpf

In my print window i have written the below code:

public void ShowReport(ReportDocument rp)
    {
        MyCrystalReportsViewer.ViewerCore.ReportSource = rp;
        this.WindowState = WindowState.Maximized;
        MyCrystalReportsViewer.ViewerCore.PrintReport();
    }

I am calling this method from another class,but when i'm displaying the print window, the print dialog box was not appearing by default.

Can any one solve my problem to display the print dialog box when I am calling print window.

Thanks in advance

Upvotes: 0

Views: 3529

Answers (2)

InterFast507
InterFast507

Reputation: 1

I did it in two ways, using a window form, I call from the page to window form, inside the window form I embed the CrystalReportViewer and the window form loads the crystal report, it is the easiest and most practical way I have been, code:

Dim rpt As New rpt_Crystal_report
Dim frm As New windowform
Frm.CrystalReportViewer1.ReportSource = rpt
Frm.Refresh ()
Frm.ShowDialog ()
Frm.Dispose ()

And the other way to load the crystal report in a page I do the following: Example, I have page 1 and page 2, inside page 1 I have a print button, which has the following code, Dim p1 As New page2 NavigationService.Navigate (p1)

And inside page 2, I embed the crystalreportviewer the same as I did in the windowform, but inside the load of page 2 I have the following code, Dim rpt As New rpt_Crystal_report Me.CrystalReportsViewer1.ViewerCore.ReportSource = rpt

And in this way I managed to show the report on a page,

If someone has another idea better and it works, it would be excellent, regards!

Upvotes: 0

Maybe this will help. I simply use the print mechanism that is provided in the Report Viewer to begin with, not try to make my own print dialogs. Also, I found Crystal's WPF implementation to be poor and lacking many features i needed, and therefore i hosted the WinForm version inside a WindowsFormsHost instead. Please try this route, as I said the WPF version was very poor and support online at Crystal's website is horrendous. Here is my code-

XAML where i host the WindowsFormHost (crystal's newer WPF version left many options out that i needed):

<Window x:Class="Example.Views.Reports.ReportView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        WindowStartupLocation="CenterOwner"
        xmlns:Viewer="clr-namespace:CrystalDecisions.Windows.Forms;assembly=CrystalDecisions.Windows.Forms"
        xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
        Title="{Binding WindowTitle}" >
    <!--xmlns:Viewer="clr-namespace:SAPBusinessObjects.WPF.Viewer;assembly=SAPBusinessObjects.WPF.Viewer"-->
    <Grid>
        <!--<Viewer:CrystalReportsViewer Name="ReportViewer"/>-->
        <WindowsFormsHost>
            <Viewer:CrystalReportViewer x:Name="ReportViewer"/>
        </WindowsFormsHost>
    </Grid>
</Window>

code behind that makes it work:

    using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Bill.Constants;
using Bill.Models.Reports;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using CrystalDecisions.Windows.Forms;

namespace Example.Views.Reports
{
    /// <summary>
    /// Interaction logic for ReportView.xaml
    /// </summary>
    public partial class ReportView : Window, INotifyPropertyChanged
    {
        public ReportView(Report report)
        {
            InitializeComponent();

            //WPF - not needed
            //this.ReportViewer.Owner = Window.GetWindow(this);  //added to fix null parameter window bug in Crystal report 

            if (Application.Current.MainWindow.IsLoaded)
            {
                this.Owner = Application.Current.MainWindow;
            }

            WindowTitle = report.DisplayName;
            this.DataContext = this;

            ShowReport(report.FileInfo.FullName);
        }

        private string _windowTitle;
        /// <summary>
        /// Name of the report, shown in Window Title
        /// </summary>
        public string WindowTitle
        {
            get { return _windowTitle; }
            set
            {
                if (_windowTitle != value)
                {
                    _windowTitle = value;
                    FirePropertyChanged("WindowTitle");
                }
            }
        }

        /// <summary>
        /// Show the selected report
        /// </summary>
        /// <param name="reportPath"></param>
        private void ShowReport(string reportPath)
        {
            ReportDocument report = new ReportDocument();

            if (!String.IsNullOrEmpty(reportPath))
            {
                TableLogOnInfos crtableLogoninfos = new TableLogOnInfos();
                TableLogOnInfo crtableLogoninfo = new TableLogOnInfo();
                ConnectionInfo crConnectionInfo = new ConnectionInfo();
                Tables CrTables;

                report.Load(reportPath);

                SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(ConnectionStrings.CurrentConnection.ConnectionString);  //from config.xml
                crConnectionInfo.ServerName = builder.DataSource;
                crConnectionInfo.DatabaseName = builder.InitialCatalog;
                crConnectionInfo.IntegratedSecurity = true;

                CrTables = report.Database.Tables;
                foreach (CrystalDecisions.CrystalReports.Engine.Table CrTable in CrTables)
                {
                    crtableLogoninfo = CrTable.LogOnInfo;
                    crtableLogoninfo.ConnectionInfo = crConnectionInfo;
                    CrTable.ApplyLogOnInfo(crtableLogoninfo);
                }
//WinForms values

                ReportViewer.ReportSource = report;
                ReportViewer.EnableDrillDown = false;
                //ReportViewer.AllowedExportFormats=ExportFormatType.
                ReportViewer.ShowLogo = false;
                ReportViewer.ShowRefreshButton = false;
                ReportViewer.ShowParameterPanelButton = false;
                ReportViewer.ShowGroupTreeButton = false;
                ReportViewer.UseWaitCursor = false;
                ReportViewer.ToolPanelView = ToolPanelViewType.None;

                //report.Refresh();
            }
        }

        #region INotifyPropertyChange

        public event PropertyChangedEventHandler PropertyChanged;

        protected void FirePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));

        }

        #endregion
    }
}

Upvotes: 1

Related Questions