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

Introduction

In Part 1 of this series, we looked at the custom formatting options available to Crystal reports developers when deploying applications. Custom formatting helps those folks who develop and resell custom applications. Their customers may want to customize the formatting of their reports to include company colors, maybe even logos. This topic is also appropriate for internal developers who develop applications across departments, giving those departments the ability to customize their report formatting.    

It is assumed the reader is familiar with how to create Crystal Reports and format them. The reader should have some familiarity with programming and programming concepts 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

We covered how to customize reports displayed in web applications in Part 1. In Part 2 we will cover how to implement something similar for your Windows Form application. Crystal Reports .NET includes the ability to use CSS style sheets for your web application. Unfortunately there is no built in ability to use something similar to CSS Style sheets in your WinForm application. 

To implement something similar to a style sheet, our examples involve using custom XML file to describe the formatting for your reports. Our WinForm application uses a custom function to apply the formatting specified in the XML file to our Report. Listing 1 shows the SQL syntax used in our report. We use the Adventureworks database and tables to create a sales report. Once we have the report that we want to display, now we need to change the formatting in our windows application.

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 Windows Application

To make our formatting easily modifiable, use an XML file with different nodes.  For this article, there is an XML file with the following format in Listing 2.

Listing 2

<Formats>
      <PageHeader>
            <FontFamily>Arial</FontFamily>
            <FontSize>16</FontSize>
            <FontWeight>Bold</FontWeight>
            <FontColor>Navy</FontColor>
            <BackGroundColor>Gold</BackGroundColor>
      </PageHeader>
      <GroupHeader>
            <FontFamily>Arial</FontFamily>
            <FontSize>14</FontSize>
            <FontWeight>Bold</FontWeight>
            <FontColor>Navy</FontColor>
      </GroupHeader>
</Formats>

The xml contains nodes that should map to different sections in the report. For instance, for the PageHeader section, we have a node PageHeader. You can create a different node for each section in a report (ReportHeader, PageFooter, etc.). Each section can contain the formatting options for the font name (fontfamily), FontSize, FontWeight, FontColor and BackgroundColor.  That is the extent of the sample in this article. This file could be extended to add other formatting options. For the purposes of this article, the number of elements is kept small.

Crystal Objects Formatting

Now we have a flexible format to store our formatting choices. Let us see how we can programmatically change the formatting. To customize our report, we have to use some of the objects in the CrystalDecisions.CrystalReports.Engine namespace. Specifically, we want to use methods in the FieldObject, and TextObject objects. Methods like ApplyFont can be used to modify the format of each of these different object types. To use ApplyFont, the object type needs to be correct.

To get to each different object, the code needs access to the section and the objects in that section to be modifed. To get that access, in the report object model, there are different section collections. For instance, Section2 is the PageHeader (you can determine this by checking the Kind property). Section1 is the ReportHeader section and so on. There are also GroupSections to help you get access to the objects in those sections as well. 

Once you grab the report section you want to apply formatting to, you can iterate through the objects in that section. To actually do the formatting, the easiest way I have found is to first use ReportObjects. You cannot directly modify formatting on the reportobject object. You need to cast it to a TextObject, FieldObject or some other kind of Crystal object with formatting methods and properties.

In our example we use the text and field objects. After casting the report object to the appropriate object, we can apply fonts and colors. To use ApplyFont to set your font, you need to use the System.Drawing.Font object. To set color, we need to return the System.Drawing.Color type.

Listing 3

public DisplayReport()
{
  InitializeComponent();
  CrystalReport1 crToDisplay = new CrystalReport1();
  DataSet dsFormatting = new DataSet();
  dsFormatting.ReadXml(@"C:\Dev\TestingCR\TestingCR\Formatting.xml");
  DataTable dtFormatting = dsFormatting.Tables["PageHeader"];
  DataRow drRow = dtFormatting.Rows[0];
  foreach (CrystalDecisions.CrystalReports.Engine.ReportObject rptObject in
    crToDisplay.Section2.ReportObjects)
  {
    CrystalDecisions.CrystalReports.Engine.FieldObject fldObject;
    CrystalDecisions.CrystalReports.Engine.TextObject txtObject;
 
    System.Drawing.Font font = new System.Drawing.Font
      (drRow["FontFamily"].ToString(), System.Convert.ToInt32
      (drRow["FontSize"].ToString()));
    switch (rptObject.Kind.ToString())
    {
      case "FieldObject":
        fldObject = (CrystalDecisions.CrystalReports.Engine.FieldObject)
          rptObject;
        fldObject.ApplyFont(font);
        fldObject.Color = GetColorFromString(drRow["FontColor"].ToString());
        break;
      case "TextObject":
        txtObject = (CrystalDecisions.CrystalReports.Engine.TextObject)
          rptObject;
        txtObject.ApplyFont(font);
        txtObject.Color = GetColorFromString(drRow["FontColor"].ToString());
        break;
      default:
        break;
 
    }
 
  }
  crToDisplay.Section2.SectionFormat.BackgroundColor = GetColorFromString
    (drRow["BackgroundColor"].ToString());
  crystalReportViewer1.ReportSource = crToDisplay;
}
 
private System.Drawing.Color GetColorFromString(String colorName)
{
  switch (colorName)
  {
    case "Red":
      return System.Drawing.Color.Red;
    case "Gold":
      return System.Drawing.Color.Gold;
    default:
      return System.Drawing.Color.Black;
  }
}

Notice in our code we load our xml file into a DataSet. You can use different methods of doing this, but I find the dataset the easiest way to load an XML file, and then manipulate the file in memory. In this example, each of the sections comes over as a Table. Our sample XML file puts the PageSection node as the first table. Within that table the program can grab the first row (there is one 1) and use the column name to help set the formatting. So in the line System.Drawing.Font font = new System.Drawing.Font(drRow["FontFamily"].ToString(), System.Convert.ToInt32(drRow["FontSize"].ToString())); the datarow drRow has a column FontFamily which we use to set the font name. Then the column FontSize is used to set the size (using points as the size type). 

This sample abstracts the color issue by using a method GetColorFromString to return the Drawing.Color object to the property or method that needs it. This way we can pass a string to the method, and set the color.

This particular sample sets the formatting in the Opening of the form. And it is set when bound to the viewer. There may be occasions where it should be set from selections in a dropdown or in different scenarios.

Downloads
Summary

This article has shown you how to programmatically set the formatting for your Crystal Report in a Windows Form. In Part 1 of this series we went over doing this for a web application. Crystal does have built in support for using style sheets to modify the formatting of reports. There is no similar mechanism to use in Crystal.NET for formatting your reports in a Windows Form. By using the different objects in the report document given to use in the API, and creating our own XML version of a CSS file, we can easily provide custom formatting for the same report. 


Product Spotlight
Product Spotlight 

©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-24 8:26:24 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search