Crystal Reports For Visual Studio 2005 phần 7 ppt

57 383 0
Crystal Reports For Visual Studio 2005 phần 7 ppt

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Walkthroughs Copyright © 2004 Business Objects Page 355 myCrystalReportViewer.ReportSource = customersViaIdrReport [end] [C#] crystalReportViewer.ReportSource = customersViaIdrReport; [end] You are now ready to build and run your project. Skip to the next section below. To instantiate the CustomersViaIDR report as a non-embedded report and bind it to the CrystalReportViewer control 1. Open the Web or Windows Form. 2. From the View menu, click Code. 3. Add a new class-level declaration for the ReportDocument report wrapper class, using the variable name customersViaIdrReport. Set its access modifier to private. [Visual Basic] Private customersViaIdrReport As ReportDocument [end] [C#] private ReportDocument customersViaIdrReport; [end] Note The ReportDocument class is a member of the CrystalDecisions.CrystalReports.Engine namespace. You have added an "Imports" [Visual Basic] or "using" [C#] declaration for this namespace in Appendix: Project Setup. When you instantiate ReportDocument and load a report into the namespace, you gain access to the report through the SDK, without embedding the report. 4. Within the ConfigureCrystalReports() method (that you have created in Appendix: Project Setup), instantiate the ReportDocument class. [Visual Basic] customersViaIdrReport = New ReportDocument() [end] [C#] customersViaIdrReport = new ReportDocument(); [end] 5. Declare a string variable, name it reportPath, and assign to it a runtime path to the local report. This path is determined differently for Web Sites and Windows projects: For a Web Site, pass the name of the local report file as a string parameter into the Server.MapPath() method. This maps the local report to the hard drive file directory path at runtime. [Visual Basic] Dim reportPath As String = Server.MapPath("CustomersViaIDR.rpt") [end] [C#] string reportPath = Server.MapPath("CustomersViaIDR.rpt"); Walkthroughs Copyright © 2004 Business Objects Page 356 [end] For a Windows project, concatenate the Application.StartupPath property with a backslash and the local report file name. This maps the report to the same directory as the Windows executable file. Note At compile time you will copy the report to the directory containing the executable file. [Visual Basic] Dim reportPath As String = Application.StartupPath & "\" & "CustomersViaIDR.rpt" [end] [C#] string reportPath = Application.StartupPath + "\\" +"CustomersViaIDR.rpt"; [end] 6. Call the Load() method of the ReportDocument instance and pass into it the reportPath string variable. [Visual Basic] customersViaIdrReport.Load(reportPath) [end] [C#] customersViaIdrReport.Load(reportPath); [end] 7. On the next line, beneath the report loading, bind the ReportSource property of the CrystalReportViewer to the ReportDocument instance. [Visual Basic] myCrystalReportViewer.ReportSource = customersViaIdrReport [end] [C#] crystalReportViewer.ReportSource = customersViaIdrReport; [end] Whether you have chosen to instantiate an embedded report class or a non-embedded report class (ReportDocument), the variable name used is the same: customersViaIdrReport. This allows you to use a common set of code in the procedures that follow. You are now ready to build and run your project. To test the loading of the CustomersViaIDR report 1. From the Build menu, select Build Solution. 2. If you have any build errors, go ahead and fix them now. 3. If you use a non-embedded report in a Windows project, locate the compiled Windows executable in the \bin\ [Visual Basic] or \bin\debug\ [C#] subdirectory, and then copy the report to that subdirectory. Walkthroughs Copyright © 2004 Business Objects Page 357 Note To have the non-embedded report loaded by the Windows executable at runtime, the report must be stored in the same directory as the Windows executable. 4. From the Debug menu, click Start. Note If you are developing a Web Site in Visual Studio 2005, and this is the first time you have started debugging, a dialog box appears and states that the Web.config file must be modified. Click the OK button to enable debugging. The report is displayed, showing data from the IDataReader static method. 5. Return to Visual Studio and click Stop to exit from debug mode. Conclusion In this data connectivity tutorial, you learned how to connect to IDataReader. You created a class library and populated it with a static method that returns IDataReader from an OleDb database connection. You compiled the class library to generate a DLL assembly file. You then created a new report with the embedded Crystal Report Designer and connected to the IDataReader static method within the DLL. Sample Code Information Each tutorial comes with Visual Basic and C# sample code that show the completed version of the project. Follow the instructions in this tutorial to create a new project or open the sample code project to work from a completed version. The sample code is stored in folders that are categorized by language and project type. The folder names for each sample code version are as follows: C# Web Site: CS_Web_Data_IDataReader C# Windows project: CS_Win_Data_IDataReader Visual Basic Web Site: VB_Web_Data_IDataReader Visual Basic Windows project: VB_Win_Data_IDataReader To locate the folders that contain these samples, see Appendix: Tutorials' Sample Code Directory. Walkthroughs Copyright © 2004 Business Objects Page 358 Crystal Reports For Visual Studio 2005 Data Connectivity Tutorial: Connecting to Object Collections Walkthroughs Copyright © 2004 Business Objects Page 359 Connecting to Object Collections Introduction In this tutorial on data connectivity, you learn how to use an object collection as the data source for a Crystal report. In this tutorial, you create a class that is the type for each object in the object collection. The class will represent stock market information. When you build your Crystal report, this Stock class is accessed from the report wizard much like a database table, but rather than add table columns as fields to display, you add class properties instead. When the report is first displayed, the report is empty. The report design is complete, but no data is available to populate the report. Then you create a method that instantiates an ArrayList and adds multiple Stock instances to the ArrayList instance. Each Stock instance has its properties set to unique values. The ArrayList instance is then returned from the method. You will add this information programmatically at design time, and again dynamically at run time. The returned ArrayList, an object collection, is assigned to the SetDataSource property of the Crystal report. When the report is displayed, each object in the object collection provides one Detail row in the report. To begin, you create the Stock class. Creating the Stock Class In this section, you create the Stock class. The Stock class contains private fields that are exposed as public properties: Symbol, Volume, and Price. To create the Stock class Note This procedure works only with a project that has been created from Appendix: Project Setup. Project Setup contains specific namespace references and code configuration that is required for this procedure, and you will be unable to complete the procedure without that configuration. Therefore, before you begin this procedure, you must first follow the steps in Appendix: Project Setup. 1. In Solution Explorer, right-click the web site name that is in bold type and then click Add New Item. 2. In the Add New Item dialog box: In the Visual Studio Installed Templates field, select Class In the Name field, type "Stock", and then click Add. In the dialog box that appears, click Yes. Note In Visual Studio 2005, all classes must be placed inside of an App_Code folder if they are to be generally consumable. When you click the Add button, an alert box will ask you if you would like to place your class inside of this App_Code folder. The Stock class must be set to public scope to be accessed when you create the report. Verify that the class you have created is public. If not, add the public modifier to the class signature to expose the class to the embedded Crystal Report Designer. [Visual Basic] Walkthroughs Copyright © 2004 Business Objects Page 360 Public Class Stock End Class [end] [C#] public class Stock { public Stock() { } } [end] 3. If coding in Visual Basic, add a default constructor. [Visual Basic] Sub New() End Sub [end] 4. Within the class, add three private fields. [Visual Basic] Private _symbol As String Private _volume As Integer Private _price As Double [end] [C#] private string _symbol; private double _price; private int _volume; [end] Next, you will add three public read/write properties to encapsulate the three private fields. 5. Create a new property named Symbol. [Visual Basic] Public Property Symbol() As String Get Return _symbol End Get Set(ByVal value As String) _symbol = value End Set Walkthroughs Copyright © 2004 Business Objects Page 361 End Property [end] [C#] public string Symbol { get { return _symbol; } set { _symbol = value; } } [end] 6. Create a new property named Price. [Visual Basic] Public Property Price() As Double Get Return _price End Get Set(ByVal value As Double) _price = value End Set End Property [end] [C#] public double Price { get { return _price; } set { _price = value; Walkthroughs Copyright © 2004 Business Objects Page 362 } } [end] 7. Create a new property named Volume. [Visual Basic] Public Property Volume() As Integer Get Return _volume End Get Set(ByVal value As Integer) _volume = value End Set End Property [end] [C#] public int Volume { get { return _volume; } set { _volume = value; } } [end] 8. Finally, create a new constructor that takes the three public properties as arguments. [Visual Basic] Sub New(ByVal symbol As String, ByVal volume As Integer, ByVal price As Double) _symbol = symbol _volume = volume _price = price End Sub [end] [C#] Walkthroughs Copyright © 2004 Business Objects Page 363 public Stock (String symbol, int volume, double price) { _symbol = symbol; _volume = volume; _price = price; } [end] 9. From the Build menu, click Build Website. If you have any build errors, fix them now. You are now ready to access this object from the embedded Crystal Report Designer. Connecting a Report to the Stock Object In this section, you create a new Crystal report in the embedded Crystal Report Designer and connect the report to the Stock object. To connect a Crystal report to the Stock object 1. Right-click the project name and click Add New Item. 2. In the Add New Item dialog box, select Crystal Report. 3. In the Name field, enter "StockObjects.rpt", and then click Add. 4. In the Crystal Reports Gallery dialog box, click OK. 5. In the Standard Report Creation Wizard dialog box, expand Project Data and the sub node .NET Objects. A list of classes within the project appears. Each class is prefixed with the project namespace. 6. Expand the Stock class to view a selectable sub node. 7. Click the right arrow to move the Stock class sub node to the Selected Tables panel, and then click Next. 8. Expand Stock and click the >> to move all columns to the Fields to Display panel, and then click Next. 9. Select Symbol and click the right-arrow to move into the Group By panel, and then click Finish. Binding the Report In Appendix: Project Setup, you placed a CrystalReportViewer control on the Web or Windows Form. In the previous procedure, you added a StockObjects report to the project. In this section, you will bind the Stock Objects report to the Crystal Report Viewer, set the data source of the report to an Object Collection, and populate the Object Collection programmatically. Walkthroughs Copyright © 2004 Business Objects Page 364 To instantiate the StockObjects report as a non-embedded report and bind it to the CrystalReportViewer control 1. Open the default Code-Behind class, Default.aspx.cs or Default.aspx.vb . 2. Above the class signature, add an "Imports" [Visual Basic] or "using" [C#] declaration to the top of the class for the System.Collections namespace. [Visual Basic] Imports System.Collections [end] [C#] using System.Collections; [end] Note This reference gives you access to the ArrayList class. ArrayList implements ICollection . This qualifies ArrayList as one of several class types that can be used to build an object collection that is recognized by Crystal Reports. 3. Add a new class-level ArrayList , call it stockValues . [Visual Basic] Private stockValues As ArrayList [end] [C#] private ArrayList stockValues; [end] 4. Add a new class-level declaration for the ReportDocument report wrapper class, with the variable name stockObjectsReport . Set its access modifier to private. [Visual Basic] Private stockObjectsReport As ReportDocument [end] [C#] private ReportDocument StockObjectsReport; [end] 5. Within the ConfigureCrystalReports() method you created in Appendix: Project Setup, declare a string variable, name it reportPath , and assign to it a runtime path to the local report. Pass the name of the local report file as a string parameter into the Server.MapPath() method. This maps the local report to the file path at runtime. [Visual Basic] Dim reportPath As String = Server.MapPath("StockObjects.rpt") [end] [C#] string reportPath = Server.MapPath("StockObjects.rpt"); [end] 6. Add two line breaks, and instantiate the ReportDocument class. [Visual Basic] [...]... CS_Web_Data_ObjectCollection Visual Basic Web Site: VB_Web_Data_ObjectCollection To locate the folders that contain these samples, see Appendix: Tutorials' Sample Code Directory Copyright © 2004 Business Objects Page 373 Walkthroughs Crystal Reports For Visual Studio 2005 Other Tutorials Copyright © 2004 Business Objects Page 374 Walkthroughs Crystal Reports For Visual Studio 2005 Other Tutorial: Configuring... strings For more information, see the previous step in this tutorial, Working with Default and Custom Language Resource Files 3 Close Viewer.txt 4 From the Start menu, go to Programs >Visual Studio 2005 >Visual Studio Tools >Visual Studio Command Prompt 5 Change directory to the resource files directory: cd c:\CrystalReportViewer_resource_files\ 6 Change directory again to the ro subdirectory cd ro 7 Run... currently held in Session [Visual Basic] Session("stockValues") = stockValues [end] [C#] Session["stockValues"] = stockValues; [end] Copyright © 2004 Business Objects Page 370 Walkthroughs 8 Finally, call the ConfigureCrystalReports() method This will re-bind the report to the updated stockValues Object Collection [Visual Basic] ConfigureCrystalReports() [end] [C#] ConfigureCrystalReports(); [end] 9 From... Objects Page 375 Walkthroughs Configuring Multilingual Client Support Introduction In this tutorial, you learn how to configure multilingual client support in a Web Site or a Windows project Crystal Reports for Visual Studio 2005 includes support for multilingual Web and Windows clients, through dynamic localization Dynamic localization allows users to view ToolTips and other content of the CrystalReportViewer... Based on a Formula Field In this section, you will create a chart that reports off of aggregate information You will first create a formula to calculate the worth of a particular holding, and then create a pie chart that displays the proportional worth of all of your holdings 1 From the Crystal Reports menu, select Report, and click Formula Workshop 2 In the Formula Workshop dialog, select Formula Fields... /compile CRWebFormViewer.txt,CrystalDecisions.Web.resources resgen /compile SCRShared.txt,CrystalDecisions.Shared.resources Copyright © 2004 Business Objects Page 378 Walkthroughs resgen /compile Viewer.txt,CrystalDecisions.Windows.Forms.resources Note Do not put a space either before or after the comma 8 Run the al.exe utility to create a DLL for each resources file al.exe /t:lib /embed:CrystalDecisions.Web.resources... Copyright © 2004 Business Objects... name="businessObjects"> Copyright © 2004 Business Objects Page 382 Walkthroughs ... report to the stockValues ArrayList [Visual Basic] stockObjectsReport.SetDataSource(stockValues) [end] [C#] stockObjectsReport.SetDataSource(stockValues); [end] 9 Finally, bind the ReportSource property of the CrystalReportViewer to the ReportDocument instance [Visual Basic] myCrystalReportViewer.ReportSource = stockObjectsReport [end] [C#] crystalReportViewer.ReportSource = stockObjectsReport; [end]... Traditional Chinese (zh-cht) Note In Crystal Reports for Visual Studio 2005, the default resource files are not shipped with the product install, but can be downloaded from the Business Objects Web Site For the download location, see Appendix: Useful Addresses at a Glance In addition to the default languages, you can create your own customized language resource files for any other language In this tutorial . Directory. Walkthroughs Copyright © 2004 Business Objects Page 374 Crystal Reports For Visual Studio 2005 Other Tutorials . Directory. Walkthroughs Copyright © 2004 Business Objects Page 358 Crystal Reports For Visual Studio 2005 Data Connectivity Tutorial: Connecting to Object Collections Walkthroughs. Page 371 8. Finally, call the ConfigureCrystalReports() method. This will re-bind the report to the updated stockValues Object Collection. [Visual Basic] ConfigureCrystalReports()

Ngày đăng: 08/08/2014, 18:22

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan