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.
CrystalReport1 crToDisplay = new CrystalReport1();
DataSet dsFormatting = new DataSet();
DataTable dtFormatting = dsFormatting.Tables["PageHeader"];
DataRow drRow = dtFormatting.Rows;
foreach (CrystalDecisions.CrystalReports.Engine.ReportObject rptObject in
System.Drawing.Font font = new System.Drawing.Font
fldObject = (CrystalDecisions.CrystalReports.Engine.FieldObject)
fldObject.Color = GetColorFromString(drRow["FontColor"].ToString());
txtObject = (CrystalDecisions.CrystalReports.Engine.TextObject)
txtObject.Color = GetColorFromString(drRow["FontColor"].ToString());
crToDisplay.Section2.SectionFormat.BackgroundColor = GetColorFromString
crystalReportViewer1.ReportSource = crToDisplay;
private System.Drawing.Color GetColorFromString(String colorName)
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.