|  | 
 
|  
 จากบทความเรื่อง [VB.NET] การพิมพ์รายงานด้วย ActiveReports .NET แบบวิธีการ UnBound Data เราสามารถปรับแต่งเล็กน้อยเพื่อทำการจัดกลุ่ม (Grouping) ให้กับรายงานได้ไม่ยากเลยครับ ...
 
 การกำหนดฟิลด์เพื่อจัดกลุ่มให้กับ GroupHeader ...
 
  
 ข้อมูลของ DepartmentName จะมีอยู่ 5 กลุ่ม ...
 
  
 การเพิ่ม Group Header/Footer ...
 
  
 มาดูโค้ดฉบับเต็มกันเถอะ ... ฟอร์มหลัก
 
 คัดลอกไปที่คลิปบอร์ดPublic Class frmUnBoundGroup
    Private Sub frmUnBoundGroup_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        '// Connect to DataBase. 
        If Not ConnectDataBase() Then
            Me.Close()
            Exit Sub
        End If
        '// ประกาศตัวแปร rpt จากหน้ารายงาน ActiveReports
        Dim rpt As New NewActiveReport1
        '// สั่งรัน
        rpt.Run()
        Me.Viewer1.ReportViewer.Zoom = 1    '(100%)
        '// แสดงผลรายงานเอกสาร
        Me.Viewer1.Document = rpt.Document
    End Sub
    Private Sub frmUnBoundData_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Me.Dispose()
        GC.SuppressFinalize(Me)
        Application.Exit()
    End Sub
End Class
โค้ดในส่วนของโมดูล (หากิน) ในการเชื่อมต่อกับฐานข้อมูล ...
 
 คัดลอกไปที่คลิปบอร์ดImports System.Data.OleDb
Imports Microsoft.VisualBasic
Module modDataBase
    '// Declare variable one time but use many times.
    Public Conn As OleDbConnection
    Public Cmd As OleDbCommand
    Public DS As DataSet
    Public DR As OleDbDataReader
    Public DA As OleDbDataAdapter
    Public strSQL As String '// Major SQL
    Public strStmt As String    '// Minor SQL
    '// Data Path 
    Public strPathData As String = MyPath(Application.StartupPath)
    Public Function ConnectDataBase() As Boolean
        Try
            strPathData = MyPath(Application.StartupPath) & "Data"
            Dim strConn As String = _
                "Provider = Microsoft.ACE.OLEDB.12.0;" & _
                "Data Source = " & strPathData & "Contact.accdb"
            Conn = New OleDb.OleDbConnection(strConn)
            Conn.Open()
            ' Return
            Return True
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            Return False
        End Try
    End Function
    ' / --------------------------------------------------------------------------------
    ' / Get my project path
    ' / AppPath = C:\My Project\bin\debug
    ' / Replace "\bin\debug" with ""
    ' / Return : C:\My Project\
    Function MyPath(AppPath As String) As String
        '/ MessageBox.Show(AppPath);
        AppPath = AppPath.ToLower()
        '/ Return Value
        MyPath = AppPath.Replace("\bin\debug", "").Replace("\bin\release", "").Replace("\bin\x86\debug", "")
        '// If not found folder then put the \ (BackSlash ASCII Code = 92) at the end.
        If Right(MyPath, 1) <> Chr(92) Then MyPath = MyPath & Chr(92)
    End Function
End Module
โค้ดในส่วนของ ActiveReports .NET ...
 
 คัดลอกไปที่คลิปบอร์ดImports System.Data.OleDb
Imports DataDynamics.ActiveReports
Imports DataDynamics.ActiveReports.Document 
Public Class NewActiveReport1
    Private ItemNo As Integer
    '// Start Here.
    Private Sub NewActiveReport1_ReportStart(sender As Object, e As System.EventArgs) Handles Me.ReportStart
        '/ การตั้งค่าหน้ากระดาษ
        With PageSettings
            '/ หน่วยวัดเป็นนิ้ว แต่ใช้การแปลงหน่วยด้วย CmToInch เข้าช่วย หากเราถนัดหน่วยวัด ซม.
            .Margins.Left = CmToInch(0.7) '// แปลงค่า 0.7 ซม. เป็นนิ้ว
            .Margins.Right = CmToInch(0.5)
            .Margins.Top = 1.0
            .Margins.Bottom = 1
            ' ตั้งค่ากระดาษแนวตั้ง
            .Orientation = PageOrientation.Portrait
            ' กระดาษขนาด A4
            .PaperKind = Drawing.Printing.PaperKind.A4
        End With
        '// เคลียร์ค่าก่อนการพิมพ์
        txtFullName.Text = ""
        txtNickName.Text = ""
        txtPositionName.Text = ""
        txtDepartmentName.Text = ""
        ItemNo = 0
        '// เริ่มต้นการอ่านข้อมูล
        Try
            '// ทำการ Query ฟิลด์ข้อมูลที่ต้องการมาแสดงผล
            strSQL = _
                " SELECT tblContact.ContactPK, tblContact.Fullname, tblContact.Nickname, tblPosition.PositionName, tblDepartment.DepartmentName  " & _
                " FROM tblDepartment INNER JOIN (tblPosition INNER JOIN tblContact ON tblPosition.PositionPK = tblContact.PositionFK) ON " & _
                " tblDepartment.DepartmentPK = tblContact.DepartmentFK " & _
                " ORDER BY DepartmentName, ContactPK "
            '// Open connection and create DataReader.     
            Cmd = New OleDbCommand(strSQL, Conn)
            If Conn.State = ConnectionState.Closed Then Conn.Open()
            DR = Cmd.ExecuteReader()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    '// Add fields to the report's fields collection.
    Private Sub NewActiveReport1_DataInitialize(sender As Object, e As System.EventArgs) Handles Me.DataInitialize
        Fields.Add("FullName")
        Fields.Add("NickName")
        Fields.Add("PositionName")
        Fields.Add("DepartmentName")
    End Sub
    '// Retrieve information to populate the report fields.
    Private Sub NewActiveReport1_FetchData(sender As Object, eArgs As DataDynamics.ActiveReports.ActiveReport.FetchEventArgs) Handles Me.FetchData
        Try
            DR.Read()
            '// นำข้อมูลมาผูกเข้ากับฟิลด์ที่เรากำหนดบน ActiveReports
            Me.Fields("FullName").Value = DR("FullName")
            Me.Fields("NickName").Value = DR("NickName")
            Me.Fields("PositionName").Value = DR("PositionName")
            Me.Fields("DepartmentName").Value = DR("DepartmentName")
            '// ยังไม่หมดข้อมูล End Of File = เท็จ ... กระโดดไปพิมพ์ที่ Detail1_Format อีกครั้ง
            eArgs.EOF = False
        Catch ex As Exception
            'MessageBox.Show(ex.ToString())
            '// แสดงว่าหมดข้อมูลแล้ว นั่นคือสิ้นสุดการพิมพ์
            eArgs.EOF = True
        End Try
    End Sub
    '// หากเหตุการณ์ FetchData ทำให้ EOF = False แสดงว่ายังไม่หมดข้อมูล จะเกิดการพิมพ์ไปเรื่อยๆ
    Private Sub Detail1_Format(sender As System.Object, e As System.EventArgs) Handles Detail1.Format
        '// นำค่าจากฟิลด์ที่เรากำหนดมาแสดงผลใน TextBox ของ ActiveReports
        txtFullName.Text = Me.Fields("FullName").Value
        txtNickName.Text = Me.Fields("NickName").Value
        txtPositionName.Text = Me.Fields("PositionName").Value
        ItemNo = ItemNo + 1
    End Sub
    Private Sub NewActiveReport1_ReportEnd(sender As Object, e As System.EventArgs) Handles Me.ReportEnd
        DR.Close()
        Conn.Close()
    End Sub
    Private Sub GroupHeader1_Format(sender As System.Object, e As System.EventArgs) Handles GroupHeader1.Format
        Me.GroupHeader1.ColumnLayout = True
        Me.GroupHeader1.DataField = "DepartmentName"
        txtDepartmentName.Text = Me.Fields("DepartmentName").Value
        'Me.GroupHeader1.GroupKeepTogether = GroupKeepTogether.None
        'Me.GroupHeader1.KeepTogether = True
        'Me.GroupHeader1.NewColumn = NewColumn.None
        'Me.GroupHeader1.NewPage = NewPage.None
        'Me.GroupHeader1.RepeatStyle = RepeatStyle.None
        'Me.GroupHeader1.UnderlayNext = False
    End Sub
    Private Sub GroupFooter1_Format(sender As System.Object, e As System.EventArgs) Handles GroupFooter1.Format
        '// NewPage.None จะพิมพ์กลุ่มต่อไป
        Me.GroupFooter1.NewPage = NewPage.After
        txtSum.Text = "Total: " & ItemNo
        ItemNo = 0
    End Sub
End Class
ดาวน์โหลดโค้ดต้นฉบับ VB.NET (2010) ได้ที่นี่ ...
 
 | 
 
xขออภัย! โพสต์นี้มีไฟล์แนบหรือรูปภาพที่ไม่ได้รับอนุญาตให้คุณเข้าถึงคุณจำเป็นต้อง ลงชื่อเข้าใช้ เพื่อดาวน์โหลดหรือดูไฟล์แนบนี้ คุณยังไม่มีบัญชีใช่ไหม? ลงทะเบียน  |