现在的位置: 首页 > 综合 > 正文

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

2012年02月03日 ⁄ 综合 ⁄ 共 5533字 ⁄ 字号 评论关闭
30元程序员衣装优惠券,仅剩3天!点击领取
 

本文的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 = ""
'保持字段名称的数组
30元程>序员衣装优惠券,仅剩3天!点击领取

抱歉!评论已关闭.