How to Change Crystal Report Formatting for Different Customers - Part 2
page 4 of 6
by Eric Landes
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 40444/ 53

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.


View Entire Article

User Comments

No comments posted yet.

Product Spotlight
Product Spotlight 



Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-05-28 11:20:36 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search