前面我們所講的報表,不管簡單的還是復雜的,都遵循這樣一個流程:
開發人員事先做好模版——〉用戶運行程序——〉用戶選擇相應條件——〉打印或預覽帳票——〉程序把用戶選擇的數據填充到我們的模版上,然後顯示
可以說,用戶除了選擇條件,控制顯示的數據之外,能做的控制很少(除非把一些簡單的控制放在條件裡面)。
本節我就講講如何用AR的Designer控件,實現用戶自定義帳票印刷。
比如,要打印如下數據:
Class:
A
AttendNo:
1
Name:
Tony
Sex:
Male
當然我們可以先做好相應的模版,然後讓用戶選擇數據就可以打印出來了。
但是,萬一用戶又需要一張類似的帳票,但需要把Name寫在Class前面,那我們怎麼辦?
1)重新做一個。真是個好辦法,下面的文章可以不用看了。
2)在用戶的條件中加一個條件(比如一個radio button,先顯示Class還是先顯示Name),然後我們在模版裡面判斷一下這個條件,根據它來對模版進行相應的調整。
這種方法其實在我們項目也是常常用到的(比如判斷顯示subject的全稱還是簡稱,就是在條件中放個Check box的條件,模版做相應判斷)。
但在本例中卻不適用,萬一用戶又要求把Class和AttendNo放一行上怎麼辦?用戶的需求可是千變萬化的。
3)用AR提供的Designer控件,可以實現用戶自定義模版。
對於本例來說,用戶可以自己把Class,name這些控件,拖到模版上,自己設定大小位置,然後打印。
ok,下面我們就來創建一個最簡單的自定義帳票:
1,數據源如下:
Class
AttendNo
Name
Sex
A
1
Tony
male
A
2
Mary
female
A
3
Tom
male
A
4
Jack
male
A
5
Smith
male
B
1
John
male
B
2
Li
female
B
3
Zhang
female
B
4
Wang
male
C
1
Gong
male
C
2
Tian
female
2,需要打印出來的帳票也超級簡單,就是根據Class來換頁的一張普通二維表:
page1:
Class
AttendNo
Name
Sex
A
1
Tony
male
A
2
Mary
female
A
3
Tom
male
A
4
Jack
male
A
5
Smith
male
3,創建如下文件:
<!--[if !vml]-->
<!--[endif]-->
frmShowAR和前面幾個例子一樣,用來放vIEw控件顯示報表
test.mdb是數據源
ReportDesigner裡面放Designer控件,讓用戶進行自定義操作
4,讓我們來看ReportDesigner:
<!--[if !vml]-->
<!--[endif]-->
1)左側放了個Designer控件:
<!--[if !vml]-->
<!--[endif]-->
2)右上放一些button控件,包括添加空白label,添加item,預覽帳票的按鈕。
3)右下放一個PropertyGrid控件,可以用來顯示和修改Designer中所有控件的public property.
<!--[if !vml]-->
<!--[endif]-->
5,添加好幾個控件之後,你可以先運行程序看看
<!--[if !vml]-->
<!--[endif]-->
哈哈,不錯吧,在程序運行當中,你可以直接在模版上添加section了,和我們設計模版時完全一樣,這下可以讓用戶自己來設計帳票了。
6,不過先在還不能在模版上增加控件。先在我們編寫代碼,使得可以在模版上創建控件,並且用PropertyGrid控件來修改這些控件的屬性。最後通過PrevIEw按鈕預覽帳票。
1)在模版上增加一個控件的方法:
先選中特定section (比如Detail,GroupFooter)
然後就可以用代碼:
2)預覽的實現,把當前Designer的模版傳給frmShowAR,然後在frmShowAR中顯示這個模版
ReportDesigner.vb
Private Sub btnPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevIEw.Click
Dim _frm As New frmShowAR()
_frm.Report = Me.Designer1.Report
_frm.ShowDialog(Me)
End Sub
frmShowAR.vb
完整代碼如下:
Public Class ReportDesigner
#Region "UI Event"
'''''' <summary>
'''''' Handles the Click event of the btnAddTextBox control.
'''''' </summary>
'''''' <param name="sender">The source of the event.</param>
'''''' <param name="e">The <see cref="System.EventArgs" /> instance containing the event data.</param>
'''''' <remarks></remarks>
'''''' <history>
'''''' [TonyGong] 3/8/2007 2:17 PM Created
'''''' </history>
Private Sub btnAddTextBox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddTextBox.Click
If Me.listItem.SelectedIndex = -1 Then
Return
End If
Dim arControl As New DataDynamics.ActiveReports.TextBox
Select Case Me.listItem.SelectedItem.ToString()
Case "Class"
arControl.DataFIEld = "Class"
Case "AttendNo"
arControl.DataFIEld = "AttendNo"
Case "Name"
arControl.DataFIEld = "Name"
Case "Sex"
arControl.DataFIEld = "Sex"
End Select
Me.addControlToReport(arControl)
End Sub
'''''' <summary>
'''''' Handles the Load event of the ReportDesigner control.
'''''' </summary>
'''''' <param name="sender">The source of the event.</param>
'''''' <param name="e">The <see cref="System.EventArgs" /> instance containing the event data.</param>
'''''' <remarks></remarks>
'''''' <history>
'''''' [TonyGong] 3/8/2007 2:17 PM Created
'''''' </history>
Private Sub ReportDesigner_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.PropertyGrid1.SelectedObject = Me.Designer1
End Sub
'''''' <summary>
'''''' Designer1_s the selection changed.
'''''' </summary>
'''''' <remarks></remarks>
'''''' <history>
'''''' [TonyGong] 3/8/2007 2:17 PM Created
'''''' </history>
Private Sub Designer1_SelectionChanged() Handles Designer1.SelectionChanged
Me.PropertyGrid1.SelectedObject = Me.Designer1.Selection(0)
End Sub
'''''' <summary>
'''''' Handles the Click event of the btnAddLabel control.
'''''' </summary>
'''''' <param name="sender">The source of the event.</param>
'''''' <param name="e">The <see cref="System.EventArgs" /> instance containing the event data.</param>
'''''' <remarks></remarks>
'''''' <hist
'''''' [TonyGong] 3/8/2007 2:17 PM Created
'''''' </history>
Private Sub btnAddLabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddLabel.Click
Me.addControlToReport(New DataDynamics.ActiveReports.Label)
End Sub
'''''' <summary>
'''''' Handles the Click event of the btnPrevIEw control.
'''''' </summary>
'''''' <param name="sender">The source of the event.</param>
'''''' <param name="e">The <see cref="System.EventArgs" /> instance containing the event data.</param>
'''''' <remarks></remarks>
'''''' <history>
'''''' [TonyGong] 3/8/2007 2:17 PM Created
'''''' </history>
Private Sub btnPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevIEw.Click
Dim rpt As New DataDynamics.ActiveReports.ActiveReport()
Dim _stream As New System.IO.MemoryStream()
Me.Designer1.Report.SaveLayout(_stream)
_stream.Position = 0
rpt.LoadLayout(_stream)
_stream.Close()
Dim _frm As New frmShowAR()
_frm.Report = rpt
_frm.ShowDialog(Me)
End Sub
#End Region
#Region "Private Method"
'''''' <summary>
'''''' Adds the control to report.
'''''' </summary>
'''''' <param name="control">The control.</param>
'''''' <remarks></remarks>
'''''' <history>
'''''' [TonyGong] 3/8/2007 2:17 PM Created
'''''' </history>
Private Sub addControlToReport(ByVal control As DataDynamics.ActiveReports.ARControl)
Select Case Me.Designer1.Selection(0).GetType.FullName
Case GetType(DataDynamics.ActiveReports.Detail).FullName
CType(Me.Designer1.Selection(0), DataDynamics.ActiveReports.Detail).Controls.Add(control)
Case GetType(DataDynamics.ActiveReports.PageHeader).FullName
CType(Me.Designer1.Selection(0), DataDynamics.ActiveReports.PageHeader).Controls.Add(control)
Case GetType(DataDynamics.ActiveReports.PageFooter).FullName
CType(Me.Designer1.Selection(0), DataDynamics.ActiveReports.PageFooter).Controls.Add(control)
Case GetType(DataDynamics.ActiveReports.GroupHeader).FullName
CType(Me.Designer1.Selection(0), DataDynamics.ActiveReports.GroupHeader).Controls.Add(control)
Case GetType(DataDynamics.ActiveReports.GroupFooter).FullName
CType(Me.Designer1.Selection(0), DataDynamics.ActiveReports.GroupFooter).Controls.Add(control)
Case Else
Return
End Select
End Sub
#End Regio
End Class
7,在frmShowAR中給模版設置數據源
Private Sub frmShowAR_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
''Show the table1
Dim conn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=test.mdb;Persist Security Info=False"
Dim cmd As String = "Select * from table1"
Dim adapter As New OleDbDataAdapter(cmd, conn)
Dim ds As New DataSet
adapter.Fill(ds)
_rpt.DataSource = ds
_rpt.DataMember = ds.Tables(0).TableName
_rpt.Run()
Me.VIEwer1.Document = _rpt.Document
End Sub
8,ok!我們的工作結束了,接下去用戶可以在程序中自己設計模版了
1)用戶運行程序,出來如下界面:
<!--[if !vml]-->
<!--[endif]-->
2)用戶自己添加group,添加控件,調整大小,增加控件的框線等等,如下:
<!--[if !vml]-->
<!--[endif]-->
3)不要忘記給GroupHeader1設置換頁字段
<!--[if !vml]-->
<!--[endif]-->
4)ok,按PrevIEw吧,看看我們的成果:
<!--[if !vml]-->
<!--[endif]-->
9,呵呵,現在用戶想怎麼打就怎麼打了。
也許可能有人要問了,每次運行程序,用戶都要去設置模版,都麻煩啊。
恩,我們可以增加一個save和load模版的功能,讓用戶自己把設計好的模版保存下來,今後直接load。
這個超級簡單,只要增加2個控件,編碼如下就可以了
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Me.Designer1.ExecuteAction(DataDynamics.ActiveReports.Design.DesignerAction.FileSave)
End Sub
Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click
Me.Designer1.ExecuteAction(DataDynamics.ActiveReports.Design.DesignerAction.F
End Sub
10,好了,本節就到這。
AR的自定義功能很強大的,運用的好,很多帳票都可以用自定義實現。
當然,文中舉的例子比較簡單,僅當拋磚引玉。
Public Property Report() As DataDynamics.ActiveReports.ActiveReport
Get
Return _rpt
End Get
Set(ByVal Value As DataDynamics.ActiveReports.ActiveReport)
_rpt = Value
End Set
End Property
Private Sub frmShowAR_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
_rpt.Run()
Me.VIEwer1.Document = _rpt.Document
End Sub
CType(Me.Designer1.Selection(0), DataDynamics.ActiveReports.PageHeader).Controls.Add(control)