thongkorn โพสต์ 2018-9-12 12:26:07

[VB.NET] ปฐมบทการใช้งานตารางกริด GridGroupControl ของฟรีจากค่าย Syncfusion

http://www.g2gnet.com/webboard/images/vbnet/GGC.png

วันนี้แอดมินจะมาขอแนะนำ ระดับสุดยอดของฟรีจริงๆ แบบไม่น่าเชื่อ จนต้องร้องขอชีวิต 5555+ ... ดาวน์โหลดได้ฟรีที่ Syncfusion Essential รุ่น Community ซึ่งทางผู้ผลิตกำหนดการใช้งานฟรีว่า องค์กรนั้นๆต้องมี Deveopers ไม่เกิน 5 คน และมีผลประกอบการไม่เกิน 1 ล้าน U$ ... หลักๆที่ทางผู้ผลิตให้มาประกอบไปด้วย

- 800+ Controls and Frameworks
- Enterprise solutions for Big Data, Dashboards and Reporting
- A $15,960 value for FREE
- Support and updates included

Control ตัวแรกที่แอดมินทดสอบคือ ตารางกริด GridGroupingControl ซึ่งจะขอเรียกชื่อย่อว่า GGC กันนะครับ ซึ่งเป็นตารางกริดแบบแสดงผลทั่วไปได้ และสามารถทำการจัดกลุ่มได้ด้วย (อันนี้สำมะคัญเลย) การทดสอบทั่วๆไปที่แอดมินวางแนวทางไว้ดังนี้ คือ ...
- เรื่องหน้าตา ก่อนล่ะ อันนี้ผ่านฉลุยตั้งแต่แรกเริ่ม 5555+
- ทำงานได้รวดเร็ว อันนี้แอดมินลองทดสอบดูเอง จึงได้นำข้อมูลมาเผยแพร่นี่แหละครับ
- การควบคุม ยากง่ายขนาดไหน ซึ่งมันอยู่ที่เรื่องประสบการณ์ของแต่ละบุคคล ในการค้นหาคำตอบ

วิธีการหลักๆในการศึกษา Control ต่างๆแล้ว เราจะต้องดูจาก คู่มือ หรือ Help ที่มีมาให้ เรื่องนี้เป็นเรื่องที่สำคัญที่หลายๆคนมักมองข้ามไป โดยเฉพาะบรรดามือใหม่ทั้งหลาย ที่ไม่ยอมอ่านกัน กะจะหาแต่ในเน็ต หรือ StackOverFlow นั่นแหละคือจุดอ่อนของตัวคุณเอง ทำให้ขาด "พื้นฐาน" ที่จะนำทางให้ไปต่อได้ในระยะยาว ...

อนึ่ง!!! ... Control ต่างๆที่เราใช้งานกันอยู่ในปัจจุบัน มันมีคุณสมบัติ (Properties) ประจำตัวอยู่นับร้อยๆอย่าง ซึ่งเราจดจำไม่ไหวหรอกครับ ดังนั้นในงานทั้งหมดของแอดมิน จะใช้วิธีการแบบ Run-Time คือใช้การเขียนโค้ด แทนที่จะจับ Control ลากมาวางแล้วปรับแต่งคุณสมบัติผ่าน Design-Time เพราะ ...

- เราไม่ต้องจดจำ แต่อาศัยความเข้าใจ
- เมื่อเข้าใจในพฤติกรรมของ Control แต่ละตัว ก็จะสามารถควบคุมมันได้ตามใจนึก ... สิ่งนี้จะนำทางให้เราเรียนรู้ฝึกฝน กับบรรดา Control ตัวใหม่ๆต่างค่ายได้อย่างไม่ยากเย็นนัก เพราะหลักการต่างๆมันแทบไม่ได้แตกต่างกันเลย

http://www.g2gnet.com/webboard/images/vbnet/GGCDesign.png
Design Time ลากแค่ GGC เข้ามาวางบนฟอร์ม แล้วปรับคุณสมบัติ Anchor เท่านั้น ส่วนอื่นๆเราจะใช้โค้ดกำหนดแทน

http://www.g2gnet.com/webboard/images/vbnet/GGCReference.png
Reference ที่จำเป็นต้องใช้งาน

มาดูโค้ดกันเถอะ ...
#Region "About"
' / -----------------------------------------------------------------
' / Developer : Mr.Surapon Yodsanga (Thongkorn Tubtimkrob)
' / eMail : thongkorn@hotmail.com
' / URL: http://www.g2gnet.com (Khon Kaen - Thailand)
' / Facebook: https://www.facebook.com/g2gnet (For Thailand)
' / Facebook: https://www.facebook.com/commonindy (Worldwide)
' / Purpose: Sample code for GridGroupingControl of Syncfusion Community.
' / Microsoft Visual Basic .NET (2010) & MS Access 2007+
' /
' / This is open source code under @CopyLeft by Thongkorn Tubtimkrob.
' / You can modify and/or distribute without to inform the developer.
' / -----------------------------------------------------------------
#End Region

Imports Syncfusion.Windows.Forms
Imports Syncfusion.Windows.Forms.Grid
Imports Syncfusion.Grouping
Imports Syncfusion.Drawing
'// DataBase
Imports System.Data.OleDb

Public Class frmGGCSample

    Private Sub frmGGCSample_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      Call ConnectDataBase()
      Call RetrieveData(False)
    End Sub

    ' / -----------------------------------------------------------------
    ' / blnSearch = True, It's search for specified data, False = all data is displayed.
    Private Sub RetrieveData(Optional ByVal blnSearch As Boolean = False)
      strSQL = _
            " SELECT tblSample.PrimaryKey, tblSample.ID, tblSample.NumberField, tblSample.DoubleField, " & _
            " tblSample.DateField, tblGroup.GroupName " & _
            " FROM tblSample INNER JOIN tblGroup ON tblSample.GroupFK = tblGroup.GroupPK "

      '// blnSearch = True for Search
      If blnSearch Then
            strSQL = strSQL & _
                " WHERE " & _
                " " & " Like '%" & txtSearch.Text & "%'" & " OR " & _
                " " & " Like '%" & txtSearch.Text & "%'" & _
                " ORDER BY PrimaryKey "
      Else
            strSQL = strSQL & " ORDER BY PrimaryKey "
      End If
      '//
      If Conn.State = ConnectionState.Closed Then Conn.Open()
      '// Creates Data Adapter.
      DA = New OleDbDataAdapter(strSQL, Conn)
      ' Creates and fills Data Set.
      DS = New DataSet
      DA.Fill(DS)
      Me.GGC.DataSource = DS.Tables(0)
      lblRecordCount.Text = ""
      DA.Dispose()
      DS.Dispose()
      Conn.Close()
      '//
      Call InitGridGroup()
      '//
      txtSearch.Clear()
    End Sub

    Private Sub btnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefresh.Click
      Call RetrieveData(False)
    End Sub

    Private Sub txtSearch_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtSearch.KeyPress
      If Trim(txtSearch.Text) = "" Or Len(Trim(txtSearch.Text)) = 0 Then Exit Sub
      '// Undesirable characters for the database ex.', * or %
      txtSearch.Text = Replace(Trim(txtSearch.Text), "'", "")
      txtSearch.Text = Replace(Trim(txtSearch.Text), "%", "")
      txtSearch.Text = Replace(Trim(txtSearch.Text), "*", "")
      ' RetrieveData(True) It means searching for information.
      If e.KeyChar = Chr(13) Then '// Press Enter
            '// No beep.
            e.Handled = True
            '//
            Call RetrieveData(True)
      End If
    End Sub

    ' / -----------------------------------------------------------------
    ' / Initilized GridGroupingControl
    Private Sub InitGridGroup()
      '// Initialize Columns GridGroup
      With Me.GGC
            '// Hidden Primary Key Column
            .TableDescriptor.VisibleColumns.Remove("PrimaryKey")
            'Using Column Name
            .TableDescriptor.Columns("ID").HeaderText = "ID"
            .TableDescriptor.Columns("NumberField").HeaderText = "Number Value"
            .TableDescriptor.Columns("DoubleField").HeaderText = "Double Value"
            .TableDescriptor.Columns("DateField").HeaderText = "Date"
            .TableDescriptor.Columns("DateField").Appearance.AnyRecordFieldCell.Format = "dd/MM/yyyy"
            .TableDescriptor.Columns("DateField").Appearance.AnyRecordFieldCell.CellType = GridCellTypeName.TextBox
      End With
      '// GridVerticalAlignment.Middle
      For i As Byte = 0 To 5
            With Me.GGC
                .TableDescriptor.Columns(i).Appearance.AnyRecordFieldCell.VerticalAlignment = GridVerticalAlignment.Middle
                .TableDescriptor.Columns(i).AllowGroupByColumn = False
            End With
      Next
      '// Initialize normal GridGrouping
      With Me.GGC
            ' Allows GroupDropArea to be visible
            .ShowGroupDropArea = False' Disable
            '// Hidden Top Level of Grouping
            .TopLevelGroupOptions.ShowCaption = False

            '// Metro Styles
            .GridVisualStyles = Syncfusion.Windows.Forms.GridVisualStyles.Metro
            ' Disables editing in GridGroupingControl
            .ActivateCurrentCellBehavior = GridCellActivateAction.None
            '.ActivateCurrentCellBehavior = GridCellActivateAction.ClickOnCell
            '// Disable Add New
            .TableDescriptor.AllowNew = False
            '// Autofit Columns
            .AllowProportionalColumnSizing = True

            '// Row Height
            .Table.DefaultRecordRowHeight = 25
            '//
            .Table.DefaultCaptionRowHeight = 25
            .Table.DefaultColumnHeaderRowHeight = 30    '// Columns Header

            '// Selection
            .TableOptions.ListBoxSelectionMode = SelectionMode.One
            'Selection Back color
            .TableOptions.SelectionBackColor = Color.Firebrick
            '//
            .Appearance.ColumnHeaderCell.TextColor = Color.DarkBlue

            'Applies back color as LightCyan for alternative records in the Grid.
            .Appearance.AlternateRecordFieldCell.BackColor = Color.LightCyan

            '.TableModel.Options.SelectCellsMouseButtonsMask = MouseButtons.Right

            '/ Disable record preview row
            .TableOptions.ShowRecordPreviewRow = False
            '//
            '/ Will enable the Group Header for the top most group.
            .TopLevelGroupOptions.ShowGroupHeader = False ' True
            '/ Will enable the Group Footer for the group.
            .TopLevelGroupOptions.ShowGroupFooter = False 'True
            '//
            .TableOptions.GroupHeaderSectionHeight = 30
            .TableOptions.GroupFooterSectionHeight = 30
      End With

    End Sub

    ' / -----------------------------------------------------------------
    '// Double click event for show Primary Key which hidden in Column(0)
    Private Sub GGC_TableControlCellDoubleClick(ByVal sender As Object, ByVal e As Syncfusion.Windows.Forms.Grid.Grouping.GridTableControlCellClickEventArgs) Handles GGC.TableControlCellDoubleClick
      '// Row of Column Header
      If e.Inner.RowIndex <= 1 Then Return
      '/ Notify the double click performed in a cell
      Dim rec As Record = Me.GGC.Table.DisplayElements(e.TableControl.CurrentCell.RowIndex).ParentRecord
      If (rec) IsNot Nothing Then
            MsgBox("Primary key = " & rec.GetValue("PrimaryKey").ToString)
      End If
    End Sub

    ' / -----------------------------------------------------------------
    '// Full Select Row
    Private Sub GGC_TableControlCurrentCellActivating(ByVal sender As Object, ByVal e As Syncfusion.Windows.Forms.Grid.Grouping.GridTableControlCurrentCellActivatingEventArgs) Handles GGC.TableControlCurrentCellActivating
      '// Get Column Index 0 is the Primary Key. (Hidden column)
      e.Inner.ColIndex = 0
    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
      Me.Close()
    End Sub

    ' / -----------------------------------------------------------------
    '// Press enter each row.
    Private Sub GGC_TableControlCurrentCellKeyPress(sender As Object, e As Syncfusion.Windows.Forms.Grid.Grouping.GridTableControlKeyPressEventArgs) Handles GGC.TableControlCurrentCellKeyPress
      '// Check rows count before.
      If GGC.TableModel.RowCount <= 0 Then Return
      '/ Notify the current cell keypress
      Dim rec As Record = Me.GGC.Table.DisplayElements(GGC.TableControl.CurrentCell.RowIndex).ParentRecord
      If (rec) IsNot Nothing Then
            MsgBox("Primary key = " & rec.GetValue("PrimaryKey").ToString)
      End If
    End Sub

    Private Sub frmGGCSample_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
      Me.Dispose()
      Application.Exit()
    End Sub
End Class
โมดูลหากิน ... modDataBase.vb
' / --------------------------------------------------------------------------------
' / Developer : Mr.Surapon Yodsanga (Thongkorn Tubtimkrob)
' / eMail : thongkorn@hotmail.com
' / URL: http://www.g2gnet.com (Khon Kaen - Thailand)
' / Facebook: https://www.facebook.com/g2gnet (For Thailand)
' / Facebook: https://www.facebook.com/commonindy (Worldwide)
' / Microsoft Visual Basic .NET (2010)
' /
' / This is open source code under @Copyleft by Thongkorn Tubtimkrob.
' / You can modify and/or distribute without to inform the developer.
' / --------------------------------------------------------------------------------
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

    '// Data Path
    Public strPathData As String = MyPath(Application.StartupPath)
    '// Images Path
    'Public strPathImages As String = MyPath(Application.StartupPath)

    Public Function ConnectDataBase() As System.Data.OleDb.OleDbConnection
      strPathData = MyPath(Application.StartupPath) & "Data\"
      'strPathImages = MyPath(Application.StartupPath) & "Images\"
      Dim strConn As String = _
            " Provider = Microsoft.ACE.OLEDB.12.0; " & _
            " Data Source = " & strPathData & "Sample.accdb"

      Conn = New OleDb.OleDbConnection(strConn)
      ' Create Connection
      Conn.ConnectionString = strConn
      ' Return
      Return Conn
    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) at the end.
      If Right(MyPath, 1) <> "\" Then MyPath = MyPath & "\"
    End Function
End Module
ดาวน์โหลดโค้ดฉบับเต็ม VB.NET (2010) ได้ที่นี่ ...
หน้า: [1]
ดูในรูปแบบกติ: [VB.NET] ปฐมบทการใช้งานตารางกริด GridGroupControl ของฟรีจากค่าย Syncfusion