AspAlliance.com LogoASPAlliance: Articles, reviews, and samples for .NET Developers
URL:
http://aspalliance.com/articleViewer.aspx?aId=1713&pId=-1
How to Change Crystal Report Formatting for Different Customers - Part 1
page
by Eric Landes
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 44596/ 123

Introduction

One of the issues I have heard discussed is how to create different looks for the same report.  When creating reports for different customers, sometimes you may need to display the same crystal report, but with different formatting.  This can apply for commercial software development groups and internal groups. Many companies have their own standard colors that internal/external documents should conform to.  We will look at how we can create one Crystal report and then deploy it to the customer with different formatting options.   

We assume familiarity with how to create Crystal Reports and format them. The reader should know how to create reports and design them in Crystal Reports .NET. We also assume that there is some familiarity with programming in .NET languages. The examples for this article will be in C#.

System Requirements

·         Visual Studio 2008

·         Crystal Reports .NET

·         SQL Server 2005

·         Adventureworks database

Formatting Options

Depending on the type of application you are deploying, we will discuss different methods of changing the formatting on your report. For web applications, you can utilize style sheets used in your web application to format your report. If you deploy the web application to different servers, the CSS file can be different formatting on each server. If your reports are going to be in the same server, I would recommend placing a different aspx file with the Crystal Viewer for each customer.

For a Windows application there is no out of the box solution to setting the formatting outside of an application like the CSS file. We will introduce a way to change the formatting in your windows application with some custom code in Part 2 of this series.

For our example we start out with a sales report we created using the Adventureworks database.  We will base the report off the view vSalesPersonSalesByFiscalYears. The sample report displays data by region, then by sales person. The sales show totals by year, and then grand totals at the bottom. See the SQL used in the report in Listing 1.

Listing 1

SELECT "vSalesPersonSalesByFiscalYears"."FullName", 
"vSalesPersonSalesByFiscalYears"."SalesTerritory", 
"vSalesPersonSalesByFiscalYears"."2002", "vSalesPersonSalesByFiscalYears"."2003", 
"vSalesPersonSalesByFiscalYears"."2004"
 FROM   "AdventureWorks"."Sales"."vSalesPersonSalesByFiscalYears" 
"vSalesPersonSalesByFiscalYears"
 ORDER BY "vSalesPersonSalesByFiscalYears"."SalesTerritory", 
"vSalesPersonSalesByFiscalYears"."FullName"
Custom Formatting your Web Application

Implementing different formatting for your clients using a web application comes out of the box in Crystal Reports .NET. When designing your report, you may notice the option Set CSS Class on the Crystal Reports menu. You can set these classes at design time. See Figure 1 for a screen shot of what this looks like.    

Figure 1

Using the "Set CSS Class" menu option sets the CSS class name in the property of each object for the section selected. For instance, if I select the option AllReportObjectsInGroupHeaderSections, and set the class name to "h1" then each object will have an "h1" in its CSS class property set.  This can be verified by right clicking on the object and selecting Format Object.

These classes assume that you have set up the elements or CSS class names somewhere in your aspx file. In most instances, this means that you have defined your class names in a CSS file referenced in the aspx file displaying your report. In design time you do not have a drop down or intellisense for the available CSS Class names. I heartily recommend this as a new feature Crystal can add to your product. 

You can also set these classes in code. Keep in mind that setting your classes at Design time makes it more difficult to set classes programmatically. You need to write some code to clear each objects class at programming time, and then you can set them. Let us see how we set your CSS classes in code. See listing 2 for the C# code on how to set your class.

Listing 2

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Sales crToShow = new Sales();
crToShow.SetCssClass(CrystalDecisions.Shared.ObjectScope.PageHeaderSections, "h1");
crToShow.SetCssClass(CrystalDecisions.Shared.ObjectScope.DetailSections, "body"); 
crToShow.SetCssClass(CrystalDecisions.Shared.ObjectScope.GroupHeaderSections, "h1");
crToShow.SetCssClass(
CrystalDecisions.Shared.ObjectScope.AllReportObjectsInReportHeaderSections, "h1");
CrystalReportViewer1.ReportSource=crToShow;
CrystalReportViewer1.DataBind();
}
}

To set the class, we need to use the reportdocument object of the report we want to display. In this application we use the Sales report, which is part of our project. In this report we want to set the page header section to use the h1 element in the stylesheet ourstyles.css that the default.aspx page uses. To set the page header section we use the ObjectScope object PageHeaderSections. 

One important point to note here is that you set the CSS style by sections. That is the scope used in the SetCSSClass. So there is not a way to set your style by object within the report, but within sections.

Setting different styles for your application

So now that we know how to set your reports style, let us see how we can deploy our application for different customers. Our custom web application is geared to be sold to for Outdoors retail shops. Our outdoors retail application named AdventureWorks allows the customer to use our hosted solution, where they do not need their own server, or they can install the application on their own web server.

We will assume that customer A has his own web server. When he deploys the web application, the installation copies the standard CSS file (named OurStyles.css) and reports. Once installed, Customer A's IT staff or anyone familiar with CSS can modify the OurStyles.css file to utilize their own look and feel. The style sheet should include comments to let the developer know what section that class name is used for. To change the Detail section, simply add the font, etc. to the class name. 

A hosted solution of our AdventureWorks application is deployed much the same way. Each website is set up independent of others, so the ourstyles.css file can be placed in your hosted FTP site. To modify it, the customer simply downloads the file, modifies it locally, and then uploads the file back to their server. We can also try to integrate something like the User Interface for modifying CSS files from DotNetNuke, to make modification easier, but that is beyond the scope of this article. 

Listing 3

/* body is used in the Detail Section */
body 
{
      font-family: 'Times New Roman' , Times, serif;
      font-size: medium;
      color: #000080;
      font-weight: bold;
      text-decoration: underline;
}
/* h1 is used in the Page Header Section */
h1
{
      font-family: 'Times New Roman';
      font-size: x-large;
      font-weight: bold;
      color: #008000;
      background-color: #C0C0C0;
}
/*h2 is used for the Group Header Section */
h2
{
      font-family: Arial;
      font-size: x-large;
      font-weight: bolder;
      color: #00FF00;
      background-color: #000080;
} 
Downloads
Summary

In this article of our series, we have gone over how to set the CSS class for objects in your report for a web application. As mentioned, this is fairly straight forward for the ASP.NET reports. In the next part we will go over how to do this for a Windows Application, which is a bit more difficult. We demonstrated that using the SetCSSClass method in the ReportDocument object makes the ability to change formatting easier than in a windows application. We also mentioned that the class can also be set at design time.

Hopefully this helps for anyone creating commercial or internally developed software that needs a different look and feel for various customers. Keep on reporting, and happy coding!


Product Spotlight
Product Spotlight 

©Copyright 1998-2014 ASPAlliance.com  |  Page Processed at 10/21/2014 7:51:00 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search