修改报表的背景颜色

在本节中,将了解如何修改报表的背景颜色。

要开始操作,请添加一个 DropDownList 控件以便选择背景颜色。

添加用于更改背景颜色的控件

  1. 打开 Web 或 Windows 窗体。
  2. 从“视图”菜单中,单击“设计器”。
  3. 从“工具箱”中将一个“Label”控件拖到表的第二行第一列中。
  4. 选择 Label 控件,然后从“属性”窗口中将 Text 设置为“选择背景颜色”。
  5. 从“工具箱”中,将 DropDownList 控件(用于网站)或 ComboBox 控件(用于 Windows 项目)添加到表的第二行第二列。
  6. 选择 DropDownList/ComboBox 控件,然后从“属性”窗口中将 ID/Name 设置为“selectBackColor”。

设置控件的默认值

现在,必须向 ConfigureCrystalReports() 方法添加代码以设置背景颜色列表和报表组件的复选框的默认值。

  1. 打开 Web 或 Windows 窗体。

  2. 从“视图”菜单中,单击“代码”。

接着,在 ConfigureCrystalReports() 方法内添加代码以设置控件的默认值。

<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>如果正在构建网站,请将这些代码行放在 Not IsPostBack 条件块内。如果正在构建 Windows 项目,请将这些代码行放在 ConfigureCrystalReports() 方法的主区域中。</p></td>
</tr>
</tbody>
</table>
  1. 将 KnownColor 枚举赋给 selectBackColorDropDownList的 DataSource 属性。

    selectBackColor.DataSource = System.Enum.GetValues(GetType(KnownColor))
    
    selectBackColor.DataSource = System.Enum.GetValues(typeof(KnownColor));
    
  2. 在网站中,将数据源绑定到 selectBackColorDropDownList。

    selectBackColor.DataBind()
    
    selectBackColor.DataBind();
    

在网站中分配背景颜色选择

接下来,要向 Button 单击事件添加代码,以基于 selectBackColor DropDownList 的选择重新显示报表。

网站与 Windows 项目相比,此代码会有所不同。为网站或 Windows 项目选择下面相应的过程。

  1. 打开 Web 窗体。

  2. 从“视图”菜单中,单击“代码”。

  3. 在类签名上方,为 System.Drawing 命名空间将 "Imports" [Visual Basic] 或 "using" [C#] 声明添加到类的顶部(如果尚未声明该命名空间)。

``` vb
Imports System.Drawing
```

``` csharp
using System.Drawing;
```
  1. 在 redisplay_Click() 事件处理程序内,添加以下代码:从 selectBackColor DropDownList 中,以字符串形式检索选定项目,然后将其传递给 Color 类的 FromName() 方法。将 Color 值赋给 CrystalReportViewer 控件的 BackColor 属性。

    myCrystalReportViewer.BackColor = Color.FromName(selectBackColor.SelectedItem.Text)
    
    crystalReportViewer.BackColor = Color.FromName(selectBackColor.SelectedItem.Text);
    

在 Windows 项目中分配背景颜色选择

现在已准备就绪,可以测试“重新显示报表”按钮。跳到下面的该节。

  1. 打开 Windows 窗体。

  2. 从“视图”菜单中,单击“代码”。

  3. 在类签名上方,为 System.Drawing 命名空间将 "Imports" [Visual Basic] 或 "using" [C#] 声明添加到类的顶部(如果尚未声明该命名空间)。

    Imports System.Drawing
    
    using System.Drawing;
    
  4. 在 redisplay_Click() 事件处理程序内,添加代码以便从 selectBackColorComboBox 中检索选定项目,并将该项转换为 KnownColor 实例。

``` vb
Dim mySelectedKnownColor As KnownColor = CType(selectBackColor.SelectedItem, KnownColor)
```

``` csharp
KnownColor selectedKnownColor = (KnownColor)selectBackColor.SelectedItem;
```
  1. 创建一个条件块来检查选定的背景颜色是否不透明。

    If Not mySelectedKnownColor = KnownColor.Transparent Then
    
    End If
    
    if (selectedKnownColor != KnownColor.Transparent)
    {
    }
    
  2. 在 if 块中,将 KnownColor 实例传递给 System.Drawing.Color 类的 FromKnownName() 方法。将 Color 值赋给 CrystalReportViewer 控件的 BackColor 属性。

``` vb
myCrystalReportViewer.BackColor = System.Drawing.Color.FromKnownColor(mySelectedKnownColor)
```

``` csharp
crystalReportViewer.BackColor = System.Drawing.Color.FromKnownColor(selectedKnownColor);
```

测试重新显示 Button 控件

现在已准备就绪,可以测试“重新显示报表”按钮。

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

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

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

即会显示 DropDownList/ComboBox,以及在前一过程中添加的 ListBox 和 Button 控件。
  1. 从 selectBackColor DropDownList 中,选择“Blue”。

    Note注意

    记住要选择报表元素(特别是 Main_Page),以使其可见。

  2. 单击“重新显示报表”。

页面会重新加载,在蓝色背景上显示报表。
  1. 返回到 Visual Studio,然后单击“停止”从调试模式中退出。