在水晶报表中实现任意选择指定字段显示

 

本文的C#改进版本请参见:http://www.cnblogs.com/babyt/archive/2008/01/03/1024941.html

很多次被问到如下问题:
水晶报表能否做到由客户选择要显示的字段进行自定义显示?

我也迷惑了好久,后来还是仔细看了一海波的文章,终于有点眉目

如何在水晶报表中动态添加字段
http://blog.csdn.net/haibodotnet/archive/2003/11/09/21504.aspx
如何向 CrystalReportViewer 和 ReportDocument 传递参数
http://blog.csdn.net/haibodotnet/archive/2003/11/16/21516.aspx

实际操作了一下,没想象得那么困难.
开发环境: VS2003(VB.Net) + 水晶报表10高级专业开发版 + Win2003


要创建一个ADO.Net的水晶报表,具体过程我就不写了
看这里http://www.cnblogs.com/babyt/articles/118097.html

Step1: xsd文件结构
我们的表结构是这样子

有时候你并不是从数据库里取的一个完整表,那么你可以自己编辑这个xsd文件

有一点需要注意,这个文件里必须包含所有你将要显示的字段



Step2:下面我们开始创建模板

在这里,我加了6个字段,但是不要在报表上放置任何的数据字段。
新建立6个公式,名字取得别太好听,一定要按顺序来,如myField1,myField2..myField6
将公式依次放到详细资料节
删除自动产生的表头
新建立6个参数字段,同样,按顺序来,叫myParaField1,myParaField2..myParaField6

格式什么的这里就先不管了,咱们主要是先要实现功能。

Step3:程序设计
设计程序界面如下图所示,大家一下就看明白了,代码我写了详尽的注释,就不多说了。

'*************************************************************************
'
**模 块 名:CR_DefineFields
'
**说 明:Facesun.cn 版权所有2005 - 2006(C)
'
**创 建 人:Babyt(阿泰) http://www.cnblogs.com/babyt
'
**日 期:2005-04-20
'
**修 改 人:
'
**日 期:
'
**描 述:自定义字段显示报表
'
**版 本:V1.0.0
'
*************************************************************************
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Imports System.Data
Imports System.Data.OleDb
Public Class Form1
Inherits System.Windows.Forms.Form
'查看CrystalReport1的定义可以查看对应的报表类
Dim crReportDocument As CrystalReport1

''ADO.NET Variables
Dim adoOleDbConnection As OleDbConnection
Dim adoOleDbDataAdapter As OleDbDataAdapter
Dim dataSet As dataSet

#Region " Windows 窗体设计器生成的代码 "

Public Sub New()
MyBase.New()

'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()

'在 InitializeComponent() 调用之后添加任何初始化

End Sub


'窗体重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub


'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer

'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents CheckBox1 As System.Windows.Forms.CheckBox
Friend WithEvents CheckBox2 As System.Windows.Forms.CheckBox
Friend WithEvents CheckBox3 As System.Windows.Forms.CheckBox
Friend WithEvents CheckBox4 As System.Windows.Forms.CheckBox
Friend WithEvents CheckBox5 As System.Windows.Forms.CheckBox
Friend WithEvents CheckBox6 As System.Windows.Forms.CheckBox
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents CrystalReportViewer1 As CrystalDecisions.Windows.Forms.CrystalReportViewer
Friend WithEvents Label1 As System.Windows.Forms.Label
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.CheckBox1 = New System.Windows.Forms.CheckBox
Me.CheckBox2 = New System.Windows.Forms.CheckBox
Me.CheckBox3 = New System.Windows.Forms.CheckBox
Me.CheckBox4 = New System.Windows.Forms.CheckBox
Me.CheckBox5 = New System.Windows.Forms.CheckBox
Me.CheckBox6 = New System.Windows.Forms.CheckBox
Me.Button1 = New System.Windows.Forms.Button
Me.CrystalReportViewer1 = New CrystalDecisions.Windows.Forms.CrystalReportViewer
Me.Label1 = New System.Windows.Forms.Label
Me.SuspendLayout()
'
'CheckBox1
'
Me.CheckBox1.Location = New System.Drawing.Point(24, 32)
Me.CheckBox1.Name = "CheckBox1"
Me.CheckBox1.Size = New System.Drawing.Size(48, 24)
Me.CheckBox1.TabIndex = 0
Me.CheckBox1.Text = "编号"
'
'CheckBox2
'
Me.CheckBox2.Location = New System.Drawing.Point(96, 32)
Me.CheckBox2.Name = "CheckBox2"
Me.CheckBox2.Size = New System.Drawing.Size(88, 24)
Me.CheckBox2.TabIndex = 1
Me.CheckBox2.Text = "材料名称"
'
'CheckBox3
'
Me.CheckBox3.Location = New System.Drawing.Point(184, 32)
Me.CheckBox3.Name = "CheckBox3"
Me.CheckBox3.Size = New System.Drawing.Size(64, 24)
Me.CheckBox3.TabIndex = 2
Me.CheckBox3.Text = "品牌"
'
'CheckBox4
'
Me.CheckBox4.Location = New System.Drawing.Point(264, 32)
Me.CheckBox4.Name = "CheckBox4"
Me.CheckBox4.Size = New System.Drawing.Size(56, 24)
Me.CheckBox4.TabIndex = 3
Me.CheckBox4.Text = "单价"
'
'CheckBox5
'
Me.CheckBox5.Location = New System.Drawing.Point(344, 32)
Me.CheckBox5.Name = "CheckBox5"
Me.CheckBox5.Size = New System.Drawing.Size(48, 24)
Me.CheckBox5.TabIndex = 4
Me.CheckBox5.Text = "数量"
'
'CheckBox6
'
Me.CheckBox6.Location = New System.Drawing.Point(424, 32)
Me.CheckBox6.Name = "CheckBox6"
Me.CheckBox6.Size = New System.Drawing.Size(80, 24)
Me.CheckBox6.TabIndex = 5
Me.CheckBox6.Text = "采购日期"
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(536, 32)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(64, 23)
Me.Button1.TabIndex = 6
Me.Button1.Text = "生成报表"
'
'CrystalReportViewer1
'
Me.CrystalReportViewer1.ActiveViewIndex = -1
Me.CrystalReportViewer1.DisplayGroupTree = False
Me.CrystalReportViewer1.Location = New System.Drawing.Point(16, 72)
Me.CrystalReportViewer1.Name = "CrystalReportViewer1"
Me.CrystalReportViewer1.ReportSource = Nothing
Me.CrystalReportViewer1.ShowGroupTreeButton = False
Me.CrystalReportViewer1.Size = New System.Drawing.Size(592, 344)
Me.CrystalReportViewer1.TabIndex = 7
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(16, 8)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(152, 16)
Me.Label1.TabIndex = 8
Me.Label1.Text = "选择要显示的字段"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(624, 445)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.CrystalReportViewer1)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.CheckBox6)
Me.Controls.Add(Me.CheckBox5)
Me.Controls.Add(Me.CheckBox4)
Me.Controls.Add(Me.CheckBox3)
Me.Controls.Add(Me.CheckBox2)
Me.Controls.Add(Me.CheckBox1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub


#End Region


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim ConnectionString As String = ""
Dim sqlString As String = ""
'保持字段的字符串
Dim FieldString As String = ""
'保持字段名称的数组
Dim FieldArray() As String
Dim i As Integer, j As Integer

'参数字段集合
Dim ParamFields As New ParameterFields
'参数字段
Dim ParamField As ParameterField
'离散值
Dim DiscreteVal As New ParameterDiscreteValue

'---------------------------------------------------------------------
'获取选取的字段并进行处理
'此处写的比较笨拙,用控件数组更方便
If CheckBox1.Checked = True Then
FieldString
= CheckBox1.Text
End If
If CheckBox2.Checked = True Then
FieldString
= FieldString & "," & CheckBox2.Text
End If
If CheckBox3.Checked = True Then
FieldString
= FieldString & "," & CheckBox3.Text
End If
If CheckBox4.Checked = True Then
FieldString
= FieldString & "," & CheckBox4.Text
End If
If CheckBox5.Checked = True Then
FieldString
= FieldString & "," & CheckBox5.Text
End If
If CheckBox6.Checked = True Then
FieldString
= FieldString & "," & CheckBox6.Text
End If
If FieldString = "" Then
MessageBox.Show(
"请选择要显示的字段")
Exit Sub
End If
'删除第一个逗号,使字串最终显示为 "字段1,字段2,字段3"
If FieldString.Substring(0, 1) = "," Then
FieldString
= FieldString.Substring(1, FieldString.Length - 1)
End If
'分割成数组,每个元素为一个字段名称
FieldArray = FieldString.Split(",")

'---------------------------------------------------------------------
'数据获取与组织部分
'以下为数据获取段,你可以根据自己的实际情况任意更改数据获取方式
ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Application.StartupPath & "\BBT_Crystal.mdb"

''打开数据库联接
adoOleDbConnection = New OleDbConnection(ConnectionString)

''Sql语句
'sqlString = "Select * From 材料采购明细"
sqlString = "Select " & FieldString & " From 材料采购明细"

''执行语句
adoOleDbDataAdapter = New OleDbDataAdapter(sqlString, adoOleDbConnection)

''创建Dataset实例
dataSet = New DataSet
''填充dataSet
adoOleDbDataAdapter.Fill(dataSet, "材料采购明细")

'---------------------------------------------------------------------
''创建一个强类型报表实例
crReportDocument = New CrystalReport1
''crReportDocument.Database.Tables("材料采购明细").SetDataSource(dataSet)

'开始以数组为依据依次顺序设置表头和公式字段
For i = 0 To UBound(FieldArray)
'设置第一个离散值并将其传递给该参数
'传递参数做为表头
'--------------------------------------
'参数设置部分
ParamField = New ParameterField
ParamField.ParameterFieldName
= "myParaField" & (i + 1).ToString
'离散值
DiscreteVal = New ParameterDiscreteValue
DiscreteVal.Value
= FieldArray(i)
'赋值
ParamField.CurrentValues.Add(DiscreteVal)
'将该字段加如到字段集合中
ParamFields.Add(ParamField)
'不允许该字段弹出提示
ParamField.AllowCustomValues = False

'将公式绑定到具体字段
crReportDocument.DataDefinition.FormulaFields("myField" & (i + 1).ToString).Text = "{材料采购明细." & FieldArray(i) & "}"
Next

'设置剩余的未使用到的参数,防止出现提示
'具体操作同上,只是给一个空值
For j = i + 1 To 6
'设置第一个离散值并将其传递给该参数
'传递参数做为表头
ParamField = New ParameterField
ParamField.ParameterFieldName
= "myParaField" & (j).ToString
ParamFields.Add(ParamField)
DiscreteVal
= New ParameterDiscreteValue
DiscreteVal.Value
= ""
ParamField.CurrentValues.Add(DiscreteVal)
ParamFields.Add(ParamField)
ParamField.AllowCustomValues
= False
Next
'将参数字段集合绑定到浏览器
CrystalReportViewer1.ParameterFieldInfo = ParamFields

''将Dataset传递给报表
crReportDocument.SetDataSource(dataSet)

''将报表传递给浏览器
CrystalReportViewer1.ReportSource = crReportDocument
End Sub

End Class

 


运行结果

在这里下载完整代码
https://files.cnblogs.com/babyt/CR_DefineFields.rar

后记1:
你可以对该例增加字段显示的先后顺序排列功能
后记2:
2005年4月21日在wp的帮助下实现格线表现方式,见
http://www.cnblogs.com/babyt/articles/142908.html
后记3:
2008年1月3日
本文的C#改进版本请参见:http://www.cnblogs.com/babyt/archive/2008/01/03/1024941.html
-------Over------------------
希望这篇文章对你有所帮助。
阿泰 20050420

posted @ 2005-04-21 01:38  阿泰  阅读(39296)  评论(77编辑  收藏  举报