在本节中,将用比较运算符填充 DropDownList 控件。您要创建一个包含比较运算符的枚举。
DropDownList 控件可用来选择是显示等于、小于、大于、小于或等于、大于或等于、不等于在 TextBox 控件中输入的文本的客户名。
在 redisplay_Click() 事件方法中,将修改当前赋给 CrystalReportViewer 控件的 SelectionFormula 属性的字符串。
创建 CeComparisonOperator 枚举
在“解决方案资源管理器”中,右击粗体显示的项目名,指向“添加”,然后单击“添加新项”。
在“添加新项”对话框中,从“模板”视图选择“类”。
在“名称”字段中,键入 CeComparisonOperator,然后单击“添加”。
<table>
<colgroup>
<col style="width: 100%" />
</colgroup>
<thead>
<tr class="header">
<th><img src="images/8yfdxzdx.alert_note(zh-cn,VS.90).gif" alt="Note" class="note" />注意</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><p>可能会要求将此类放置在 Code 目录中。单击“是”按钮。</p></td>
</tr>
</tbody>
</table>
在类签名中,将单词 class 改为 enum,以将该类转换为枚举。
在 C# Windows 项目中,还必须将命名空间更改为项目的名称。
注意在 Visual Basic 中,要记住把类的开始和结束签名都更改为 enum。
因为枚举没有构造函数,所以要删除代码的 C# 版本中提供的默认构造函数方法。
在枚举内部,输入值:
EqualTo LessThan GreaterThan LessThan_or_EqualTo GreaterThan_or_EqualTo Not_EqualToEqualTo, LessThan, GreaterThan, LessThan_or_EqualTo, GreaterThan_or_EqualTo, Not_EqualTo
从 CeComparisonOperator 枚举填充网站的 DropDownList 控件
以下过程解释了如何将 CeComparisonOperator 枚举绑定到网站项目或 Windows 项目的 DropDownList 控件。请按照说明完成下面的某个过程。
打开 Web 窗体。
从“视图”菜单中,单击“代码”。
在 ConfigureCrystalReports() 方法的 Not IsPostBack 条件块中,于选择公式字符串声明之前,将“DropDownList”控件的 DataSource 属性设置为 CeComparisonOperator 枚举的值。
``` vb
selectOperatorList.DataSource =
System.Enum.GetValues(GetType(CeComparisonOperator))
```
``` csharp
selectOperatorList.DataSource =
System.Enum.GetValues(typeof(CeComparisonOperator));
```
- 现在调用“selectOperatorList DropDownList”的 DataBind() 方法以将值绑定到控件。
``` vb
selectOperatorList.DataBind()
```
``` csharp
selectOperatorList.DataBind();
```
从 CeComparisonOperator 枚举填充Windows 项目的 DropDownList 控件
打开 Windows 窗体。
从“视图”菜单中,单击“代码”。
在 ConfigureCrystalReports() 方法中,于选择公式字符串声明之前,将“selectOperatorList ComboBox”的 DataSource 属性设置为 CeComparisonOperator 枚举的值。
``` vb
selectOperatorList.DataSource =
System.Enum.GetValues(GetType(CeComparisonOperator))
```
``` csharp
selectOperatorList.DataSource =
System.Enum.GetValues(typeof(CeComparisonOperator));
```
创建 GetSelectedCompareOperator() 帮助器方法
接下来,将创建 GetSelectedCompareOperator() 帮助器方法,将选定的索引以代表比较运算符的字符串形式返回。
- 在类的底部,创建名为 GetSelectedCompareOperator() 的私有帮助器方法,它返回字符串变量。
``` vb
Private Function GetSelectedCompareOperator() As String
End Function
```
``` csharp
private string GetSelectedCompareOperator()
{
}
```
- 在该方法中,创建 "Select Case" [Visual Basic] 或 "switch" [C#] 语句,该语句引用 CeComparisonOperator 枚举的成员,并将比较运算符以字符串变量形式返回。
``` vb
Select Case selectOperatorList.SelectedIndex
Case CeComparisonOperator.EqualTo
return "="
Case CeComparisonOperator.LessThan
return "<"
Case CeComparisonOperator.GreaterThan
return ">"
Case CeComparisonOperator.LessThan_or_EqualTo
return "<="
Case CeComparisonOperator.GreaterThan_or_EqualTo
return ">="
Case CeComparisonOperator.Not_EqualTo
return "<>"
Case Else
return "="
End Select
```
``` csharp
switch ((CeComparisonOperator)selectOperatorList.SelectedIndex)
{
case CeComparisonOperator.EqualTo:
return "=";
case CeComparisonOperator.LessThan:
return "<";
case CeComparisonOperator.GreaterThan:
return ">";
case CeComparisonOperator.LessThan_or_EqualTo:
return "<=";
case CeComparisonOperator.GreaterThan_or_EqualTo:
return ">=";
case CeComparisonOperator.Not_EqualTo:
return "<>";
default:
return "=";
}
```
修改赋给 SelectionFormula 属性的“客户名”比较运算符
在 redisplay_Click() 事件方法中,有一个“大于”符号(“>”)当前用于“客户名”字段选择。接下来,将学习如何把该符号更改为已从 DropDownList 控件选定的比较运算符。当调用 GetSelectedCompareOperator() 帮助器方法时,选定的符号以字符串形式返回。
- 在 redisplay_Click() 事件方法顶部,调用 GetSelectedCompareOperator() 帮助器方法,并把结果赋给字符串变量。
``` vb
Dim mySelectedOperator As String = GetSelectedCompareOperator()
```
``` csharp
string selectedOperator = GetSelectedCompareOperator();
```
对于选择公式字符串变量,将“大于”符号(“>”)替换为选定的运算符字符串。
Dim mySelectFormula As String = "{Customer.Last Year's Sales} > " & lastYearsSales.Text _ & " AND Mid({Customer.Customer Name}, 1) " & mySelectedOperator & " """ & customerName.Text & """"string selectFormula = "{Customer.Last Year's Sales} > " + lastYearsSales.Text + " AND Mid({Customer.Customer Name}, 1) " + selectedOperator + " \"" + customerName.Text + "\"";
测试 CustomersBySalesName 报表的选择公式
您已创建了一个选择公式,它依赖于为“去年销售额”字段和“客户名”字段输入的值。
现在您可生成并测试选择公式。
从“生成”菜单中,单击“生成解决方案”。
如果生成过程中出错,请立即纠正。
从“调试”菜单中,单击“开始”。
1. 在 lastYearsSales TextBox 中,键入“40000”。
2. 在 customerName TextBox 中,键入“Athens Bicycle Co”。
3. 在 DropDownList 中,选择“LessThan”。
4. 单击“重新显示报表”。
Crystal 报表显示两条客户记录:Alley Cat Cycles 和 Ankara Bicycle Company。
- 返回到 Visual Studio,然后单击“停止”从调试模式中退出。