How can I serialize a DevExpress XtraReport report design
Asked Answered
S

3

8

I need to serialize a report design. This is the scenario:

The app has base reports, let's say "Sales Report" with a set of pre-defined columns and design, like the corp. logo in the header. The users needs to have the ability to change that layout adding, for example, a footer with the office address, or page numbers. For doing that they need to edit the report, enter the designer and add/change what they need. This changed report layout needs to be serialized to be stored in the database for that user, so the next time, the user opens that report, using that design.

Makes sense?

Saddleback answered 3/8, 2009 at 12:52 Comment(0)
C
9

Here's a simplified version of how I do this:

XtraReport customReport;
customReport = new MyXtraReport();
byte[] layout = LoadCustomLayoutFromDB();
if (layout != null) {
    using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(layout)) {
        customReport.LoadLayout(memoryStream);
    }
}

using (XRDesignFormEx designer = new XRDesignFormEx()) {
    MySaveCommandHandler customCommands = new MySaveCommandHandler(designer.DesignPanel);
    designer.DesignPanel.AddCommandHandler(customCommands);
    designer.OpenReport(customReport);
    designer.ShowDialog(this);
    if (customCommands.ChangesSaved)
        SaveCustomLayoutToDB(customCommands.Layout);
}

Inside MySaveCommandHandler class:

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

    using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream()) {
        panel.Report.SaveLayout(memoryStream);
        this.layout = memoryStream.ToArray();
        changesSaved = true;
    }

    panel.ReportState = ReportState.Saved;
    handled = true;
}
Chianti answered 4/8, 2009 at 4:18 Comment(1)
I have a question... why you save the layout to a byte[], is it simple, more direct to save to the database? So in the SaveCustomLayoutToDb method you pass that to a SQL Server binary column or something?Saddleback
C
3

I think what you are looking for is the SaveLayout method:

Saving the report

YourReport report = new YourReport();

// Save the layout to a file.
report.SaveLayout(@"C:\YourReport.repx");

Loading the report

YourReport report = new YourReport();

// Load the layout
report.LoadLayout(@"C:\YourReport.repx");

Edit:

here a link to the devexpress support site explaining how to save the report definition.

Conlee answered 3/8, 2009 at 13:27 Comment(3)
Excuse me Francis, but saving the report to a repx file is not, as far as I know, a serialization. Do you know if the tool has a SaveToXml() method or something? Thanks.Saddleback
Serialization does not mean you have to save the information in a xml file. Right now, DevExpress does not support saving the report definition in xml but they implemented this feature for a future release (devexpress.com/Support/Center/p/AS4336.aspx).Conlee
That's true Francis. It doesn't mean that. But IMHO it's the most common way to work these days. To serialize objects into XML and send it through the wire using web services or save it in a text file in the database.Saddleback
K
1

You can Save/Load to and from a stream using Save and LoadLayout overrides. For the designer you can add a command handler to intercept the save command.

These articles should cover what you need:

How to: Save and Restore a Report Definition from a Stream

How to: Override Commands in the End-User Designer (Custom Saving)

And for completeness: List of all how-to's

Edit: fixed links

Kirt answered 3/8, 2009 at 20:22 Comment(1)
Thanks a lot Dag, that's a very good lead for me to go further in the matter.Saddleback

© 2022 - 2024 — McMap. All rights reserved.