Page tree
Skip to end of metadata
Go to start of metadata

Expressions are widely used throughout a report definition to retrieve, calculate, display, group, sort, filter, parameterize, and format the contents of a report. Some expressions are created for you automatically (for example, when you drag a field from the Toolbox onto a section of your report, an expression that retrieves the value of that field is displayed in the text box). However, in most cases, you create your own expressions to provide more functionality to your report.
C1Report relies on VBScript to evaluate expressions in calculated fields and to handle report events.
VBScript is a full-featured language, and you have access to all its methods and functions when writing C1Report expressions. For the intrinsic features of the VBScript language, refer to the Microsoft Developer's Network (MSDN). C1Report extends VBScript by exposing additional objects, variables, and functions.
The following topics demonstrate how you can create your own expressions to provide more functionality to your report.
按值格式化字段
!MISSING PHRASE 'Show All'! !MISSING PHRASE 'Hide All'! 按值格式化字段功能是Section.OnPrint属性中最常用到的一种。例如,在一个报表中按照产品分组并且根据内容进行排序。报表可以将实际库存量低于产品再订购水平(产品库存量低于再订购水平,表明该产品可能面临断货风险)的产品的产品名称字体改为红色粗体字符,从而避免额外增加实际可用库存量字段(实际可用库存量 = 实际库存量–产品再订购水平)。
本示例中,通过将产品名称改为红色粗体字符的方式来表明该产品的再订购水平低于产品库存量,此效果可以使用下面的脚本来实现:
Visual Basic

Visual Basic

Dim script As String = _
"If UnitsInStock < ReorderLevel Then" & vbCrLf & _
"ProductNameCtl.ForeColor = RGB(255,0,0)" & vbCrLf & _
"ProductNameCtl.Font.Bold = True" & vbCrLf & _
"Else" & vbCrLf & _
"ProductNameCtl.ForeColor = RGB(0,0,0)" & vbCrLf & _
"ProductNameCtl.Font.Bold = False" & vbCrLf & _ "End If" c1r.Sections.Detail.OnPrint = script

C#

C#

If UnitsInStock < ReorderLevel Then
ProductNameCtl.ForeColor = RGB(255,0,0)
ProductNameCtl.Font.Bold = True
Else
ProductNameCtl.ForeColor = RGB(0,0,0)
ProductNameCtl.Font.Bold = False
End If

上述代码首先创建一个包含了VBScript事件控件的字符串,并且将该字符串赋值给当前区域的Section.OnPrint属性。
使用使用C1ReportDesigner实现:实现:
当然,你也可以不编写代码而是使用C1ReportDesigner实现该功能。在Detail区域Section.OnPrint属性的VBScript脚本编辑框中,你只需要输入下面的脚本代码即可实现该功能。具体实现步骤如下所示:

  1. 在Designer的属性窗口下拉菜单中选择Detail选项。此处显示的是Section中可用的属性。
  2. 单击Section.OnPrint属性下方的空白单元格,然后单击下拉箭头,在列表中选择Script Editor选项
  3. 在VBScript编辑器中,将下面的脚本输入即可:

If UnitsInStock < ReorderLevel Then
ProductNameCtl.ForeColor = RGB(255,0,0)
ProductNameCtl.Font.Bold = True
Else
ProductNameCtl.ForeColor = RGB(0,0,0)
ProductNameCtl.Font.Bold = False
End If

  1. 单击OK按钮,关闭编辑器控件将在该Section即将显示的时候执行这段VBScript代码。此脚本根

据"ReorderLevel"数据字段的值来设置报表中"ProductName"字段的Field.Font.Bold以及Field.ForeColor属性。如果产品库存量低于再订购水平值,则将该产品名称字段的字体设置为红色粗体字符。在下图中,你可以查看该报表修改后的显示效果。

无数据时隐藏该区域
!MISSING PHRASE 'Show All'! !MISSING PHRASE 'Hide All'!
通过给Detail区域的OnFormat属性指定表达式,你可以根据该字段的数据来改变该字段的显示格式例如,你的Detail区域中有个字段带有image控件,当对应记录的图像不存在时,你可能希望隐藏这个记录。想要隐藏这样没有数据的Detail区域,请在Detail区域的OnFormat属性中添加如下脚本:
If isnull(PictureFieldName) Then
Detail.Visible = false
Else
Detail.Visible = true
End If

隐藏无数据的区域请用如下代码:

如果想隐藏无数据的区域,这个例子中指的是记录的图像数据不存在时,请使用如下脚本代码:
Visual Basic

Visual Basic

C1Report1.Sections.Detail.OnFormat = "Detail.Visible = not isnull(PictureFieldName)"

C#

C#

c1Report1.Sections.Detail.OnFormat = "Detail.Visible = not isnull(PictureFieldName)";

使用C1报表设计器实现隐藏无数据对应区域:

除了编写代码外,你还可以使用C1报表设计器(C1ReportDesigner)将下面的脚本代码直接输入到Detail区域(Detail section)的OnFormat属性的VBScript编辑器中。完整步骤如下:

  1. 从设计器的属性窗口下拉列表中选择Detail。这样会显示出该区域(section)的全部可用属性。
  2. 点击OnFormat 属性旁边的空白框,然后点击下拉箭头,从列表中选择脚本编辑器(Script Editor) 3. 在VBScript编辑器中,直接输入下方的脚本代码: 在窗口中键入下面脚本:


If isnull(PictureFieldName) Then
Detail.Visible = false
Else
Detail.Visible = true
End If 也可以使用更简洁一些的脚本: Detail.Visible = not isnull(PictureFieldName)
<span style="color: #3f529c">重置页计数器</span>
Unable to render embedded object: File (MISSING PHRASE 'Show All') not found. Unable to render embedded object: File (MISSING PHRASE 'Hide All') not found.
控件会自动创建和更新Page变量的值。这对于在页眉和页脚添加页数非常有用。
分组开始时重置页面计数器:
某些情况下,在分组开始时你可能会需要去重置页面的计数器。例如,一个报表根据"国家"(country)字段分组显示,并且它有一个带表达式的已计算的页脚字段:
=[Country] &" - Page "& [Page] <span style="color: #3f529c">使用代码<strong>:</strong></span>
通过设置页脚字段的Text属性,可以在分组(例如,一个新的国家)开始时重置页计数器。输入如下代码:
Visual Basic


Visual Basic

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="421a4f1d-e532-41f8-8dd9-a9ccd8f269da"><ac:plain-text-body><![CDATA[

C1Report1.Fields("PageFooter").Text = "[ShipCountry] & "" "" & [Page]"

]]></ac:plain-text-body></ac:structured-macro>

C#

C#

 


c1Report1.Fields("PageFooter").Text = "[ShipCountry] + "" "" + [Page]";
<span style="color: #3f529c">使用<strong>C1</strong>报表设计器<strong>:</strong></span>
通过设置页脚字段的Text属性,可以在分组(例如,一个新的国家)开始时重置页计数器。完整步骤如下:


  1. 从设计器的属性窗口下拉列表中选择页脚(PageFooter)的PageNumber字段,这样会显示出该字段的全部可用属性。
  2. 点击Text属性旁边的空白框,然后点击下拉箭头,从列表中选择脚本编辑器(Script Editor)
  3. 在VBScript编辑器中,直接输入下方的脚本代码:


=[Country] &" - Page "& [Page] <span style="color: #3f529c">打印和预览功能入门</span>
在本节,你会学习如何使用C1PrintDocument 控件的基本功能来创建简单的文档。本章节并不是全方位介绍 C1PrintDocument控件各个功能的教程,但会教你如何快速入门,并着重介绍使用该控件的一些常见做法。

  • No labels