Blogs

DevExpress Data Blog

This blog features all things having to do with data shaping, layout, and presentation!

Custom Report Templates – Using In-House Templates

In our previous write-up we chatted about report templates in general as well as how to create them. I wanted to spend a few minutes (as promised) to show you how to use your own templates within your applications as a means to assist end users to get up and running with internal reporting. Oftentimes companies have the same stylistic and structural elements across their large set of reports. In other words, when someone tells me they have hundreds of reports they really have a smaller set of “base” reports that contain a certain aesthetic and structure to them. Whenever your end users want to create a new Product report, for example, what they really mean is that they want to create a variation on an existing product report. When end users are confronted with a blank report they will often use the developers as a safety valve to help them get started. Instead of constantly facing that endless time sink-hole, why not present them with a dialog that has your basic reports and let the users chose from that set.

templates

How hard is this to do then? It is truly quite simple and can be accomplished in two simple steps:

  1. Create a report template extension by inheriting from the ReportTemplateExtension class
  2. Register the extension

Once these two steps are complete, your custom end user report designer will show the “Load Report Template” link that…

load

The Code

The first step mentioned was creating a descendant of the ReportTemplateExtension abstract class. This is implemented by overriding the GetTemplates( ) function. You are truly free to retrieve the templates from whatever storage medium you see fit. In this instance I simply have all of the templates tucked away into a Templates folder.

public class CustomTemplate : ReportTemplateExtension
{
    public override IEnumerable<Template> GetTemplates()
    {
        var archive = new TemplateArchiveManager();
        string path = Path.Combine(Application.StartupPath, "Templates");
        var files = Directory.GetFiles(path);

        var templates = new List<Template>(files.Length);

        foreach (string file in files)
            using (Stream stream = new MemoryStream(File.ReadAllBytes(file)))
                templates.Add(archive.GetTemplatesFromArchive(stream));

        return templates;
    }
}

The second step was to register the extension with the reporting system. This is easily done with the following line of code:

ReportTemplateExtension.RegisterExtensionGlobal(new CustomTemplate());

This should be done when the application first starts.

That should do it! In two simple steps we can now enable our end users to quickly and easily get started with reports with custom templates. I would love to see how this feature is used!

As always, if there are any comments and/or questions, feel free to get a hold of me!

Seth Juarez
Email: sethj@devexpress.com
Twitter: @SethJuarez

Want The Best Reporting Tool Ever?

Get The No-Compromise Reporting Tool for WinForms, ASP.NET, Silverlight and WPF! - Native integration with DevExpress WinForms and ASP.NET Controls, unequalled design-time productivity, industrial-grade features. Try a fully-functional version of DXperience for free now: http://www.devexpress.com/Downloads/NET/

Let us know what you think of our Reporting Suite by rating it in the VS Gallery!

Follow SethJuarez on Twitter

Published Jul 26 2012, 08:00 AM by Seth Juarez (DevExpress)
Bookmark and Share

Comments

renejdm

Great blog. Looks easy...

July 26, 2012 3:37 PM

Unique Display Name

How would you handle the scenario where you want to change the available templates depending on some other variable?

e.g I may have a "statements" editor and an "invoices" editor both of which have their own non-interchangeable templates.

When in the "invoice" editor, I only want to show invoice templates and when in the statement editor, show only statement templates.

July 27, 2012 4:14 AM

Seth Juarez (DevExpress)

Great question! When you create a descendant of the ReportTemplateExtension you can have additional state in the new class indicating the type of templates the user can view. In this case you would simply check the current state of this new class and enumerate the available templates. I hope this helps! If not, shoot me an email. I would be happy to help further.

August 2, 2012 4:27 PM
LIVE CHAT

Chat is one of the many ways you can contact members of the DevExpress Team.
We are available Monday-Friday between 7:30am and 4:30pm Pacific Time.

If you need additional product information, write to us at info@devexpress.com or call us at +1 (818) 844-3383

FOLLOW US

DevExpress engineers feature-complete Presentation Controls, IDE Productivity Tools, Business Application Frameworks, and Reporting Systems for Visual Studio, along with high-performance HTML JS Mobile Frameworks for developers targeting iOS, Android and Windows Phone. Whether using WPF, Silverlight, ASP.NET, WinForms, HTML5 or Windows 8, DevExpress tools help you build and deliver your best in the shortest time possible.

Your Privacy - Legal Statements

Copyright © 1998-2013 Developer Express Inc.
ALL RIGHTS RESERVED
All trademarks or registered trademarks
are property of their respective owners