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

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

[VB.NET] จัดกลุ่มด้วยวิธีการเขียนโค้ดให้กับ GridGrouping ของฟรีจาก Syncfusion

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

311

กระทู้

502

โพสต์

6052

เครดิต

ผู้ดูแลระบบ

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

Rank: 9Rank: 9Rank: 9

เครดิต
6052



แอดมินได้แจกโค้ดวิธีการใช้งาน GridGrouping Control ของฟรีจากค่าย Syncfusion ไปหลายรอบแล้ว คราวนี้มาดู วิธีการจัดกลุ่มให้กับ Control ตัวนี้ โดยการเขียนด้วยโค้ดเข้าไปควบคุมแทนการใช้ Drag & Drop ซึ่งแม้ว่าวิธีนี้มันดูง่าย แต่กลับจะทำให้ผู้ใช้งานสับสนหรือไม่ค่อยกล้าใช้งาน แน่นอนล่ะว่าต้องเพิ่มความยากในการเขียนโปรแกรมขึ้นมาอยู่แล้วครับ สำหรับ Control ของค่ายนี้ค่อนข้างจะมีโค้ดของตารางกริด ที่แตกต่างไปจากค่ายอื่นๆเขาเยอะมาก ทำให้เราเรียนรู้ได้ช้า และจดจำได้ยาก ดังนั้นควรจะศึกษาจากโค้ดแบบ Run Time ทดแทนการจดจำด้วยวิธีการ Design Time ครับผม ...

Add References ... ต้องใช้ Net FrameWork 4.0 ชุดเต็มด้วยน่ะครับ


มาดูโค้ดต้นฉบับกันเถอะ ...
  1. '// https://help.syncfusion.com/windowsforms/classic/gridgroupingcontrol/overview
  2. Imports Syncfusion.Windows.Forms
  3. Imports Syncfusion.Windows.Forms.Grid
  4. Imports Syncfusion.Grouping

  5. '// DataBase
  6. Imports System.Data.OleDb

  7. Public Class frmGroupGGC

  8.     Private Sub frmGGCSample_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
  9.         Me.Dispose()
  10.         Application.Exit()
  11.     End Sub

  12.     Private Sub frmGGCSample_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  13.         Call ConnectDataBase()
  14.         '//
  15.         With cmbGroup
  16.             .Items.Add("Show all records")
  17.             .Items.Add("Zone Name")
  18.         End With
  19.         cmbGroup.SelectedIndex = 0
  20.     End Sub

  21.     ' / --------------------------------------------------------------------------------
  22.     Private Sub cmbGroup_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cmbGroup.SelectedIndexChanged
  23.         strSQL = _
  24.             " SELECT Countries.CountryPK, Countries.A2, Countries.Country, Countries.Capital, " & _
  25.             " Countries.Population, Zones.ZoneName " & _
  26.             " FROM Countries INNER JOIN Zones ON Countries.ZoneFK = Zones.ZonePK " & _
  27.             " ORDER BY Countries.A2 "
  28.         If Conn.State = ConnectionState.Closed Then Conn.Open()
  29.         '// Creates Data Adapter.
  30.         DA = New OleDbDataAdapter(strSQL, Conn)
  31.         '// Creates and fills Data Set.
  32.         DS = New DataSet
  33.         DA.Fill(DS)
  34.         Me.GGC.DataSource = DS.Tables(0)
  35.         '//
  36.         Select Case cmbGroup.SelectedIndex
  37.             '// Show all records.
  38.             Case 0
  39.                 '// Delete Grouping
  40.                 GGC.TableDescriptor.GroupedColumns.Remove("ZoneName")
  41.                 '// Show ZoneName Column
  42.                 GGC.TableDescriptor.VisibleColumns.Add("ZoneName")

  43.                 '// Make group.
  44.             Case 1
  45.                 '// Grouping with ZoneName
  46.                 GGC.TableDescriptor.GroupedColumns.Add("ZoneName")
  47.                 '// Hidden ZoneName
  48.                 GGC.TableDescriptor.VisibleColumns.Remove("ZoneName")
  49.         End Select
  50.         DA.Dispose()
  51.         DS.Dispose()
  52.         Conn.Close()
  53.         '//
  54.         Call InitGridGroup()
  55.     End Sub

  56.     ' / --------------------------------------------------------------------------------
  57.     Private Sub InitGridGroup()
  58.         '// Initialize Columns GridGroup
  59.         With Me.GGC.TableDescriptor
  60.             '// Hidden Primary Key Column
  61.             .VisibleColumns.Remove("CountryPK")
  62.             '/ Using Column Name
  63.             .Columns("A2").HeaderText = "A2"
  64.             .Columns("Country").HeaderText = "Country"
  65.             .Columns("Capital").HeaderText = "Capital"
  66.             '// Format Population
  67.             With .Columns("Population")
  68.                 .HeaderText = "Population"
  69.                 .Appearance.AnyRecordFieldCell.CellValueType = GetType(Double)
  70.                 .Appearance.AnyRecordFieldCell.Format = "N2"
  71.             End With
  72.             .Columns("ZoneName").HeaderText = "Zone Name"
  73.         End With
  74.         '// GridVerticalAlignment.Middle
  75.         For i As Byte = 0 To 5
  76.             With Me.GGC
  77.                 .TableDescriptor.Columns(i).Appearance.AnyRecordFieldCell.VerticalAlignment = GridVerticalAlignment.Middle
  78.                 .TableDescriptor.Columns(i).AllowGroupByColumn = False
  79.                 ' // Set Font any Columns.
  80.                 .TableDescriptor.Columns(i).Appearance.AnyRecordFieldCell.Font = New Syncfusion.Windows.Forms.Grid.GridFontInfo(New Font("Tahoma", 11.0F, FontStyle.Regular))
  81.             End With
  82.         Next

  83.         '// Initialize normal GridGrouping
  84.         With Me.GGC
  85.             '// Font Style Column Headers.
  86.             .Appearance.ColumnHeaderCell.Font = New Syncfusion.Windows.Forms.Grid.GridFontInfo(New Font("Tahoma", 12.0F, FontStyle.Bold))
  87.             '// Font Style Caption Cells.
  88.             .TableDescriptor.Appearance.GroupCaptionCell.Font = New GridFontInfo(New Font("Tahoma", 12.0F, FontStyle.Bold))
  89.             .Appearance.GroupCaptionCell.TextColor = Color.FromArgb(192, 64, 0)

  90.             '/ Allows GroupDropArea to be visible
  91.             .ShowGroupDropArea = False  ' Disable
  92.             '// Hidden Top Level of Grouping
  93.             .TopLevelGroupOptions.ShowCaption = False
  94.             '// Styles
  95.             .GridVisualStyles = Syncfusion.Windows.Forms.GridVisualStyles.SystemTheme
  96.             '/ Disables editing in GridGroupingControl
  97.             .ActivateCurrentCellBehavior = GridCellActivateAction.None
  98.             '// Metro Styles
  99.             .GridVisualStyles = Syncfusion.Windows.Forms.GridVisualStyles.Metro
  100.             '/ Disables editing in GridGroupingControl
  101.             .TableDescriptor.AllowNew = False
  102.             '// Autofit Columns
  103.             .AllowProportionalColumnSizing = False ' True

  104.             '// Row Height
  105.             .Table.DefaultRecordRowHeight = 28 '25
  106.             '//
  107.             .Table.DefaultCaptionRowHeight = 28 '25
  108.             .Table.DefaultColumnHeaderRowHeight = 36 '30    '// Columns Header

  109.             '// Selection
  110.             .TableOptions.ListBoxSelectionMode = SelectionMode.One
  111.             '/ Selection Back color
  112.             .TableOptions.SelectionBackColor = Color.Firebrick
  113.             '//
  114.             .Appearance.ColumnHeaderCell.TextColor = Color.DarkBlue

  115.             '/ Applies back color as LightCyan for alternative records in the Grid.
  116.             .Appearance.AlternateRecordFieldCell.BackColor = Color.LightCyan
  117.         End With

  118.         With Me.GGC
  119.             '/ Disable record preview row
  120.             .TableOptions.ShowRecordPreviewRow = False
  121.             '//
  122.             '/ Will enable the Group Header for the top most group.
  123.             .TopLevelGroupOptions.ShowGroupHeader = False ' True
  124.             '/ Will enable the Group Footer for the group.
  125.             .TopLevelGroupOptions.ShowGroupFooter = False 'True
  126.             '//
  127.             .TableOptions.GroupHeaderSectionHeight = 30
  128.             .TableOptions.GroupFooterSectionHeight = 30
  129.         End With
  130.     End Sub

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

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

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

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

โมดูลหากินในการเชื่อมต่อกับฐานข้อมูล ... modDataBase.vb
  1. Imports System.Data.OleDb
  2. Imports Microsoft.VisualBasic

  3. Module modDataBase
  4.     '// Declare variable one time but use many times.
  5.     Public Conn As OleDbConnection
  6.     Public Cmd As OleDbCommand
  7.     Public DS As DataSet
  8.     Public DR As OleDbDataReader
  9.     Public DA As OleDbDataAdapter
  10.     Public strSQL As String '// Major SQL
  11.     Public strStmt As String    '// Minor SQL

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

  16.     ' / --------------------------------------------------------------------
  17.     ' / Connect Database.
  18.     Sub ConnectDataBase()
  19.         strPathData = MyPath(Application.StartupPath) & "Data"
  20.         'strPathImages = MyPath(Application.StartupPath) & "Images"
  21.         Dim strConn As String = _
  22.             " Provider = Microsoft.ACE.OLEDB.12.0; " & _
  23.             " Data Source = " & strPathData & "Countries.accdb"
  24.         Try
  25.             Conn = New OleDbConnection(strConn)
  26.             '// Test Connection
  27.             Conn.Open()
  28.             'MsgBox("Connection Complete.")
  29.         Catch ex As Exception
  30.             MessageBox.Show(ex.Message, "ERROR")
  31.             End
  32.         End Try
  33.     End Sub

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

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

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

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

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

0

กระทู้

58

โพสต์

10

เครดิต

Member

Rank: 2

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

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

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

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

GMT+7, 2024-4-26 03:50 , Processed in 0.149744 second(s), 4 queries , File On.

Powered by Discuz! X3.4, Rev.62

Copyright © 2001-2020 Tencent Cloud.

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