Set programatically the title to the viewer form in XtraReport

一笑奈何 提交于 2019-12-04 17:13:18

I don't believe that the preview form used by the XtraReport object is exposed in such a way that you could simply set the title. However, it is possible to create your own preview form. That would give you ultimate control over how your preview is displayed. Unfortunately, using that approach requires you to invoke the preview differently. You would no longer call myReport.ShowPreviewDialog(). In the example, the report is a private member of the preview form that is created in the form's load event. But I would pass a reference to an existing report object into the form before it loads so you can re-use one print preview form.

EDIT: Apparently if you do not call CreateDocument it will appear to work sometimes, other times not. So Make sure it is there (it was missing in my first post).

I believe that Kyle's answer is not correct. It appears that you can access the form, it is just not intuitive. As Pierre pointed out, there are valid reasons to create your own form, but if you are find with the default and just want to customize the title then try:

using(var rpt = new XtraReport1())
{
   rpt.PrintingSystem.PrintPreviewFormEx.Text = "My Custom Caption";
   rpt.CreateDocument();
   rpt.ShowPreviewDialog();
}

In our projects, we always end up creating a ReportViewer form that purpose is to display a XtraReport (or PrintingSystem).

The viewer consist of a normal XtraForm on which we drop a PrintRibbonController. That will automatically create the ribbon bar and the print control.

Then we use a method that bind the report to the viewer:

public partial class ReportViewer : DevExpress.XtraEditors.XtraForm
{
    public ReportViewer()
    {
        InitializeComponent();
    }

    // Used when displaying a single report
    public void SetReport(XtraReport report)
    {
        this.printControl.PrintingSystem = report.PrintingSystem;
        report.CreateDocument();
        this.printControl.UpdatePageView();
    }

    // Used when displaying merged reports
    public void SetReport(PrintingSystem system)
    {
        this.printControl.PrintingSystem = system;
        this.printControl.UpdatePageView();
    }
}

So displaying a report goes like this:

ReportViewer viewer = new ReportViewer();
viewer.SetReport(new EmployeeReport());
viewer.Show();

This approach of creating your own viewer can help you:

  • Manages security by user (for example: a normal user can't change the watermark),
  • Changes the ribbon by removing or adding button to fit your requirements.

I think there is an article on the devexpress suport that might help you - Unable to change report preview window titlebar caption

The gist of it:

XtraReport1 rep = new XtraReport1();
            rep.CreateDocument();
            PrintPreviewFormEx form = new PrintPreviewFormEx();
            form.Text = "test";
            form.PrintingSystem = rep.PrintingSystem;
            form.Show(); 

You can use ReportPrintTool class for fixing your problem:

var report = new MyXtraReport();
ReportPrintTool reportPrintTool = new ReportPrintTool(report);
reportPrintTool.PreviewForm.Text = "Some Text"
report.ShowPreviewDialog();    

I have found Pierre's answer very helpful - having your own custom Report Viewer can indeed help you manage access and the like. I have added this code:

 PrintingSystemCommand[] commands = {PrintingSystemCommand.DocumentMap,
                                                   PrintingSystemCommand.Open,
                                                   PrintingSystemCommand.Save};

 this.printControl1.PrintingSystem.SetCommandVisibility(commands, CommandVisibility.None);

Of-course you gotta have the references:

using DevExpress.XtraEditors;
using DevExpress.XtraReports.UI;
using DevExpress.XtraPrinting;

Thanks Again.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!