ReportDocument


The CrystalReportViewer control provides properties and methods for manipulating report output programmatically. For example, you can set the control’s DisplayGroupTree and ShowGroupTreeButton properties to False to prevent the user from displaying the group tree on the control’s left.

The CrystalReportViewer does not provide properties and methods that work with the report itself, however. For example, it cannot export the report into a file.

The ReportDocument object serves as an intermediate layer between a report and the CrystalReportViewer control to give your program more control over the report. ReportDocument provides access to a much larger object model that lets you interact with the report’s sections, fields, data source, and other underlying entities.

When you create a CrystalReportViewer and attach it to a report as described earlier in this chapter, Visual Basic makes a typed ReportDocument object and adds it to the form. If you followed the earlier instructions carefully, you should see an object named StudentTestScores1 in the component tray below the form. The CrystalReportViewer control’s ReportSource property is attached to this object.

Your code can access this object to manipulate the report. For example, the following code gets an object representing the report’s field named TestNumber1. It displays the object’s name and formula name.

  ' Manipulate the ReportDocument. Dim field_object As FieldObject field_object = DirectCast( _     StudentTestScores1.ReportDefinition.ReportObjects("TestNumber1"), _     FieldObject) Debug.WriteLine("The " & field_object.Name & _     " field has data source " & field_object.DataSource.FormulaName) 

The following text shows the result:

  The TestNumber1 field has data source {TestScores.TestNumber} 

The following code exports a report into an Excel file:

  ' Export the report. Dim output_file As String output_file = Application.StartupPath & "\scores.xls" StudentTestScores1.ExportToDisk( _     CrystalDecisions.Shared.ExportFormatType.Excel, output_file) 

The following example makes the GroupFooterSection1 section tall and gives it an aqua background. It then makes the AvgofScore1 field red and surrounded by a double line box with a drop shadow.

  ' Make GroupFooterSection1 tall and aqua. Using section1 As Section = DirectCast( _   StudentTestScores1.ReportDefinition.Sections("GroupFooterSection1"), _   Section)     section1.Height = 600     section1.SectionFormat.BackgroundColor = Color.Aqua     ' Make the average score field red and surrounded by a box.     Using avg_field_object As FieldObject = DirectCast( _       StudentTestScores1.ReportDefinition.ReportObjects("AvgofScore1"), _       FieldObject)         avg_field_object.Color = Color.Red         avg_field_object.Top = (section1.Height - avg_field_object.Height) \ 2         With avg_field_object.Border             .TopLineStyle = CrystalDecisions.Shared.LineStyle.DoubleLine             .BottomLineStyle = CrystalDecisions.Shared.LineStyle.DoubleLine             .LeftLineStyle = CrystalDecisions.Shared.LineStyle.DoubleLine             .RightLineStyle = CrystalDecisions.Shared.LineStyle.DoubleLine             .HasDropShadow = True         End With     End Using ' avg_field_object End Using ' section1 

Usually you can make formatting changes at design time but, as these examples show, you can manipulate the report’s objects at runtime if necessary.




Visual Basic 2005 with  .NET 3.0 Programmer's Reference
Visual Basic 2005 with .NET 3.0 Programmer's Reference
ISBN: 470137053
EAN: N/A
Year: 2007
Pages: 417

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net