向网站的报表添加边框

在本节中,您将学习如何向网站的 Crystal 报表添加自定义的边框。对于 Windows 项目,边框宽度、样式和颜色的属性都不可用。

在 BorderStyle 枚举中列出了边框样式。在 KnownColor 枚举中列出了颜色。但 CrystalReportViewer 控件的 BorderColor 属性是从 Color 类获取值。因此,必须将 KnownColor 值转换为 Color 值。

首先,要向 Web 窗体添加必要的控件。为了绘制边框,需要一个 TextBox 控件、两个 DropDownList 控件和一个 Button 控件。

添加用于设置边框宽度、样式和颜色的控件

  1. 打开 Web 窗体。
  2. 从“视图”菜单中,单击“设计器”。
  3. 从“工具箱”中将一个“Label”控件拖到表的第六行第一列中。
  4. 选择该“Label”控件,然后从“属性”窗口中将“Text”设置为“边框宽度”。
  5. 从“工具箱”中将一个“TextBox”控件拖到该“Label”控件所在的同一个表单元格中。
  6. 选择“TextBox”控件,然后在“属性”窗口中,将“ID”(或“Name”)设置为“borderWidth”。
  7. 从“工具箱”中将第二个“Label”控件拖到表的第六行第二列中。
  8. 选择该“Label”控件,然后从“属性”窗口中将“Text”设置为“边框样式”。
  9. 从“工具箱”中将一个“DropDownList”控件拖到该“Label”控件所在的同一个表单元格中。
  10. 选择“DropDownList”控件,然后在“属性”窗口中将“ID”(或“Name”)设置为“selectBorderStyle”。
  11. 从“工具箱”中将第三个“Label”控件拖到表的第六行第三列中。
  12. 选择该“Label”控件,然后从“属性”窗口中将“Text”设置为“边框颜色”。
  13. 从“工具箱”中将一个“DropDownList”控件拖到该“Label”控件所在的同一个表单元格中。
  14. 选择“DropDownList”控件,然后在“属性”窗口中将“ID”(或“Name”)设置为“selectBorderColor”。
  15. 从“工具箱”中将一个“Button”控件拖到表的第六行第四列中。
  16. 选择“Button”控件,然后从“属性”窗口执行以下操作:
  17. 将“ID”(或“Name”)设置为“drawBorder”。
  18. 将“文本”设置为“绘制边框”。

填充 DropDownList 控件

现在必须以可用于 CrystalReportViewer 控件的边框样式或边框颜色填充 DropDownList 控件。应在 ConfigureCrystalReports() 方法中填充 DropDownList 控件。

对于网站,边框样式存储在 System.Web.UI.WebControls.BorderStyle 枚举中。边框颜色是从 System.Drawing.KnownColor 枚举中获得的。

  1. 如果还没有声明 System.Web.UI.WebControls 和 System.Drawing 命名空间,则在类签名之上,向类顶部为这些命名空间添加 "Imports" [Visual Basic] 或 "using" [C#] 声明。
``` vb
Imports System.Web.UI.WebControls
Imports System.Drawing
```

``` csharp
using System.Web.UI.WebControls;
using System.Drawing;
```
  1. 在 ConfigureCrystalReports() 方法中,于 Not IsPostBack 条件块内,将 BorderStyle 枚举赋给“selectBorderStyleDropDownList”的 DataSource 属性。

    selectBorderStyle.DataSource = System.Enum.GetValues(GetType(BorderStyle))
    
    selectBorderStyle.DataSource = System.Enum.GetValues(typeof(BorderStyle));
    
  2. 将数据源绑定到“selectBorderStyleDropDownList”。

    selectBorderStyle.DataBind()
    
    selectBorderStyle.DataBind();
    
  3. 还是在 Not IsPostBack 条件块中,将 KnownColor 枚举赋给“selectBorderColor DropDownList”的 DataSource 属性。

``` vb
selectBorderColor.DataSource =
System.Enum.GetValues(GetType(KnownColor))
```

``` csharp
selectBorderColor.DataSource =
System.Enum.GetValues(typeof(KnownColor));
```
  1. 将数据源绑定到“selectBorderColorDropDownList”。

    selectBorderColor.DataBind()
    
    selectBorderColor.DataBind();
    

为“绘制边框”按钮控件编写代码

接下来,为 CrystalReportViewer 控件的 BorderWidth、BorderStyle 和 BorderColor 属性赋值。

  1. 打开 Web 窗体。

  2. 从“视图”菜单中,单击“设计器”。

  3. 双击“绘制边框”按钮控件。

即会出现报表的代码隐藏类,显示已经自动生成 drawBorder\_Click() 事件处理程序。
  1. 在 drawBorder_Click() 事件处理程序内,将在 borderWidthTextBox 中键入的文本赋给“CrystalReportViewer”控件的 BorderWidth 属性。
<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>您尚未确认是否在 TextBox 控件中输入了整数。对于生产性应用程序,可以添加针对 TextBox 控件配置的确认控件。</p></td>
</tr>
</tbody>
</table>

``` vb
myCrystalReportViewer.BorderWidth =
Unit.Parse(borderWidth.Text.ToString())
```

``` csharp
crystalReportViewer.BorderWidth =
Convert.ToInt32(borderWidth.Text);
```
  1. 从 selectBorderStyleDropDownList 中获取选定的索引,然后将其转换为 BorderStyle 值。将 BorderStyle 值赋给 CrystalReportViewer 控件的 BorderStyle 属性。

    myCrystalReportViewer.BorderStyle = CType(selectBorderStyle.SelectedIndex, BorderStyle)
    
    crystalReportViewer.BorderStyle = (BorderStyle)selectBorderStyle.SelectedIndex;
    
  2. 从“selectBorderColor DropDownList”中获取字符串形式的选定项,并把它传递到 Color 类的 FromName() 方法。将 Color 值赋给“CrystalReportViewer”控件的 BorderColor 属性。

``` vb
myCrystalReportViewer.BorderColor = Color.FromName(selectBorderColor.SelectedItem.Text)
```

``` csharp
crystalReportViewer.BorderColor = Color.FromName(selectBorderColor.SelectedItem.Text);
```

在 Crystal 报表周围绘制边框

  1. 从“生成”菜单中,单击“生成解决方案”。

  2. 如果生成过程中出错,请立即纠正。

  3. 从“调试”菜单中,单击“开始”。

  4. 对于边框宽度,键入“10”。

  5. 对于边框样式,选择“Double”。

  6. 对于边框颜色,选择“SteelBlue”。

  7. 单击“绘制边框”按钮。

    页面会重新加载,并在 Crystal 报表周围显示边框。

  8. 返回到 Visual Studio,然后单击“停止”从调试模式中退出。