ชุมชนคนรักภาษาเบสิค - Visual Basic Community

 ลืมรหัสผ่าน
 ลงทะเบียน
ค้นหา
ดู: 3307|ตอบกลับ: 1

[VB.NET] การพิมพ์ใบกำกับภาษี (Invoice) ด้วย ActiveReports แบบ Unbound DataSet

[คัดลอกลิงก์]

309

กระทู้

500

โพสต์

6030

เครดิต

ผู้ดูแลระบบ

ทองก้อน ทับทิมกรอบ

Rank: 9Rank: 9Rank: 9

เครดิต
6030



โค้ดชุดนี้เป็น Demo ActiveReports .Net หรือตัวอย่างจากทางค่าย GrapeCity (DataDynamics) ซึ่งแอดมินนำมาปรับแต่งโมดิฟายใหม่ เพื่อให้สะดวกต่อการรันโปรแกรม โดยที่ชุดตัวอย่างจะมี Data Source เป็นแบบข้อมูล Unbound Data อยู่ 4 แบบ ประกอบด้วย DataSet, DataReader, Text File และ Data Array สำหรับในบทความชุดนี้ แอดมินเลือกมาเพียง DataSet เพื่อเป็นตัวอย่างให้ศึกษากันครับ ...

ตัวอย่างคุณสมบัติในส่วนของ PageHeader ... การกำหนด DataField จะต้องพิมพ์เองโดยเลือกมาจากฟิลด์ข้อมูลของตารางข้อมูล


ตัวอย่างคุณสมบัติในส่วนของ GroupHeader ... ให้สังเกตว่ามีการจัดกลุ่มด้วยฟิลด์ OrderID


ตัวอย่างคุณสมบัติในส่วนของ Detail ... DataField แต่ละตัวจะต้องพิมพ์เอง ตามชื่อฟิลด์จากตารางข้อมูล


ตัวอย่างคุณสมบัติในส่วนของ GroupFooter ... จะมีการทำคำสั่งในส่วนของ Summary เพื่อสรุปยอดเงิน


ส่วนของ Script ซึ่งในตัวอย่างจะเป็น C# แต่ แอดมินเปลี่ยนเป็น VB.NET แทน และเพิ่มโค้ดในส่วนของ GroupFooter1_BeforePrint ซึ่งแอดมินชอบแบบเขียนคำสั่งลงใน Designer มากกว่า


มาดูโค้ดฉบับเต็มสำหรับฟอร์มหลัก ...
  1. Public Class frmAR6DataSet

  2.     Private Sub frmAR6DataSet_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
  3.         'Create the report
  4.         Dim rpt As New ARUnboundDS()

  5.         'Run and view the report
  6.         rpt.Run(False)
  7.         Me.Viewer1.Document = rpt.Document
  8.     End Sub

  9. End Class
คัดลอกไปที่คลิปบอร์ด

โค้ดในส่วนของ ActiveReports ...
  1. Imports System
  2. Imports System.Data
  3. Imports System.Data.OleDb
  4. Imports System.Globalization
  5. Imports DataDynamics.ActiveReports
  6. Imports DataDynamics.ActiveReports.Document

  7. Public Class ARUnboundDS
  8.     'DataSource for the report
  9.     Private _InvoiceData As DataSet
  10.     Private _RowCounter As Integer

  11.     ' DataInitialize Event
  12.     ' ActiveReports event that is called during the report initalization
  13.     ' procedure. (after .Run is called on your report object)  Normally used
  14.     ' with unbound reporting to establish an active connection to your data
  15.     ' to be used with the FetchData event, or to setup a bound report with a
  16.     ' dynamic database connection at runtime.
  17.     Private Sub ARUnboundDS_DataInitialize(sender As Object, e As System.EventArgs) Handles Me.DataInitialize
  18.         'Dataset to hold data
  19.         _InvoiceData = New DataSet()
  20.         _InvoiceData.Locale = CultureInfo.InvariantCulture

  21.         'Database connection populated from the sample Northwind access database
  22.         'MsgBox(GetDBPath.GetNwindConnectString)
  23.         Dim _nwindConn As New OleDbConnection(GetDBPath.GetNwindConnectString)

  24.         'SQL Select command to run against the database
  25.         Dim _selectCMD As New OleDbCommand("SELECT * FROM Invoices ORDER BY Customers.CompanyName, OrderID", _nwindConn)
  26.         _selectCMD.CommandTimeout = 30

  27.         'Data adapter used to run the select command
  28.         Dim _InvoicesDA As New OleDbDataAdapter()
  29.         _InvoicesDA.SelectCommand = _selectCMD

  30.         'Fill the DataSet
  31.         _InvoicesDA.Fill(_InvoiceData, "Invoices")

  32.         'Initialize counter to first row of the DataSet        
  33.         _RowCounter = 0

  34.         'Add all the columns from the DataSet as Fields for the report
  35.         Dim i As Integer
  36.         For i = 0 To (_InvoiceData.Tables(0).Columns.Count) - 1
  37.             Me.Fields.Add(_InvoiceData.Tables(0).Columns(i).ColumnName)
  38.         Next i

  39.         'Add Calculated Fields
  40.         Me.Fields.Add("DiscountTotal")

  41.     End Sub

  42.     ' FetchData Event
  43.     ' ActiveReports event that is called during the report run once per
  44.     ' row from the dataset.  This event is usually only used in unbound reporting,
  45.     ' you would set the Fields collection value to the value from your dataset and
  46.     ' advance the next row.  When you run out of data, you should set the
  47.     ' FetchEventArgs argument's EOF field to true to tell the report that the report
  48.     ' is done with the data.
  49.     Private Sub ARUnboundDS_FetchData(sender As Object, eArgs As DataDynamics.ActiveReports.ActiveReport.FetchEventArgs) Handles Me.FetchData
  50.         If _RowCounter = _InvoiceData.Tables(0).Rows.Count Then
  51.             'If the row counter has reached the end of the data then
  52.             'set the eArgs.EOF flag to true and exit the procedure
  53.             eArgs.EOF = True
  54.             Return
  55.         Else
  56.             'Populate the fields collection from the DataSet
  57.             Dim i As Integer
  58.             For i = 0 To (_InvoiceData.Tables(0).Columns.Count) - 1
  59.                 Me.Fields(_InvoiceData.Tables(0).Columns(i).ColumnName).Value = _InvoiceData.Tables(0).Rows(_RowCounter)(i)
  60.             Next i

  61.             'Add Unbound DiscountTotal Field total to instance of the Fields collection (for data binding and summary totaling in the group footer)
  62.             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)

  63.             'Advance Row Counter
  64.             eArgs.EOF = False
  65.             _RowCounter += 1
  66.         End If
  67.     End Sub

  68.     Private Sub ARUnboundDS_ReportStart(sender As Object, e As System.EventArgs) Handles Me.ReportStart
  69.         With PageSettings
  70.             ' In inches unit.
  71.             .Margins.Left = CmToInch(2) ' Convert centimeters to inches.
  72.             .Margins.Right = CmToInch(1.2)
  73.             .Margins.Top = 1
  74.             .Margins.Bottom = 1
  75.             ' Vertical paper setting.
  76.             .Orientation = PageOrientation.Portrait
  77.             ' Paper Size A4.
  78.             .PaperKind = Drawing.Printing.PaperKind.A4
  79.         End With
  80.     End Sub

  81.     '// If you don't use script from Designer.
  82.     'Private Sub GroupFooter1_BeforePrint(sender As Object, e As System.EventArgs) Handles GroupFooter1.BeforePrint
  83.     '    txtTotal.Text = Format(CDbl(txtSubTotal.Value) + CDbl(txtFreight.Value), "$###,##0.00")
  84.     'End Sub
  85. End Class
คัดลอกไปที่คลิปบอร์ด

โมดูลในการกำหนดตำแหน่งของไฟล์ฐานข้อมูล ... GetDBPath.vb
  1. ' GetDBPath is a helper class used to locate the Northwind sample database on the computer.
  2. NotInheritable Class GetDBPath
  3.     ' Used to get the full connection string to the Sample Database installed with ActiveReports
  4.     ' Returns: String variable with the full connection string to the Sample Database      
  5.     Public Shared ReadOnly Property GetNwindConnectString() As String
  6.         Get
  7.             Return "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=" + GetDBPath.GetPath(Application.StartupPath) + "nwind.mdb"
  8.         End Get
  9.     End Property

  10.     Public Shared Function GetPath(ByVal MyPath As String) As String
  11.         '/ Replace folder.
  12.         MyPath = MyPath.ToLower.Replace("\bin\debug", "").Replace("\bin\release", "").Replace("\bin\x86\debug", "").Replace("\bin\x86\release", "")
  13.         '// If not found folder then put the \ (BackSlash ASCII Code = 92) at the end.
  14.         If Microsoft.VisualBasic.Right(MyPath, 1) <> Chr(92) Then MyPath = MyPath & Chr(92)
  15.         Return MyPath
  16.     End Function 'GetPath
  17. End Class
คัดลอกไปที่คลิปบอร์ด

ดาวน์โหลดโค้ดฉบับเต็ม VB.NET (2010) + ActiveReports .Net (AR6) ได้ที่นี่ ...

ขออภัย! โพสต์นี้มีไฟล์แนบหรือรูปภาพที่ไม่ได้รับอนุญาตให้คุณเข้าถึง

คุณจำเป็นต้อง ลงชื่อเข้าใช้ เพื่อดาวน์โหลดหรือดูไฟล์แนบนี้ คุณยังไม่มีบัญชีใช่ไหม? ลงทะเบียน

x
สิ่งที่ดีกว่าการให้ คือการให้แบบไม่มีที่สิ้นสุด

0

กระทู้

58

โพสต์

10

เครดิต

Member

Rank: 2

เครดิต
10
โพสต์ 2022-10-25 15:20:28 | ดูโพสต์ทั้งหมด

ขอบคุณครับ
ขออภัย! คุณไม่ได้รับสิทธิ์ในการดำเนินการในส่วนนี้ กรุณาเลือกอย่างใดอย่างหนึ่ง ลงชื่อเข้าใช้ | ลงทะเบียน

รายละเอียดเครดิต

ข้อความล้วน|อุปกรณ์พกพา|ประวัติการแบน|G2GNet.com  

GMT+7, 2024-4-20 03:15 , Processed in 0.396492 second(s), 13 queries , File On.

Powered by Discuz! X3.4, Rev.62

Copyright © 2001-2020 Tencent Cloud.

ตอบกระทู้ ขึ้นไปด้านบน ไปที่หน้ารายการกระทู้