HOME PAGE | DOWNLOAD | TUTORIALS | XtraReports
Devexpress

Sunday, July 22, 2012

Create a Data-Aware Report

To create a data-aware report, do the following.

Create an Application and Add a Report

1.      Run Microsoft® Visual Studio® (2008 or 2010).

2.      Start a new project (CTRL+SHIFT+N), and create a new Windows Forms Application.

3.      On the Project menu, choose Add New Item... (or press CTRL+SHIFT+A) to invoke the Add New Item dialog.

In this dialog, choose the XtraReport Class v12.1 item and click Add. This will add a new blank report to your application.

Alternatively, you can choose the XtraReport Wizard v12.1 template, which invokes the Report Wizard that is intended for quick creation of classic reports.

Create Data Objects and Bind a Report to Them

4.      In this example, a report will be bound to data from the Categories datatable of the Northwind database (nwind.mdb file located in the directory, where you installed DevExpress demos).

To create a data source and assign it to a report at design time, first select a report.

5.      Open the Properties window (by pressing F4), and in the report's XtraReportBase.DataSource property editor, click the Add Project Data Source... link.

6.      There will appear a dialog that allows you to choose a data source to bind to. Select the Database option. Click Next.

7.      On the next page specify the path to the Northwind Traders database (nwind.mdb) and click Next.

8.      Also, click Next on the following page that asks you whether the created connection string should be saved to the configuration file.

9.      The next page allows you to choose which tables should be obtained from the database. Select the Categories table and click Finish.

10.  After the above steps have been performed, the following .NET components are available in the report's designer: nwindDataSet1 and categoriesTableAdapter. The report automatically assigns its XtraReportBase.DataAdapter and XtraReportBase.DataSource properties to the newly created data objects. Note that also the XtraReportBase.DataMember property is automatically assigned to Categories. In this example, it specifies the data table, which provides data the report must iterate through.

This means that the report is bound to data. The next steps show how to customize a report's layout, and to specify which data fields should be used to provide data for report controls.

Customize Report Bands

11.  Click the top-left corner of the report designer to invoke the report's smart tag. Choose the Edit and Reorder Bands... option in this list to edit the collection of report bands.

12.  In the invoked Report Editor dialog, remove the PageHeader band (this is unnecessary for this report) and add a ReportHeader band. Note that you can also use the Report Editor dialog to change any other report band settings (e.g. the height of the PageFooter band).

Add Data-Bound Controls

13.  The following steps show how to design a report's layout.

First, drop an XRLabel control from the toolbox onto the Report Header band (as you did in Lesson 1) and set its Text to Fall Catalog.

14.  Then, create two labels bound to the CategoryName and Description data fields. To do this, you can simply drag the necessary data field items from the Field List tree and drop them onto the report's Detail band. Creating an XRLabel control bound to the Description data field is shown in the image below.

15.  Then, right-click the Picture item in the Field List tree and drop it onto the Detail band. This will invoke the context menu shown in the image below. Simply choose the XRPictureBox item from the list, and an XRPictureBox control bound to the Picture data field will be created and added to the report.

To change the size mode used to display the image within the picture box control, use its XRPictureBox.Sizing property. For instance, set this property's value to ZoomImage to proportionally resize the image within the control (without clipping).

16.  To add page numbers to the bottom of each page in the report, drag the XRPageInfo item from the toolbox DX.12.1: Report Controls tab, and then drop it onto the PageFooter band.

If necessary, you can then change the format of page numbers shown in the report via the XRPageInfo.Format property. For instance, set this property's value to Page {0} of {1}.

Now, you have a data-aware report. Its structure is represented in the Report Explorer window. Note that in the Report Explorer tree, bound controls are marked with yellow database icons.

Get the Result

The report is now ready. Switch to the Preview tab and view the result.

 

No comments:

Post a Comment