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

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

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

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

309

กระทู้

500

โพสต์

6030

เครดิต

ผู้ดูแลระบบ

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

Rank: 9Rank: 9Rank: 9

เครดิต
6030



วันนี้แอดมินจะมาขอแนะนำ ระดับสุดยอดของฟรีจริงๆ แบบไม่น่าเชื่อ จนต้องร้องขอชีวิต 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 ตัวใหม่ๆต่างค่ายได้อย่างไม่ยากเย็นนัก เพราะหลักการต่างๆมันแทบไม่ได้แตกต่างกันเลย


Design Time ลากแค่ GGC เข้ามาวางบนฟอร์ม แล้วปรับคุณสมบัติ Anchor เท่านั้น ส่วนอื่นๆเราจะใช้โค้ดกำหนดแทน


Reference ที่จำเป็นต้องใช้งาน

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

  15. Imports Syncfusion.Windows.Forms
  16. Imports Syncfusion.Windows.Forms.Grid
  17. Imports Syncfusion.Grouping
  18. Imports Syncfusion.Drawing
  19. '// DataBase
  20. Imports System.Data.OleDb

  21. Public Class frmGGCSample

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

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

  33.         '// blnSearch = True for Search
  34.         If blnSearch Then
  35.             strSQL = strSQL & _
  36.                 " WHERE " & _
  37.                 " [ID] " & " Like '%" & txtSearch.Text & "%'" & " OR " & _
  38.                 " [GroupName] " & " Like '%" & txtSearch.Text & "%'" & _
  39.                 " ORDER BY PrimaryKey "
  40.         Else
  41.             strSQL = strSQL & " ORDER BY PrimaryKey "
  42.         End If
  43.         '//
  44.         If Conn.State = ConnectionState.Closed Then Conn.Open()
  45.         '// Creates Data Adapter.
  46.         DA = New OleDbDataAdapter(strSQL, Conn)
  47.         ' Creates and fills Data Set.
  48.         DS = New DataSet
  49.         DA.Fill(DS)
  50.         Me.GGC.DataSource = DS.Tables(0)
  51.         lblRecordCount.Text = "[Total: " & Format(DS.Tables(0).Rows.Count, "#,##") & " Records.]"
  52.         DA.Dispose()
  53.         DS.Dispose()
  54.         Conn.Close()
  55.         '//
  56.         Call InitGridGroup()
  57.         '//
  58.         txtSearch.Clear()
  59.     End Sub

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

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

  77.     ' / -----------------------------------------------------------------
  78.     ' / Initilized GridGroupingControl
  79.     Private Sub InitGridGroup()
  80.         '// Initialize Columns GridGroup
  81.         With Me.GGC
  82.             '// Hidden Primary Key Column
  83.             .TableDescriptor.VisibleColumns.Remove("PrimaryKey")
  84.             'Using Column Name
  85.             .TableDescriptor.Columns("ID").HeaderText = "ID"
  86.             .TableDescriptor.Columns("NumberField").HeaderText = "Number Value"
  87.             .TableDescriptor.Columns("DoubleField").HeaderText = "Double Value"
  88.             .TableDescriptor.Columns("DateField").HeaderText = "Date"
  89.             .TableDescriptor.Columns("DateField").Appearance.AnyRecordFieldCell.Format = "dd/MM/yyyy"
  90.             .TableDescriptor.Columns("DateField").Appearance.AnyRecordFieldCell.CellType = GridCellTypeName.TextBox
  91.         End With
  92.         '// GridVerticalAlignment.Middle
  93.         For i As Byte = 0 To 5
  94.             With Me.GGC
  95.                 .TableDescriptor.Columns(i).Appearance.AnyRecordFieldCell.VerticalAlignment = GridVerticalAlignment.Middle
  96.                 .TableDescriptor.Columns(i).AllowGroupByColumn = False
  97.             End With
  98.         Next
  99.         '// Initialize normal GridGrouping
  100.         With Me.GGC
  101.             ' Allows GroupDropArea to be visible
  102.             .ShowGroupDropArea = False  ' Disable
  103.             '// Hidden Top Level of Grouping
  104.             .TopLevelGroupOptions.ShowCaption = False

  105.             '// Metro Styles
  106.             .GridVisualStyles = Syncfusion.Windows.Forms.GridVisualStyles.Metro
  107.             ' Disables editing in GridGroupingControl
  108.             .ActivateCurrentCellBehavior = GridCellActivateAction.None
  109.             '.ActivateCurrentCellBehavior = GridCellActivateAction.ClickOnCell
  110.             '// Disable Add New
  111.             .TableDescriptor.AllowNew = False
  112.             '// Autofit Columns
  113.             .AllowProportionalColumnSizing = True

  114.             '// Row Height
  115.             .Table.DefaultRecordRowHeight = 25
  116.             '//
  117.             .Table.DefaultCaptionRowHeight = 25
  118.             .Table.DefaultColumnHeaderRowHeight = 30    '// Columns Header

  119.             '// Selection
  120.             .TableOptions.ListBoxSelectionMode = SelectionMode.One
  121.             'Selection Back color
  122.             .TableOptions.SelectionBackColor = Color.Firebrick
  123.             '//
  124.             .Appearance.ColumnHeaderCell.TextColor = Color.DarkBlue

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

  127.             '.TableModel.Options.SelectCellsMouseButtonsMask = MouseButtons.Right

  128.             '/ Disable record preview row
  129.             .TableOptions.ShowRecordPreviewRow = False
  130.             '//
  131.             '/ Will enable the Group Header for the top most group.
  132.             .TopLevelGroupOptions.ShowGroupHeader = False ' True
  133.             '/ Will enable the Group Footer for the group.
  134.             .TopLevelGroupOptions.ShowGroupFooter = False 'True
  135.             '//
  136.             .TableOptions.GroupHeaderSectionHeight = 30
  137.             .TableOptions.GroupFooterSectionHeight = 30
  138.         End With

  139.     End Sub

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

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

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

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

  171.     Private Sub frmGGCSample_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
  172.         Me.Dispose()
  173.         Application.Exit()
  174.     End Sub
  175. End Class
คัดลอกไปที่คลิปบอร์ด

โมดูลหากิน ... modDataBase.vb
  1. ' / --------------------------------------------------------------------------------
  2. ' / Developer : Mr.Surapon Yodsanga (Thongkorn Tubtimkrob)
  3. ' / eMail : thongkorn@hotmail.com
  4. ' / URL: http://www.g2gnet.com (Khon Kaen - Thailand)
  5. ' / Facebook: https://www.facebook.com/g2gnet (For Thailand)
  6. ' / Facebook: https://www.facebook.com/commonindy (Worldwide)
  7. ' / Microsoft Visual Basic .NET (2010)
  8. ' /
  9. ' / This is open source code under @Copyleft by Thongkorn Tubtimkrob.
  10. ' / You can modify and/or distribute without to inform the developer.
  11. ' / --------------------------------------------------------------------------------
  12. Imports System.Data.OleDb
  13. Imports Microsoft.VisualBasic

  14. Module modDataBase
  15.     '// Declare variable one time but use many times.
  16.     Public Conn As OleDbConnection
  17.     Public Cmd As OleDbCommand
  18.     Public DS As DataSet
  19.     Public DR As OleDbDataReader
  20.     Public DA As OleDbDataAdapter
  21.     Public strSQL As String '// Major SQL

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

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

  32.         Conn = New OleDb.OleDbConnection(strConn)
  33.         ' Create Connection
  34.         Conn.ConnectionString = strConn
  35.         ' Return
  36.         Return Conn
  37.     End Function

  38.     ' / --------------------------------------------------------------------------------
  39.     ' / Get my project path
  40.     ' / AppPath = C:\My Project\bin\debug
  41.     ' / Replace "\bin\debug" with ""
  42.     ' / Return : C:\My Project\
  43.     Function MyPath(AppPath As String) As String
  44.         '/ MessageBox.Show(AppPath);
  45.         AppPath = AppPath.ToLower()
  46.         '/ Return Value
  47.         MyPath = AppPath.Replace("\bin\debug", "").Replace("\bin\release", "").Replace("\bin\x86\debug", "")
  48.         '// If not found folder then put the \ (BackSlash) at the end.
  49.         If Right(MyPath, 1) <> "" Then MyPath = MyPath & ""
  50.     End Function
  51. End Module
คัดลอกไปที่คลิปบอร์ด

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

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

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

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

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

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

GMT+7, 2024-4-20 08:47 , Processed in 0.188680 second(s), 4 queries , File On.

Powered by Discuz! X3.4, Rev.62

Copyright © 2001-2020 Tencent Cloud.

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