This example demonstrates how to implement a report storage to store and retrieve report layouts. The project contains code for the following custom storages:
- DataSetReportStorage β stores reports in an XML file.
- XpoReportStorage β stores reports in an XPCollection.
- ZipReportStorage β stores reports in a zip archive.
Uncomment the appropriate code in the Program.cs (VB: Program.vb) file to select a storage type.
A storage is a class that inherits the ReportStorageExtension class and implements methods to store and retrieve reports serialized in XML format.
Note that the default report serialization mechanism does not support data sources and user-defined types, such as data objects, data sets, XPO data sources, and custom report parameters. You should implement custom XML serialization logic to store these objects along with the reports. Review the following examples:
- How to implement custom XML serialization of a report that is bound to a dataset
- How to serialize the XPO data source
- How to serialize parameters of custom types
When you run the project, a window appears that allows you to select a report, and open the End-User Designer to edit the report, or open the report preview.
If you click Design, the End-User Report Designer is invoked. It allows you to open, edit and save the report. End-User Report Designer displays the following warning on an attempt to load a report:
For more information, review the following help topic: Ensure Safe Loading of Reports (WinForms).
The following image shows the invoked End-User Report Designer with Ribbon UI:
When you click Open or Save in the Report Designer, the Storage Editor window appears. It allows you to select a report or specify a new report name.
Files to Review
- DataSetReportStorage.cs (VB: DataSetReportStorage.vb)
- Form1.cs (VB: Form1.vb)
- Program.cs (VB: Program.vb)
- StorageDataSet.cs (VB: StorageDataSet.vb)
- StorageEditorForm.cs (VB: StorageEditorForm.vb)
- XpoReportStorage.cs (VB: XpoReportStorage.vb)
- ZipReportStorage.cs (VB: ZipReportStorage.vb)
Documentation
- Custom Report Storage
- End-User Report Designer for WinForms
- End-User Documentation
- WinForms Reporting Application Security
More Examples
- WPF End-User Report Designer - How to Implement a Report Storage
- How to integrate the Web Report Designer into an MVC web application
Does this example address your development requirements/objectives?
(you will be redirected to DevExpress.com to submit your response)
Example Code
C#using System;
using System.Windows.Forms;
using DevExpress.Xpo;
using DevExpress.XtraReports.Extensions;
// ...
namespace ReportStorageSample {
static class Program {
static ReportStorageExtension reportStorage;
public static ReportStorageExtension ReportStorage {
get {
return reportStorage;
}
}
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
// This code registers a report storage that uses System.DataSet.
reportStorage = new DataSetReportStorage();
// Uncomment these lines to register a report storage that uses XPO.
//XpoDefault.DataLayer = XpoDefault.GetDataLayer(DevExpress.Xpo.DB.AccessConnectionProvider.GetConnectionString("ReportStorage.mdb"), DevExpress.Xpo.DB.AutoCreateOption.DatabaseAndSchema);
// reportStorage = new XpoReportStorage(new UnitOfWork());
// Uncomment this line to register a report storage, which uses Zip file.
// reportStorage = new ZipReportStorage();
ReportStorageExtension.RegisterExtensionGlobal(reportStorage);
Application.Run(new Form1());
}
}
}
C#using System;
using System.IO;
using System.Data;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections.Generic;
using DevExpress.XtraReports.UI;
using DevExpress.XtraReports.Extensions;
// ...
namespace ReportStorageSample {
class DataSetReportStorage : ReportStorageExtension {
const string fileName = "ReportStorage.xml";
StorageDataSet dataSet;
public DataSetReportStorage() {
}
string StoragePath {
get {
string dirName = Path.GetDirectoryName(Application.ExecutablePath);
return Path.Combine(dirName, fileName);
}
}
StorageDataSet DataSet {
get {
if (dataSet == null) {
dataSet = new StorageDataSet();
// Populate a dataset from an XML file specified in fileName.
if (File.Exists(StoragePath))
dataSet.ReadXml(StoragePath, XmlReadMode.ReadSchema);
}
return dataSet;
}
}
StorageDataSet.ReportStorageDataTable ReportStorage {
get {
return DataSet.ReportStorage;
}
}
public override bool CanSetData(string url) {
// Always return true to confirm that the SetData method is available.
return true;
}
public override bool IsValidUrl(string url) {
return !string.IsNullOrEmpty(url);
}
public override byte[] GetData(string url) {
// Get a dataset row containing the report.
StorageDataSet.ReportStorageRow row = FindRow(url);
if (row != null)
return row.Buffer;
return new byte[] { };
}
StorageDataSet.ReportStorageRow FindRow(string url) {
DataRow[] result = ReportStorage.Select(string.Format("Url = '{0}'", url));
if (result.Length > 0)
return result[0] as StorageDataSet.ReportStorageRow;
return null;
}
public override void SetData(XtraReport report, string url) {
StorageDataSet.ReportStorageRow row = FindRow(url);
// Write the report to a corresponding row in the dataset.
// If a row with a specified URL field value does not exist, create a new one.
if (row != null)
row.Buffer = GetBuffer(report);
else {
int id = ReportStorage.Rows.Count;
report.Extensions["StorageID"] = id.ToString();
row = ReportStorage.AddReportStorageRow(id, url, GetBuffer(report));
}
DataSet.WriteXml(StoragePath, XmlWriteMode.WriteSchema);
}
byte[] GetBuffer(XtraReport report) {
using (MemoryStream stream = new MemoryStream()) {
report.SaveLayout(stream);
return stream.ToArray();
}
}
public override string GetNewUrl() {
// Show the report selection dialog and return a URL for a selected report.
StorageEditorForm form = CreateForm();
form.textBox1.Enabled = false;
if (form.ShowDialog() == System.Windows.Forms.DialogResult.OK)
return form.textBox1.Text;
return string.Empty;
}
StorageEditorForm CreateForm() {
StorageEditorForm form = new StorageEditorForm();
foreach (string item in GetUrls())
form.listBox1.Items.Add(item);
return form;
}
public override string SetNewData(XtraReport report, string defaultUrl) {
StorageEditorForm form = CreateForm();
form.textBox1.Text = defaultUrl;
form.listBox1.Enabled = false;
// Show the save dialog to get a URL for a new report.
if (form.ShowDialog() == DialogResult.OK) {
string url = form.textBox1.Text;
if (!string.IsNullOrEmpty(url) && !form.listBox1.Items.Contains(url)) {
TypeDescriptor.GetProperties(typeof(XtraReport))["DisplayName"].SetValue(report, url);
SetData(report, url);
return url;
}
else {
MessageBox.Show("Incorrect report name", "Error",
MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
}
}
return string.Empty;
}
// The following code is intended to support selection of a value for
// the Report Source Url property of Subreport controls.
// (Use this code to avoid assigning the master report as a
// detail report's source.)
public override bool GetStandardUrlsSupported(ITypeDescriptorContext context) {
// Always return true to confirm that the GetStandardUrls method is available.
return true;
}
public override string[] GetStandardUrls(ITypeDescriptorContext context) {
if (context != null && context.Instance is XRSubreport) {
XRSubreport xrSubreport = context.Instance as XRSubreport;
if (xrSubreport.RootReport !=
null && xrSubreport.RootReport.Extensions.TryGetValue("StorageID", out storageID)) {
List<string> result = GetUrlsCore(CanPassId);
return result.ToArray();
}
}
return GetUrls();
}
string storageID;
bool CanPassId(string id) {
return id != storageID;
}
string[] GetUrls() {
return GetUrlsCore(null).ToArray();
}
List<string> GetUrlsCore(Predicate<string> method) {
List<string> list = new List<string>();
foreach (StorageDataSet.ReportStorageRow row in ReportStorage.Rows)
if (method == null || method(row.ID.ToString()))
list.Add(row.Url);
return list;
}
}
}
C#using System;
using System.IO;
using System.Windows.Forms;
using DevExpress.XtraReports.UserDesigner;
using DevExpress.XtraReports.UI;
// ...
namespace ReportStorageSample {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void buttonDesign_Click(object sender, EventArgs e) {
// Open a selected report in the report designer.
XRDesignRibbonForm form = new XRDesignRibbonForm();
string url = GetSelectedUrl();
if (!string.IsNullOrEmpty(url))
form.OpenReport(url);
form.ShowDialog(this);
object selectedItem = listBox1.SelectedItem;
FillListBox();
if (selectedItem != null && listBox1.Items.Contains(selectedItem))
listBox1.SelectedItem = selectedItem;
}
private void buttonPreview_Click(object sender, EventArgs e) {
// Show a preview for a selected report.
XtraReport report = GetSelectedReport();
if (report != null)
report.ShowRibbonPreviewDialog();
}
string GetSelectedUrl() {
return listBox1.SelectedItem as string;
}
XtraReport GetSelectedReport() {
// Return a report by a URL selected in the ListBox.
string url = GetSelectedUrl();
if (string.IsNullOrEmpty(url))
return null;
using (MemoryStream stream = new MemoryStream(Program.ReportStorage.GetData(url))) {
return XtraReport.FromStream(stream, true);
}
}
private void Form1_Load(object sender, EventArgs e) {
FillListBox();
if (listBox1.Items.Count > 0)
listBox1.SelectedIndex = 0;
}
void FillListBox() {
listBox1.Items.Clear();
string[] urls = Program.ReportStorage.GetStandardUrls(null);
foreach (string url in urls) {
listBox1.Items.Add(url);
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e) {
buttonPreview.Enabled = listBox1.SelectedItem != null;
}
}
}
C#using System;
using System.Windows.Forms;
// ...
namespace ReportStorageSample {
public partial class StorageEditorForm : Form {
public StorageEditorForm() {
InitializeComponent();
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e) {
textBox1.Text = listBox1.SelectedItem.ToString();
}
private void StorageEditorForm_Load(object sender, EventArgs e) {
if (listBox1.Items.Count > 0 && string.IsNullOrEmpty(textBox1.Text))
listBox1.SelectedIndex = 0;
}
private void textBox1_TextChanged(object sender, EventArgs e) {
buttonOK.Enabled = !string.IsNullOrEmpty(textBox1.Text);
}
private void buttonOK_Click(object sender, EventArgs e) {
}
}
}
C#using System;
using System.IO;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections.Generic;
using DevExpress.Xpo;
using DevExpress.Data.Filtering;
using DevExpress.XtraReports.UI;
using DevExpress.XtraReports.Extensions;
// ...
namespace ReportStorageSample {
class XpoReportStorage : ReportStorageExtension {
XPCollection<StorageItem> items;
UnitOfWork Session {
get { return (UnitOfWork)items.Session; }
}
public StorageItem FindItem(string name) {
return Session.FindObject<StorageItem>(new BinaryOperator("Url", name));
}
public XPCollection<StorageItem> Items {
get { return items; }
}
public XpoReportStorage(UnitOfWork session) {
items = new XPCollection<StorageItem>(session);
}
public override bool CanSetData(string url) {
// Always return true to confirm that the SetData method is available.
return true;
}
public override bool IsValidUrl(string url) {
return !string.IsNullOrEmpty(url);
}
public override byte[] GetData(string url) {
// Get a StorageItem containing the report.
StorageItem item = FindItem(url);
if (item != null)
return item.Layout;
return new byte[] { };
}
public override void SetData(XtraReport report, string url) {
// Write the report to a corresponding StorageItem.
// If a StorageItem with a specified Url property value does not exist, create a new one.
StorageItem item = FindItem(url);
if (item != null)
item.Layout = GetBuffer(report);
else {
item = new StorageItem(Session);
item.Url = url;
Session.CommitChanges();
report.Extensions["StorageID"] = item.Oid.ToString();
item.Layout = GetBuffer(report);
}
Session.CommitChanges();
items.Reload();
}
byte[] GetBuffer(XtraReport report) {
using (MemoryStream stream = new MemoryStream()) {
report.SaveLayout(stream);
return stream.ToArray();
}
}
public override string GetNewUrl() {
// Show the report selection dialog and return a URL for a selected report.
StorageEditorForm form = CreateForm();
form.textBox1.Enabled = false;
if (form.ShowDialog() == DialogResult.OK)
return form.textBox1.Text;
return string.Empty;
}
StorageEditorForm CreateForm() {
StorageEditorForm form = new StorageEditorForm();
foreach (string item in GetUrls())
form.listBox1.Items.Add(item);
return form;
}
public override string SetNewData(XtraReport report, string defaultUrl) {
StorageEditorForm form = CreateForm();
form.textBox1.Text = defaultUrl;
form.listBox1.Enabled = false;
// Show the save dialog to get a URL for a new report.
if (form.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
string url = form.textBox1.Text;
if (!string.IsNullOrEmpty(url) && !form.listBox1.Items.Contains(url)) {
TypeDescriptor.GetProperties(typeof(XtraReport))["DisplayName"].SetValue(report, url);
SetData(report, url);
return url;
}
else {
MessageBox.Show("Incorrect report name", "Error",
MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
}
}
return string.Empty;
}
public override bool GetStandardUrlsSupported(ITypeDescriptorContext context) {
// Always return true to confirm that the GetStandardUrls method is available.
return true;
}
public override string[] GetStandardUrls(ITypeDescriptorContext context) {
if (context != null && context.Instance is XRSubreport) {
XRSubreport xrSubreport = context.Instance as XRSubreport;
if (xrSubreport.RootReport !=
null && xrSubreport.RootReport.Extensions.TryGetValue("StorageID", out storageID)) {
List<string> result = GetUrlsCore(CanPassId);
return result.ToArray();
}
}
return GetUrls();
}
string storageID;
bool CanPassId(string id) {
return id != storageID;
}
string[] GetUrls() {
return GetUrlsCore(null).ToArray();
}
List<string> GetUrlsCore(Predicate<string> method) {
List<string> list = new List<string>();
foreach (StorageItem item in Items)
if (method == null || method(item.Oid.ToString()))
list.Add(item.Url);
return list;
}
}
public class StorageItem : XPObject {
string url;
byte[] layout = null;
public string Url {
get { return url; }
set { SetPropertyValue("Url", ref url, value); }
}
public byte[] Layout {
get { return layout; }
set { SetPropertyValue("Layout", ref layout, value); }
}
public StorageItem(Session session)
: base(session) {
}
}
}
C#using System;
using System.IO;
using System.Windows.Forms;
using System.ComponentModel;
using System.IO.Compression;
using System.Collections.Generic;
using DevExpress.Xpo;
using DevExpress.Data.Filtering;
using DevExpress.XtraReports.UI;
using DevExpress.XtraReports.Extensions;
using DevExpress.Utils.Zip;
// ...
namespace ReportStorageSample {
class ZipReportStorage : ReportStorageExtension {
class ZipFilesHelper : IDisposable {
Stream stream;
InternalZipFileCollection zipFiles = new InternalZipFileCollection();
public InternalZipFileCollection ZipFiles {
get {
return zipFiles;
}
}
public ZipFilesHelper(string path) {
if (File.Exists(path)) {
stream = File.OpenRead(path);
zipFiles = InternalZipArchive.Open(stream);
}
}
public virtual void Dispose() {
if (stream != null)
stream.Dispose();
}
}
const string fileName = "ReportStorage.zip";
public ZipReportStorage() {
}
string StoragePath {
get {
string dirName = Path.GetDirectoryName(Application.ExecutablePath);
return Path.Combine(dirName, fileName);
}
}
public override bool CanSetData(string url) {
// Always return true to confirm that the SetData method is available.
return true;
}
public override bool IsValidUrl(string url) {
return !string.IsNullOrEmpty(url);
}
public override byte[] GetData(string url) {
// Open ZIP archive.
using (ZipFilesHelper helper = new ZipFilesHelper(StoragePath)) {
// Read a file with a specified URL from the archive.
InternalZipFile zipFile = GetZipFile(helper.ZipFiles, url);
if (zipFile != null)
return GetBytes(zipFile);
return new byte[] { };
}
}
static byte[] GetBytes(InternalZipFile zipFile) {
return GetBytes(zipFile.FileDataStream, (int)zipFile.UncompressedSize);
}
static byte[] GetBytes(Stream stream, int length) {
byte[] result = new byte[length];
DevExpress.Utils.Helpers.StreamHelper.FillBuffer(stream, result, 0, length);
return result;
}
static InternalZipFile GetZipFile(InternalZipFileCollection zipFiles, string url) {
foreach(InternalZipFile item in zipFiles) {
if (StringsEgual(item.FileName, url))
return item;
}
return null;
}
static bool StringsEgual(string a, string b) {
return string.Equals(a, b, StringComparison.OrdinalIgnoreCase);
}
public override void SetData(XtraReport report, string url) {
report.Extensions["StorageID"] = url;
SaveArchive(url, GetBuffer(report));
}
void SaveArchive(string url, byte[] buffer) {
string tempPath = Path.ChangeExtension(StoragePath, "tmp");
// Create a new ZIP archive.
using(InternalZipArchive arch = new InternalZipArchive(tempPath)) {
// Open a ZIP archive where report files are stored.
using (ZipFilesHelper helper = new ZipFilesHelper(StoragePath)) {
bool added = false;
// Copy all report files to a new archive.
// Update a file with a specified URL.
// If the file does not exist, create it.
foreach(InternalZipFile item in helper.ZipFiles) {
if (StringsEgual(item.FileName, url)) {
arch.Add(item.FileName, DateTime.Now, buffer);
added = true;
}
else
arch.Add(item.FileName, DateTime.Now, GetBytes(item));
}
if (!added)
arch.Add(url, DateTime.Now, buffer);
}
}
// Replace the old ZIP archive with the new one.
if (File.Exists(StoragePath))
File.Delete(StoragePath);
File.Move(tempPath, StoragePath);
}
byte[] GetBuffer(XtraReport report) {
using (MemoryStream stream = new MemoryStream()) {
report.SaveLayout(stream);
return stream.ToArray();
}
}
public override string GetNewUrl() {
// Show the report selection dialog and return a URL for a selected report.
StorageEditorForm form = CreateForm();
form.textBox1.Enabled = false;
if (form.ShowDialog() == System.Windows.Forms.DialogResult.OK)
return form.textBox1.Text;
return string.Empty;
}
StorageEditorForm CreateForm() {
StorageEditorForm form = new StorageEditorForm();
foreach (string item in GetUrls())
form.listBox1.Items.Add(item);
return form;
}
public override string SetNewData(XtraReport report, string defaultUrl) {
StorageEditorForm form = CreateForm();
form.textBox1.Text = defaultUrl;
form.listBox1.Enabled = false;
// Show the save dialog to get a URL for a new report.
if (form.ShowDialog() == DialogResult.OK) {
string url = form.textBox1.Text;
if (!string.IsNullOrEmpty(url) && !form.listBox1.Items.Contains(url)) {
TypeDescriptor.GetProperties(typeof(XtraReport))["DisplayName"].SetValue(report, url);
SetData(report, url);
return url;
}
else {
MessageBox.Show("Incorrect report name", "Error",
MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
}
}
return string.Empty;
}
public override bool GetStandardUrlsSupported(ITypeDescriptorContext context) {
// Always return true to confirm that the GetStandardUrls method is available.
return true;
}
public override string[] GetStandardUrls(ITypeDescriptorContext context) {
if (context != null && context.Instance is XRSubreport) {
XRSubreport xrSubreport = context.Instance as XRSubreport;
if (xrSubreport.RootReport !=
null && xrSubreport.RootReport.Extensions.TryGetValue("StorageID", out storageID)) {
List<string> result = GetUrlsCore(CanPassId);
return result.ToArray();
}
}
return GetUrls();
}
string storageID;
bool CanPassId(string id) {
return id != storageID;
}
string[] GetUrls() {
return GetUrlsCore(null).ToArray();
}
List<string> GetUrlsCore(Predicate<string> method) {
List<string> list = new List<string>();
using (ZipFilesHelper helper = new ZipFilesHelper(StoragePath)) {
foreach(InternalZipFile item in helper.ZipFiles)
if (method == null || method(item.FileName))
list.Add(item.FileName);
return list;
}
}
}
}