[VB.NET] การพิมพ์ใบกำกับภาษี (Invoice) ด้วย ActiveReports แบบ Unbound DataSet
http://www.g2gnet.com/webboard/images/vbnet/arnet/ardatasetunbound.pngโค้ดชุดนี้เป็น Demo ActiveReports .Net หรือตัวอย่างจากทางค่าย GrapeCity (DataDynamics) ซึ่งแอดมินนำมาปรับแต่งโมดิฟายใหม่ เพื่อให้สะดวกต่อการรันโปรแกรม โดยที่ชุดตัวอย่างจะมี Data Source เป็นแบบข้อมูล Unbound Data อยู่ 4 แบบ ประกอบด้วย DataSet, DataReader, Text File และ Data Array สำหรับในบทความชุดนี้ แอดมินเลือกมาเพียง DataSet เพื่อเป็นตัวอย่างให้ศึกษากันครับ ...
ตัวอย่างคุณสมบัติในส่วนของ PageHeader ... การกำหนด DataField จะต้องพิมพ์เองโดยเลือกมาจากฟิลด์ข้อมูลของตารางข้อมูล
http://www.g2gnet.com/webboard/images/vbnet/arnet/ardatasetpageheader.png
ตัวอย่างคุณสมบัติในส่วนของ GroupHeader ... ให้สังเกตว่ามีการจัดกลุ่มด้วยฟิลด์ OrderID
http://www.g2gnet.com/webboard/images/vbnet/arnet/ardatasetgroupheader.png
ตัวอย่างคุณสมบัติในส่วนของ Detail ... DataField แต่ละตัวจะต้องพิมพ์เอง ตามชื่อฟิลด์จากตารางข้อมูล
http://www.g2gnet.com/webboard/images/vbnet/arnet/ardatasetdetail.png
ตัวอย่างคุณสมบัติในส่วนของ GroupFooter ... จะมีการทำคำสั่งในส่วนของ Summary เพื่อสรุปยอดเงิน
http://www.g2gnet.com/webboard/images/vbnet/arnet/ardatasetgroupfooter.png
ส่วนของ Script ซึ่งในตัวอย่างจะเป็น C# แต่ แอดมินเปลี่ยนเป็น VB.NET แทน และเพิ่มโค้ดในส่วนของ GroupFooter1_BeforePrint ซึ่งแอดมินชอบแบบเขียนคำสั่งลงใน Designer มากกว่า
http://www.g2gnet.com/webboard/images/vbnet/arnet/ardatasetscript.png
มาดูโค้ดฉบับเต็มสำหรับฟอร์มหลัก ...
Public Class frmAR6DataSet
Private Sub frmAR6DataSet_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'Create the report
Dim rpt As New ARUnboundDS()
'Run and view the report
rpt.Run(False)
Me.Viewer1.Document = rpt.Document
End Sub
End Class
โค้ดในส่วนของ ActiveReports ...
Imports System
Imports System.Data
Imports System.Data.OleDb
Imports System.Globalization
Imports DataDynamics.ActiveReports
Imports DataDynamics.ActiveReports.Document
Public Class ARUnboundDS
'DataSource for the report
Private _InvoiceData As DataSet
Private _RowCounter As Integer
' DataInitialize Event
' ActiveReports event that is called during the report initalization
' procedure. (after .Run is called on your report object)Normally used
' with unbound reporting to establish an active connection to your data
' to be used with the FetchData event, or to setup a bound report with a
' dynamic database connection at runtime.
Private Sub ARUnboundDS_DataInitialize(sender As Object, e As System.EventArgs) Handles Me.DataInitialize
'Dataset to hold data
_InvoiceData = New DataSet()
_InvoiceData.Locale = CultureInfo.InvariantCulture
'Database connection populated from the sample Northwind access database
'MsgBox(GetDBPath.GetNwindConnectString)
Dim _nwindConn As New OleDbConnection(GetDBPath.GetNwindConnectString)
'SQL Select command to run against the database
Dim _selectCMD As New OleDbCommand("SELECT * FROM Invoices ORDER BY Customers.CompanyName, OrderID", _nwindConn)
_selectCMD.CommandTimeout = 30
'Data adapter used to run the select command
Dim _InvoicesDA As New OleDbDataAdapter()
_InvoicesDA.SelectCommand = _selectCMD
'Fill the DataSet
_InvoicesDA.Fill(_InvoiceData, "Invoices")
'Initialize counter to first row of the DataSet
_RowCounter = 0
'Add all the columns from the DataSet as Fields for the report
Dim i As Integer
For i = 0 To (_InvoiceData.Tables(0).Columns.Count) - 1
Me.Fields.Add(_InvoiceData.Tables(0).Columns(i).ColumnName)
Next i
'Add Calculated Fields
Me.Fields.Add("DiscountTotal")
End Sub
' FetchData Event
' ActiveReports event that is called during the report run once per
' row from the dataset.This event is usually only used in unbound reporting,
' you would set the Fields collection value to the value from your dataset and
' advance the next row.When you run out of data, you should set the
' FetchEventArgs argument's EOF field to true to tell the report that the report
' is done with the data.
Private Sub ARUnboundDS_FetchData(sender As Object, eArgs As DataDynamics.ActiveReports.ActiveReport.FetchEventArgs) Handles Me.FetchData
If _RowCounter = _InvoiceData.Tables(0).Rows.Count Then
'If the row counter has reached the end of the data then
'set the eArgs.EOF flag to true and exit the procedure
eArgs.EOF = True
Return
Else
'Populate the fields collection from the DataSet
Dim i As Integer
For i = 0 To (_InvoiceData.Tables(0).Columns.Count) - 1
Me.Fields(_InvoiceData.Tables(0).Columns(i).ColumnName).Value = _InvoiceData.Tables(0).Rows(_RowCounter)(i)
Next i
'Add Unbound DiscountTotal Field total to instance of the Fields collection (for data binding and summary totaling in the group footer)
Me.Fields("DiscountTotal").Value = Convert.ToDouble(Me.Fields("UnitPrice").Value, CultureInfo.CurrentCulture) * Convert.ToInt32(Me.Fields("Quantity").Value, CultureInfo.InvariantCulture) * Convert.ToDouble(Me.Fields("Discount").Value, CultureInfo.CurrentCulture)
'Advance Row Counter
eArgs.EOF = False
_RowCounter += 1
End If
End Sub
Private Sub ARUnboundDS_ReportStart(sender As Object, e As System.EventArgs) Handles Me.ReportStart
With PageSettings
' In inches unit.
.Margins.Left = CmToInch(2) ' Convert centimeters to inches.
.Margins.Right = CmToInch(1.2)
.Margins.Top = 1
.Margins.Bottom = 1
' Vertical paper setting.
.Orientation = PageOrientation.Portrait
' Paper Size A4.
.PaperKind = Drawing.Printing.PaperKind.A4
End With
End Sub
'// If you don't use script from Designer.
'Private Sub GroupFooter1_BeforePrint(sender As Object, e As System.EventArgs) Handles GroupFooter1.BeforePrint
' txtTotal.Text = Format(CDbl(txtSubTotal.Value) + CDbl(txtFreight.Value), "$###,##0.00")
'End Sub
End Class
โมดูลในการกำหนดตำแหน่งของไฟล์ฐานข้อมูล ... GetDBPath.vb
' GetDBPath is a helper class used to locate the Northwind sample database on the computer.
NotInheritable Class GetDBPath
' Used to get the full connection string to the Sample Database installed with ActiveReports
' Returns: String variable with the full connection string to the Sample Database
Public Shared ReadOnly Property GetNwindConnectString() As String
Get
Return "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=" + GetDBPath.GetPath(Application.StartupPath) + "nwind.mdb"
End Get
End Property
Public Shared Function GetPath(ByVal MyPath As String) As String
'/ Replace folder.
MyPath = MyPath.ToLower.Replace("\bin\debug", "\").Replace("\bin\release", "\").Replace("\bin\x86\debug", "\").Replace("\bin\x86\release", "\")
'// If not found folder then put the \ (BackSlash ASCII Code = 92) at the end.
If Microsoft.VisualBasic.Right(MyPath, 1) <> Chr(92) Then MyPath = MyPath & Chr(92)
Return MyPath
End Function 'GetPath
End Class
ดาวน์โหลดโค้ดฉบับเต็ม VB.NET (2010) + ActiveReports .Net (AR6) ได้ที่นี่ ...
ขอบคุณครับ
หน้า:
[1]