|  | 
 
|  
 โค้ดชุดนี้แอดมินขอนำเสนอ การทำสรุปรายงานตัวเลขของ PageFooter ในแต่ละหน้าว่าหน้านี้มียอดจำนวนเงินเท่าไหร่ และรวมยอดจำนวนเงินทุกหน้าที่ผ่านมา ลักษณะคล้ายๆกับมีการพิมพ์รายงานหลายๆหน้า แล้วทำการยกยอดมา แต่อันนี้ให้ไปใส่เพิ่มเติมเอง เพราะสามารถคำนวณหาได้ได้จาก ยอดรวมทุกหน้า - ยอดเฉพาะหน้านั้นๆ ... อนึ่ง!!! โค้ดชุดนี้จะต้องอาศัยทั้งการ Design Time และ Run Time มาใช้งานคู่กัน โดยให้ดูจากภาพ ...
 
 ตัวอย่างข้อมูลเพื่อทำการสร้าง DataTable เอาไว้เป็น DataSource ...
 
 คัดลอกไปที่คลิปบอร์ด    '// Sample Data
    Private Function FillDataSample() As DataTable
        Dim table As New DataTable
        table.Columns.Add("Col1")
        table.Columns.Add("Col2")
        Dim row As String()
        For i = 0 To 9
            '// Sample data.
            row = New String() {i + 1, Format(100 * (i + 1), "#,##0.00")}
            table.Rows.Add(row)
        Next
        Return table
    End Function
การกำหนดฟิลด์ให้กับชุดข้อมูล (เสมือนมาจากตารางข้อมูล) ...
 
  
 การสร้างฟิลด์ที่ใช้ในการคำนวณผลลัพธ์ เพื่อหาค่ายอดรวมในแต่ละหน้า ... (DataInitialize - ActiveReports)
 
 คัดลอกไปที่คลิปบอร์ด        '/ Add Calculated Fields and see properties setting too.
        Me.Fields.Add("SumPrice")
        Me.Fields.Add("SumTotal")
Design Time กับการกำหนดฟิลด์ผลรวมในแต่ละหน้า (PageTotal) ...
 
  
 Design Time กับการกำหนดฟิลด์ผลรวมของทุกๆหน้าแบบ Group ...
 
  
 มาดูโค้ดบนฟอร์มหลัก ...
 
 คัดลอกไปที่คลิปบอร์ดPublic Class frmAR6Summary
    Private Sub frmAR6Summary_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim DT As New DataTable
        '// Bound Data into DataTable.
        DT = FillDataSample()
        DS = New DataSet
        DS.Tables.Add(DT)
        dgvData.DataSource = DT
        '//
        Dim rpt1 As New arPrintSummary
        rpt1.Run()
        Me.Viewer1.Document = rpt1.Document
        Me.Viewer1.ReportViewer.Zoom = 1
    End Sub
    '// Sample Data
    Private Function FillDataSample() As DataTable
        Dim table As New DataTable
        table.Columns.Add("Col1")
        table.Columns.Add("Col2")
        Dim row As String()
        For i = 0 To 9
            '// Sample data.
            row = New String() {i + 1, Format(100 * (i + 1), "#,##0.00")}
            table.Rows.Add(row)
        Next
        Return table
    End Function
End Class
มาดูโค้ดบน ActiveReports .NET ... ขั้นตอนการทำงานจะเริ่มจาก ReportStart แล้วไปที่ DataInitialize จากนั้นก็ทำการ FetchData เพื่อทำการพิมพ์ ...
 
 คัดลอกไปที่คลิปบอร์ดImports DataDynamics.ActiveReports
Imports DataDynamics.ActiveReports.Document
Imports System.Data.OleDb
Public Class arPrintSummary
    Private ItemNo As Integer = 0
    Private _RowCounter As Integer
    Private Sub arPrintSummary_DataInitialize(sender As Object, e As System.EventArgs) Handles Me.DataInitialize
        '/ Add all the columns from the DataSet as Fields for the report
        Dim i As Integer
        For i = 0 To (DS.Tables(0).Columns.Count) - 1
            Me.Fields.Add(DS.Tables(0).Columns(i).ColumnName)
        Next i
        '/ Add Calculated Fields and see properties setting too.
        Me.Fields.Add("SumPrice")
        Me.Fields.Add("SumTotal")
    End Sub
    Private Sub arPrintSummary_FetchData(sender As Object, eArgs As DataDynamics.ActiveReports.ActiveReport.FetchEventArgs) Handles Me.FetchData
        If _RowCounter = DS.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 (DS.Tables(0).Columns.Count) - 1
                Me.Fields(DS.Tables(0).Columns(i).ColumnName).Value = DS.Tables(0).Rows(_RowCounter)(i)
            Next
            Me.Fields("SumPrice").Value = CDbl(DS.Tables(0).Rows(_RowCounter)(1))
            Me.Fields("SumTotal").Value = CDbl(DS.Tables(0).Rows(_RowCounter)(1))
            'Advance Row Counter
            eArgs.EOF = False
            _RowCounter += 1
        End If
    End Sub
    Private Sub arPrintSummary_ReportStart(sender As Object, e As System.EventArgs) Handles Me.ReportStart
        ' การตั้งค่าหน้ากระดาษ
        With PageSettings
            ' หน่วยวัดเป็นนิ้ว
            .Margins.Left = CmToInch(1) ' แปลงค่า 1.0 ซม. เป็นนิ้ว
            .Margins.Right = 0.1
            .Margins.Top = 0.5
            .Margins.Bottom = 0.2
            '// กรณีใช้กระดาษขนาด A5
            '.Orientation = PageOrientation.Landscape
            '.PaperKind = Drawing.Printing.PaperKind.A5
            '//
            '/  กรณีที่กำหนดขนาดกระดาษเองเพื่อทำการทดสอบ
            .PaperKind = Drawing.Printing.PaperKind.Custom
            '/ ตั้งค่ากระดาษแนวตั้ง
            .Orientation = PageOrientation.Portrait
            .PaperWidth = CmToInch(21) ' 21 ซม.
            '// ปรับความสูงให้สั้นลงเพื่อทดสอบการพิมพ์ในแต่ละหน้า และยอดสรุป
            .PaperHeight = CmToInch(10)
        End With
        '// เคลียร์ค่าต่างๆของ TextBox ก่อนการพิมพ์
        txtItem.Text = ""
        txtUnitPrice.Text = "0.00"
        txtSumPrice.Text = "0.00"
        txtSumTotal.Text = "0.00"
    End Sub
End Class
ดาวน์โหลดโค้ดต้นฉบับ VB.NET (2010) + ActiveReports .NET ได้ที่นี่ ...
 
 | 
 |