饭饭TXT > 学习管理 > 《Visual Studio 2010 高级编程(英文出书版)》作者:Nick Randolph/等【完结】 > [Visual.Studio.2010.高级编程].Professional.Visual.Studio.2010.txt

第 96 页

作者:Nick Randolph/等 当前章节:15432 字 更新时间:2026-6-18 14:51

Click the New button to add a source of data to your project (such as to create a new entity model if

it doesn’t already exist). This opens the Data Source Configuration Wizard detailed in Chapter 27.

You will assume the Entity Framework model of the AdventureWorksLT database that you created

in Chapter 29 already exists in your project, so you can skip this step and simply select the type of

entity objects that you will be passing to the report (for this example you want the Product entities)

from the Available Datasets drop-down box. Finding which item to select when dealing with

Entity Framework entities can be rather confusing initially, but the parent entity is the first part of

the item name, and the name of the actual entity you want to use in the report is in the brackets

following it. So to select the Product entity in the AdventureWorksLTEntities model you select the

AdventureWorksLTEntities (Product) item. When you select the item the list of the fields it contains

are displayed in the Fields list. This data source will now be displayed in the Report Data tool

window, and will list the fields under it that you can use in your report.

If this data source changes (such as if a new field has been added to it), right-click it and select the

Refresh item from the context menu to update it to its new structure.

reporting controls

If you take a look at the Toolbox tool window you will see that it contains

the various types of controls that you can use in your report, as shown in

Figure 30-4.

To use a control, simply drag and drop it on your report at the required

position, and then you can set its properties using the Properties tool

window. Alternatively you can select the control in the Toolbox and draw

the control on the report design surface. Another method is to right-click

anywhere on your report, select the Insert submenu, and select the control

you want to insert.

Now take a closer look at each of these controls.

Text Box

The name of the Text Box control is a little confusing because you probably immediately think of

a control that the user can enter text into (which makes little sense in a report) like the Text Box

control in Windows Forms and other platforms. This mental image is also backed up by its icon

(which shows a textbox with a caret in it), but in fact this control is only for displaying text, not

for accepting text entry. The Text Box control isn’t used just for displaying static text, but can also

contain expressions (which are evaluated when the report is being generated, such as data field

values, aggregate functions, and formulas). Expressions can be entered directly into the textbox, or

they can be created using the expression builder (discussed later in this chapter) by right-clicking the

textbox and selecting the Expression menu item.

fiGure 30-4

Designing reports . 651

When you drag a data fi eld onto the report, a textbox is created at that location containing a

placeholder. The placeholder has an expression behind it, which will get and display the value for

that fi eld. A placeholder is essentially a way of hiding expressions in textboxes in order to reduce

the report design ’ s complexity. You could think of it like a parameterless function, which has a

name (referred to as a label), and contains code (known as an expression). In the report designer the

textbox will display the label instead of the (potentially long and complex) expression.

If you drag a data fi eld onto your report and it displays < < Expr > > this means

it has had to create a complex expression to refer to that fi eld (such as getting

the fi eld ’ s value in the fi rst row in the dataset), which is hidden behind the

< < Expr > > placeholder it has created. Unless this is the behavior you are after

(such showing a value in a report header or footer), it probably should be placed

in a table, matrix, or a list in order to display the value of that fi eld for each

row in the dataset. If this is the behavior you are after, fi rst click the < < Expr > >

placeholder, then right - click, select the Placeholder Properties menu item, and

give it a meaningful name by entering one in the Label textbox.

You can quickly create an expression to display a data fi eld value by typing the

name of the fi eld surrounded by square brackets (for example, [EmailAddress]).

This text will automatically be turned into a placeholder, with an expression

behind it to display the corresponding fi eld ’ s value.

You can also drag a data fi eld into an existing textbox. This will create a placeholder with an

expression behind it to display the value of that fi eld in the dropped location in the textbox. You

may do this if, for example, you wanted to display the value of that fi eld inline with some static text,

or even combine the values of multiple fi elds in the one textbox.

To create a placeholder manually, put the textbox in edit mode (where it is displaying a cursor for

you to type), then right - click and select the Create Placeholder menu item. Creating placeholders and

expressions is discussed in detail later in this chapter.

The format of the text in the Text Box (as a whole) can be set in a number of ways. The formatting

properties for the textbox can be found in the Properties tool window, and there is also a Font tab

in the Text Box Properties window for the Text Box (right - click the textbox, and select the Text

Box Properties menu item). Another way is to use the formatting options found on the Report

Formatting toolbar. This is the easiest way, but has another side benefi t. If you select the textbox in

the designer and choose formatting options from this toolbar, it will apply those formatting options

to all its text. However, the text within a textbox doesn ’ t need to be all the same format, and

selecting text within the textbox and choosing formatting options using this toolbar will apply that

formatting to just the selected text. Of course you can use standard formatting shortcut keys too,

such as Ctrl + B for bold text, and so on.

652 .

chaPter 30 reporTing

When you are displaying the value of a

number or date data field you quite often

need to format it for display in the report.

If your textbox contains just an expression,

select the textbox, right-click, select the

Text Box Properties menu item, and select

the Number tab (shown in Figure 30-5).

Alternatively, if the textbox contains text

or other field values, you can format just

the value of the placeholder by selecting

the placeholder in the textbox, right-

clicking, selecting the Placeholder Properties

menu item, and selecting the Number tab.

Then select how you want the field to be

formatted from the options available. If a

standard format isn’t available, you can select

Custom from the Category list and enter

a format string, or you can even write an

expression to format the value by clicking the fx button.

line/rectangle

The Line and Rectangle controls are shapes that you can use to draw on your report. The Line

control is often used as a separator between various parts of a report. The Rectangle control is

generally used to encapsulate an area in a report. The Rectangle control is a container control,

meaning other controls can be placed on it, and when it is moved they will be moved along with it.

Table

The Table control is used to display the data in a tabular form, with fixed columns and a varying

number of rows (depending on the data being used to populate the report). In addition to the data,

tables can also display column headers, row group headers, and totals rows.

By default, each of the cells in a table is a Text Box control (and thus each cell has the same features

described for the Text Box control). However, a cell can contain any control from the Toolbox

(such as an Image control, Chart, Gauge, and so on) by simply

dragging the control from the Toolbox into the cell.

When you first drop a Table control onto your report you will see that

fiGure 30-6

it contains a header row and a data row, as shown in Figure 30-6.

To display data in the table, drag a field from the appropriate data source in the Report Data

tool window and drop it on a column in the table. You’ll see that it has created a placeholder

with an expression behind it to display the value of that field in the data row, and that it has also

automatically filled in the header row for that column to give it a title. This header name is the name

of the field, but assuming the field name follows Pascal case naming rules you will find spaces have

been intelligently inserted into the name before capital letters (so the ProductNumber field will

fiGure 30-5

Designing reports . 653

automatically have Product Number inserted as its

header). If this header name isn ’ t suitable, you can

change it by typing a new one in its place.

Another means of setting which fi eld should be

displayed in a column is to mouse over a cell in the data row and click the icon that appears in its

top right - hand corner. This is shown in Figure 30-7. This displays a menu from which you can select

the fi eld to display in that column.

fiGure 30-7

fiGure 30-8

If you have multiple datasets in your report and you haven ’ t specifi ed the dataset

that is the source of data for the table, clicking this icon fi rst requires you to drill

down selecting the dataset fi rst (before the fi eld). The dataset selected will then

be set as the source of the data for the table, and the next time you click the icon

it will only display the fi elds from that dataset accordingly.

The table will have three columns when you drop it onto a report,

but you can add additional columns by simply dragging another

fi eld from the Report Data tool window over the table such that

the insertion point drawn on the table is at its right edge (shown

in Figure 30 - 8 ).

You can insert a column in the table by the same means, but positioning the insertion point at the

location where the column should be inserted. Alternatively, you can add or insert a new column

by right - clicking on a gray column handle, selecting the Insert Column submenu, and selecting the

location (Left or Right) relative to the column that is selected.

To delete an unwanted column, right - click the gray column handle and select Delete Columns from

the menu.

Note that tables can only contain data from a single dataset; therefore, you can ’ t

join data from multiple data sources in the one table (such as including data from

an Orders data source and a Customers data source to show each order and the

name of the customer that placed the order in the table). Therefore you will need

to do this join in the data that you have passed to populate the report with.

You can fi nd which dataset is the source of the data for a table by selecting it and fi nding the

DataSetName property in the Properties tool window. You can change which data source it uses by

selecting an alternative one from the drop - down list.

Often you will fi nd that you need to display aggregate values at the bottom of the table, such as in

a totals row. There are two ways to implement this. If you have a numeric fi eld that you want to

sum all the values in that column, right - click the cell (not the placeholder, but the entire cell) and

select the Add Total menu item at the bottom of the menu (this menu item will only be enabled

654 .

chaPter 30 reporTing

for numeric fields). A new row will be added below the

data row to display the totals, and a SUM aggregate

expression for that field will be inserted, as shown in

Figure 30-9.

fiGure 30-9

Because the Add Total menu item is only enabled for

numeric fields, you may need to create the totals row manually (such as if you want a count of items

for example). Right-click the data row’s handle, and select Insert Row . Outside Group - Below.

Then you can write the aggregate expression in the newly inserted row as required.

If you want to change the type of aggregate function used by the total, you will need to

modify the expression. Instead of manually making the change, a quicker way to do this is

to select the placeholder (and not the cell), right-click, select the Summarize By submenu,

and select the alternative aggregate function from the submenu.

A table can filter and sort data from the data source before displaying it. Both of these can be

configured in the Tablix Properties window (right-click the gray handle area for the table and select

the Tablix Properties menu item). The Filter tab enables you to specify filters (each consisting of an

expression, an operator, and a value). The Sorting tab enables you to specify one or more fields to

sort the data by and the sort order for each.

You may also want to group rows in a table, showing a group header between each grouping. For

example, you may want to group orders by customer, and show the customer’s name in the group

header row (which therefore doesn’t need to be displayed as a column). You can have multiple levels

of grouping, enabling complex nested hierarchies to

be created. Again, there are multiple ways to set the

grouping for a table. One is to select the table and

drag a field from the Report Data tool window onto

the Row Groups pane at the bottom of the report

designer above the (Details) entry already there.

Another way (that gives you additional options for

the grouping) is to right-click the data row’s gray

handle and select Add Group . Parent Group from

the menu. This displays the Tablix Group window

shown in Figure 30-10.

Here you can select the field or an expression to group by, and there is also the option to add a

group header and/or footer row. For example, these additional options may be useful if you want to

display the value of the group field in a header above the data for a group, and totals in the footer

below it.

By default (even if you select to create a group header row or if there is a column displaying the

group field’s value) a new column will be inserted to the left of the data configured to show the value

目录
设置
设置
阅读主题
字体风格
雅黑 宋体 楷书 卡通
字体大小
适中 偏大 超大
保存设置
恢复默认
手机
手机阅读
扫码获取链接,使用浏览器打开
书架同步,随时随地,手机阅读
首 页 < 上一章 章节列表 下一章 > 尾 页