Xceed Grid for WinForms v4.3 Documentation
Report styles and report style sheets

Welcome to Xceed Grid for WinForms v4.3 > Basic Concepts > Reporting > Report styles and report style sheets

The styles used in reports cover their visual aspects, such as color, font, spacing, etc. 

It is important to make a few distinctions between the ReportStyle properties and the styles in the ReportStyleSheet class. The ReportStyle properties of the various grid elements are not perfectly mirrored in ReportStyleSheet. For example, GridControl.DataRows is a collection of DataRow objects, which each contain Cell objects representing the cells contained in the row. Each DataRow object has its own ReportStyle property, as does each Cell object. Consequently, you can change the background color that will be used in a report for a specific Cell object by using the following code:

VB.NET
Copy Code
Imports Xceed.Grid.Reporting
' Using Microsoft's Northwind database
Me.gridControl1.DataRows( 0 ).Cells( "CustomerID" ).ReportStyle.BackColor = Color.Cyan
C#
Copy Code
using Xceed.Grid.Reporting;
// Using Microsoft's Northwind database
this.gridControl1.DataRows[ 0 ].Cells[ "CustomerID" ].ReportStyle.BackColor = Color.Cyan;

This will change the background color of the first cell of the first data row to cyan when a report is created from this GridControl.

By contrast, ReportStyleSheet.Grid.DataRows is a collection of styles that are applied cyclically to data rows. For example, the following code will produce a very colorful report whose data rows cycle through red, green and dark orange.

VB.NET
Copy Code
Dim report As New Report( Me.gridControl1 )
report.ReportStyleSheet.Grid.DataRows.Add( New RowReportStyle() )
report.ReportStyleSheet.Grid.DataRows.Add( New RowReportStyle() )report.ReportStyleSheet.Grid.DataRows.Add( New RowReportStyle() )
Dim reportStyleSheet As report.ReportStyleSheet
reportStyleSheet.Grid.DataRows( 0 ).BackColor = Color.Red
reportStyleSheet.Grid.DataRows( 1 ).BackColor = Color.Green
reportStyleSheet.Grid.DataRows( 2 ).BackColor = Color.DarkOrange
report.PrintPreview();
C#
Copy Code
Report report = new Report( this.gridControl1 );
report.ReportStyleSheet.Grid.DataRows.Add( new RowReportStyle() );
report.ReportStyleSheet.Grid.DataRows.Add( new RowReportStyle() );
report.ReportStyleSheet.Grid.DataRows.Add( new RowReportStyle() );
ReportStyleSheet reportStyleSheet = report.ReportStyleSheet;
reportStyleSheet.Grid.DataRows[ 0 ].BackColor = Color.Red;
reportStyleSheet.Grid.DataRows[ 1 ].BackColor = Color.Green;
reportStyleSheet.Grid.DataRows[ 2 ].BackColor = Color.DarkOrange;
report.PrintPreview();

Of course, this may not be a very realistic use of the Grid’s reporting capabilities, but it serves to illustrate the concept. Because ReportStyleSheet.Grid.DataRows does not refer to DataRow objects, there is no way to set the color (or any other property) of a particular cell through a ReportStyleSheet object. 

As a result, ReportStyleSheet is used for a more general configuration of the appearance of a printed report and serves as a convenient way of grouping the report styles of a report in one object, whereas the ReportStyle properties of the grid elements let you control more precisely the appearance of specific grid elements in a report. 

Note that ReportStyle properties set explicitly in the GridControl override any properties set in a ReportStyleSheet object. Consequently, combining the previous two code snippets will result in a report whose data rows cycle through red, green and dark orange, but in which the first cell of the first row will be Cyan, despite the fact that the back color of the data rows is specified after the color of the first cell of the first data row is set.  To stop a grid element's ReportStyle property from overriding a property in a ReportStyleSheet object, you must call one of that element's "reset" methods. Reset methods exist for specific properties, but also for all of the properties set explicitly in a report style as a whole:

VB.NET
Copy Code
' Resets the back color of the first cell of the first data row; will no longer
' override settings in a ReportStylSheet
Me.gridControl1.DataRows( 0 ).Cells( "CustomerID" ).ReportStyle.ResetBackColor()
' Resets any property set explicitly in the ReportStyle
Me.gridControl1.DataRows( 0 ).Cells( "CustomerID" ).ReportStyle.Reset()
C#
Copy Code
// Resets the back color of the first cell of the first data row; will no longer
// override settings in a ReportStylSheet
this.gridControl1.DataRows[ 0 ].Cells[ "CustomerID" ].ReportStyle.ResetBackColor();
// Resets any property set explicitly in the ReportStyle
this.gridControl1.DataRows[ 0 ].Cells[ "CustomerID" ].ReportStyle.Reset();
ReportStyleSheet objects also serve as a way of manipulating the appearance of a report without having to directly modify the properties of a grid control.

Ambientness

Both the ReportStyle properties of the grid elements and the various styles contained in the ReportStyleSheet class (for example, reportStyleSheet.Grid.DataRows) feature ambientness. That is, when a value is not explicitly set in a report style property, the value is inherited from an ambient parent. For details, see Ambientness and reports.

GridReportStyle

The GridReportStyle class represents the report style of a grid and provides an "entry point" to a hierarchy of ReportStyle properties that let you control the style of various elements of a grid report. 

The properties discussed in this section are typically accessed through the ReportStyleSheet.Grid property or the GridControl.ReportStyle property, which contain a reference to GridControlReportStyle, which is in turn derived from GridReportStyle. Three properties in particular deserve special mention.

The DataRows property

The DataRows property contains a reference to a RowReportStyleList object containing a list of RowReportStyle objects. The styles are applied to the data rows contained in the report by cycling through the styles represented by the RowReportStyle objects.

The GroupLevels property

The GroupLevels property contains a reference to a GroupReportStyleList object containing a list of GroupReportStyle objects. The styles are applied to groups of same level contained in the report. For example, the following code sets the BackColor of two GroupReportStyle objects in the GroupLevels collection to red and green, respectively:

VB.NET
Copy Code
Me.gridControl1.ReportStyle.GroupLevels.Add( New GroupReportStyle() )
Me.gridControl1.ReportStyle.GroupLevels.Add( New GroupReportStyle() )
Me.gridControl1.ReportStyle.GroupLevels( 0 ).BackColor = Color.Red
Me.gridControl1.ReportStyle.GroupLevels( 1 ).BackColor = Color.Green
C#
Copy Code
this.gridControl1.ReportStyle.GroupLevels.Add( new GroupReportStyle() );
this.gridControl1.ReportStyle.GroupLevels.Add( new GroupReportStyle() );
this.gridControl1.ReportStyle.GroupLevels[ 0 ].BackColor = Color.Red;
this.gridControl1.ReportStyle.GroupLevels[ 1 ].BackColor = Color.Green;
In a report generated from this grid control, all level-one groups will be red and all level-two groups will be green. However, if the number of groups in the grid is greater than the number of GroupReportStyle objects in the GroupLevels property, the GroupReportStyle last added will be used. With this example code, if any additional group were added to the grid, Green would be used, which was the last color added to the GroupLevels property.

The DetailGrids property

The DetailGrids property contains a reference to a GridReportStyleList object containing a list of GridReportStyle objects which will by applied to the DetailGrids contained in the report by cycling through the GridReportStyleList object. Note that the DetailGrids property is itself a reference to a GridReportStyle object, because a detail grid is a grid within a grid. 

The DetailGrids property is important because it is not possible to modify the style of detail grids through CustomizeReportStyleForm. It must be done programmatically using the GridReportStyle.DetailGrids property:

VB.NET
Copy Code
Me.gridControl1.ReportStyle.DetailGrids.Add( New GridReportStyle() )Me.gridControl1.ReportStyle.DetailGrids.Add( New GridReportStyle() )
Me.gridControl1.ReportStyle.DetailGrids( 0 ).BackColor = Color.Red
Me.gridControl1.ReportStyle.DetailGrids( 1 ).BackColor = Color.Green
C#
Copy Code
this.gridControl1.ReportStyle.DetailGrids.Add( new GridReportStyle() );
this.gridControl1.ReportStyle.DetailGrids.Add( new GridReportStyle() );
this.gridControl1.ReportStyle.DetailGrids[ 0 ].BackColor = Color.Red;
this.gridControl1.ReportStyle.DetailGrids[ 1 ].BackColor = Color.Green;
Each GridReportStyle added to the DetailGrids collection will provide the report style for the corresponding DetailGrid template. For example, if the grid for which the report is being created contains two DetailGridTemplates, the first report style added to the list will be applied to the first template, the second to the second template, and so on. If there are more report styles than DetailGrid templates, the extra report styles are ignored. If on the other hand there are more DetailGrid templates than report styles, the report engine will cycle through the styles, similarly to what is done with the the DataRows property (see above).

The HeaderFooter and HeaderFooterElement classes

The sections displayed above and below each page of a report—that is, the header and footer—are each represented by a HeaderFooter object. Note that there are only two objects of this type per report, as the same header and footer are repeated for each page. 

Each HeaderFooter object is in turn made of up three HeaderFooterElement objects displayed horizontally from left to right: LeftElement, CenterElement, and RightElement

The HeaderFooter objects used in a report can be modified in the Customize Report Style form, in the Report Elements list box (Page Header and Page Footer). They can also be accessed programmatically through the PageHeader and PageFooter properties of the ReportStyleSheet class.

VB.NET
Copy Code
Dim report As New Report( Me.gridControl1 )
report.ReportStyleSheet.PageHeader.LeftElement.Font = New Font( "Arial", 14 )
report.ReportStyleSheet.PageHeader.LeftElement.TextFormat = "Strictly confidential"
C#
Copy Code
Report report = new Report( this.gridControl1 );
report.ReportStyleSheet.PageHeader.LeftElement.Font = new Font( "Arial", 14 );
report.ReportStyleSheet.PageHeader.LeftElement.TextFormat = "Strictly confidential";
The preceding code sets the font of the left element to 14-point Arial and sets the text to “Strictly confidential”. 

Additionally, the HeaderFooter class contains properties that let you control the maximum and minimum height the header or footer, as well as the appearance of their top or bottom border. 

HeaderFooterElement objects can be neither removed from nor added to the HeaderFooter object.

Loading and saving report style sheets

Report style sheets can be managed by end-users in the Customize Report Style form, but they can also be handled programmatically by the developer. The static Load methods return a ReportStyleSheet object that can be assigned to the ReportStyleSheet property of a Report object. In the following example, the path and name of an XML file is passed to the Load method as a string. Another version of the Load method takes a stream as its argument.

VB.NET
Copy Code
Imports Xceed.Grid.Reporting
Dim report As New Report( Me.gridControl1 )
report.ReportStyleSheet = ReportStyleSheet.Load( "D:\styleFolder\marketing.xml" )
report.PrintPreview()
C#
Copy Code
using Xceed.Grid.Reporting;
Report report = new Report( this.gridControl1 );
report.ReportStyleSheet = ReportStyleSheet.Load( "D:\\styleFolder\\marketing.xml" );
report.PrintPreview();
Similarly, a ReportStyleSheet object can be saved to an XML file using the Save method. In the following example, a string representing the path and name of the file to be saved is used. In addition, a boolean value indicates whether or not an existing file should be overwritten or not. Another version of the Save method only takes a stream as its argument.
VB.NET
Copy Code
Imports Xceed.Grid.Reporting
Dim report As New Report( Me.gridControl1 )
' Make modifications to the properties in report.ReportStyleSheet
report.ReportStyleSheet.Grid.BackColor = Color.Gray
report.ReportStyleSheet.Save( "D:\styleFolder\accounting.xml", True )
C#
Copy Code
using Xceed.Grid.Reporting;
Report report = new Report( this.gridControl1 );
// Make modifications to the properties in report.ReportStyleSheet
report.ReportStyleSheet.Grid.BackColor = Color.Gray;
report.ReportStyleSheet.Save( "D:\\styleFolder\\accounting.xml", true );

For your convenience, the ReportStyleSheets embedded in the control are available as XML files (default installation directory: [Program Files]\Xceed\Xceed Grid for WinForms [version]\Report StyleSheets). You can use these files as you see fit, including deploying them to your clients.