Introduction to Rave Reports

Introduction to Rave Reports - Part IV: More Data Aware Reports

In Part III we have explored the data aware capabilities of Rave Reports, using the Driver Data View. On Part IV we are going to learn how to use Direct Data Views, the creation of Master/Detail reports, and the use of CalcOP and CalcTotal components to display calculated values and totals.

The Direct Data View

The Direct Data View allows you share the database connection established by your application, fetching data directly from the DataSets in your application. You can link them to any TDataSet descendant, and, with some code, you can even fetch data from custom DataStructures, like arrays of records or TStringLists. Apart from that, it behaves in the way as the Driver Data View we explored on Part III.

Using DataSet Connections

Let's create a master detail report using Direct Data Views. Create a new application, and set up a connection with your database using your favorite Db Framework. I'll still be using BDE, for demonstration purposes. Add two tables (or queries, as you prefer), selecting data from the orders and items tables. For each DataSet, add a corresponding TRvDataSetConnection, available in the Rave tab, in the pallete. Set the property DataSet of each DataSetConnection to the corresponding component, and give them meaningful names (I called them dscItems and dscOrders).

The name is important: it is the your application link to the report. You won't be able to have two DataSet connections, even in different reports, with the same name, or you might run into trouble if someone tries to run both reports concurrently.

After the DataSets are set up, return to the Rave Reports designer. Choose New Data Connection, and you should see the DataSet Connections you added to your form in the Delphi designer:

Select dscItems to create the first DataView, then repeat the process to create the DataView for dscOrders. You should now see the fields in the Project Treeview, under the newly created DataViews:

Don't forget to rename the DataViews to better names than DataView1 and DataView2.

Creating the Bands

After the DataViews have been created, we should set up the bands. Create a Region consisting of the whole page area, and add one band (Header), two DataBands (Master and Detail), and another Band (Footer). Set the ControllerBand property of Header, Detail and Footer to Master. Using the BandStyle property editor, set the style of the Header band to Body Header (B), the style of the Detail band to Detail (D), and the Footer band to Body Footer (b). Notice how the Band Display gives a nice preview of how the bands are going to printed:

You also need to link the DataView property of the Master band to dvOrders, and the Detail band to dvItems. To create the Master/Detail relashionship, set the MasterDataView property of the Detail band to dvOrders, and both the MasterKey and DetailKey properties to the field OrderNo.

The FontMaster Component

You proably want similar items (all the items in the Detail Band, for example), to print with the same font. Instead of setting each Font property individually, you can you a FontMaster. Add a FontMaster component to the page and set its Font propert to Times New Roman, 10. In the next step, when adding the Text and DataText components, set their FontMirror property to the FontMaster. Now, if you ever change your mind about the font that is going to be used in this report, you don't need to change it in every component, just change it in the FontMaster and it will be reflect in every other component that is linked to it. You can have as much FontMasters in one report as you need.

The FontMaster is a Non-Visual component, meaning it won't show in the Design Area. If you need to reselect it, you will have to do it through the Project TreeView.

Adding the DataTexts

Now its just a matter of adding Text and DataText components to the bands. Don't forget you can drag-and-drop (while pressing the ctrl and alt keys) from the Project Treeview to create them. I added OrderNo, SaleDate, Terms, PaymentMethod, AmountPaid and Freight to the Master band, and ItemNo, PartNo, Qty and Discount to the Detail band. Don't forget that the DisplayFormat of float fields is set in the fields themselves, in the Project Treeview, and not in the DataText components.

Adding Totals and Calculated Fields

You can make calculations and aggregated values using the CalcOP and CalcTotal components, available on the Report tab. The result of these calculations can be outputted to parameters, PI Vars, or used as intermediate result for other calculations. We are going to output them to parameters, so select your report in the Project Treeview and add two parameters, called AmountPaidTotal and FreightTotal:

Now add two CalcTotal components to your the Footer band, and set their Controller property to the Master band, their DataView property to dvOrders, the DataField to AmountPaid and Freight, and the DestParam to the params you created earlier, AmountPaidTotal and FreightTotal. It's also in the CalcTotal component where you define the DisplayFormat of the value printed.

Note that the Calc components are evaluated in the order they appear in the Project Treeview. If a CalcOp result is going to printed by a DataText component, make sure it appears in the Treeview before it. You can use the buttons Move Forward and Move Behind from the Alignment Toolbar to change their order.

You can now add in the Footer Band two DataText components to print those variables. Notice that is has summed the values, and you can define through the CalcType property of the the CalcTotal components the operation you would like to perform.

The sample data included in the DbDemos does not have a value for Freight in any of the records. Feel free to edit a few records to see them added up.

Now let's suppose we need to sum both totals, and display them in the footer band. Add a new parameter called Total to the report and a CalcOp component below the CalcTotal components, in the Footer band, and set the Src1DataField to Param.AmountPaidTotal, and Scr2DataField to Param.FreightTotal. Set the DestParam property to Total, and the DisplayFormat of the value. Add a DataText to print this value.

Finishing Up

To add the report to your project, just do the same as was done in the previous reports: just link it in a RvProject and call the execute method. You might want to filter the DataSet based on user input, but that's the same way as you always done before.

Conclusions

We have seen how to create DataAware reports using Direct Data Views, how to set up master/detail relationships, and how to use Calc components. You can find a project containing the report developed here on CodeCentral. Part V will demonstrate the use of Custom Connections and some other tricks.

你可以在VCL或CLX应用程序中使用Rave Reports去实现来自数据库或非数据库数据的报表.下面的过程说明 怎样增加一个简单的报表到已存在的数据库应用程序中. 1.在Delphi中打开一个数据库应用程序. 2.从组件面板的Rave页,假如TRvDataSetConnection组件到应用程序的表单. 3.在Object Inspector中,设置DataSet的属性为一个已在你的应用程序中定义的dataset组件.使用Rave Visual Designer 4.去设计你的报表然后并创建一个report project文件(.rav file). 选择Tools|Rave Designer运行Rave Visual Designer. 选择File|New Data Object显示Data Connections对话框. 在Data Object Type列表,选择Direct Data View然后单击Next. 在Active Data Connections列表,选择RVDataSetConnection1然后单击Finish. 在Rave Visual Designer窗口左边的Project Tree中,展开Data View Dictionary结点,燃后展开刚创建的DataView1结点. 你要使用的数据字段显示在DataView1结点下. 选择Tools|Report Wizards|Simple Table显示Simple Table wizard. 选择DataView1然后单击Next. 选择你想要在这个报表中显示的两个或三个字段然后单击Next. 按照随后的向导页提示区设置要在这个报表中使用的字段的排序, 页边距,首部文本,以及字体. 在最后向导页上,单击Generate完成这个向导然后在页面设计器中显示报表. 选择File|Save as显示Save As对话框. 把Rave project存到你的Delphi应用程序的目录下并命名为MyRave.rav. 最小化Rave Visual Designer窗口返回Delphi. 5.从组件面板的Rave页,添加一个项目组件TRvProject到表单. 6.在Object Inspector,设置ProjectFile属性为刚才创建的report project文件(MyRave.rav). 7.从组件面板的Standard页,添加TButton到表单. 8.在Object Inspector,单击Events标签然后双击OnClick事件. 9.在事件处理程序中使用ExecuteReport方法去执行Rave project组件. 10.按F9运行应用程序. 11.单击在第七步添加的按钮. 12.Output Options对话框被显示出来,单击OK显示报表.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值