问题
Does anybody know how to set the title to the form viewer when showing an XtraReport document? The scenario is the following:
I have an XtraReport report configured, I show it calling the ShowPreviewDialog method, a viewer form opens and shows the document. I need to set the title to this viewer form and can't find the property or way to accomplish this.
Thanks in advance.
回答1:
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.
回答2:
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();
}
回答3:
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.
回答4:
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();
回答5:
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();
回答6:
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.
来源:https://stackoverflow.com/questions/1401507/set-programatically-the-title-to-the-viewer-form-in-xtrareport