Note注意

重要说明: .NET Client Profile Framework 不支持此功能。 要在最终用户的机器上使用此功能,则必须安装完整的 .NET Framework。 更多信息,请参阅 Windows 窗体部署 文档中的 关于 .NET Framework 4.0 Client Profile 的重要说明 小节。

Note注意

关于在最终用户设计器中更复杂的报表保存方法,请参阅 如何: 实现自定义报表存储器

这个示例展示了如何重写并定制 最终用户设计器 中的保存功能。 这是有用的,例如,如果项目中的所有报表都应该通过流被保存在数据库中、然后从数据库取回,并且需要自动完成所有这些操作,而不迫使最终用户除了单击 保存 (或 另存为) 工具栏按钮以外、还执行其他操作。

在本例中,当最终用户关闭报表设计器时,将使用自定义保存程序来自动保存报表。

要完成此任务,则接管 ReportCommand.SaveFileReportCommand.SaveFileAs 命令 (如果有必要,也可接管 ReportCommand.Closing 命令)。 注意,要防止标准的保存程序被调用,则只需要启用 handled 属性。

下列代码展示了如何完成此任务。

C#CopyCode image复制代码
using DevExpress.XtraReports.UserDesigner;
// ...

private void button1_Click(object sender, EventArgs e) {
    // Create a design form and get its panel.
    XRDesignForm form = new XRDesignForm();

    // Open an empty report in the form.
    form.OpenReport(new XtraReport1());

    // Get the active Design Panel.
    XRDesignPanel panel = form.ActiveDesignPanel;

    // Add a new command handler which saves a report in a custom way.
    panel.AddCommandHandler(new SaveCommandHandler(panel));

    // Show the form.
    form.ShowDialog();
    panel.CloseReport();
}

public class SaveCommandHandler : ICommandHandler {
    XRDesignPanel panel;

    public SaveCommandHandler(XRDesignPanel panel) {
        this.panel = panel;
    }

    public virtual void HandleCommand(ReportCommand command, object[] args, ref bool handled) {
        if (!CanHandleCommand(command)) return;

        // Save a report.
        Save();

        // Set handled to true to avoid the standard saving procedure to be called.
        handled = true;
    }

    public virtual bool CanHandleCommand(ReportCommand command) {
        // This handler is used for SaveFile, SaveFileAs and Closing commands.
        return command == ReportCommand.SaveFile ||
            command == ReportCommand.SaveFileAs ||
            command == ReportCommand.Closing;
    }

    void Save() {
        // Write your custom saving here.
        // ...

        // For instance:
        panel.Report.SaveLayout("c:\\report1.repx");

        // Prevent the "Report has been changed" dialog from being shown.
        panel.ReportState = ReportState.Saved;
    }
}
Visual BasicCopyCode image复制代码
Imports DevExpress.XtraReports.UserDesigner
' ...

Private Sub button1_Click(ByVal sender As Object, _ 
ByVal e As EventArgs) Handles button1.Click
    ' Create a design form and get its panel.
    Dim form As New XRDesignForm()

    ' Open an empty report in the form.
    form.OpenReport(New XtraReport1())

    ' Get the active Design Panel.
    Dim panel As XRDesignPanel = form.ActiveDesignPanel

    ' Add a new command handler which saves a report in a custom way.
    panel.AddCommandHandler(New SaveCommandHandler(panel))

    ' Show the form.
    form.ShowDialog()
    panel.CloseReport()
End Sub

Public Class SaveCommandHandler
    Implements ICommandHandler
    Private panel As XRDesignPanel

    Public Sub New(ByVal panel As XRDesignPanel)
        Me.panel = panel
    End Sub

    Public Overridable Sub HandleCommand(ByVal command As ReportCommand, _ 
    ByVal args() As Object, ByRef handled As Boolean) Implements ICommandHandler.HandleCommand
        If (Not CanHandleCommand(command)) Then
            Return
        End If

        ' Save a report.
        Save()

        ' Set handled to true to avoid the standard saving procedure to be called.
        handled = True
    End Sub

    Public Overridable Function CanHandleCommand(ByVal command As ReportCommand) As Boolean _ 
    Implements ICommandHandler.CanHandleCommand
        ' This handler is used for SaveFile, SaveFileAs and Closing commands.
        Return command = ReportCommand.SaveFile OrElse command = ReportCommand.SaveFileAs OrElse _ 
        command = ReportCommand.Closing
    End Function

    Private Sub Save()
        ' Write your custom saving here.
        ' ...

        ' For instance:
        panel.Report.SaveLayout("c:\report1.repx")

        ' Prevent the "Report has been changed" dialog from being shown.
        panel.ReportState = ReportState.Saved
    End Sub
End Class

Expand image参阅