[VB.NET] จัดกลุ่มด้วยวิธีการเขียนโค้ดให้กับ GridGrouping ของฟรีจาก Syncfusion
http://www.g2gnet.com/webboard/images/vbnet/SyncfusionGridGroup.pngแอดมินได้แจกโค้ดวิธีการใช้งาน GridGrouping Control ของฟรีจากค่าย Syncfusion ไปหลายรอบแล้ว คราวนี้มาดู วิธีการจัดกลุ่มให้กับ Control ตัวนี้ โดยการเขียนด้วยโค้ดเข้าไปควบคุมแทนการใช้ Drag & Drop ซึ่งแม้ว่าวิธีนี้มันดูง่าย แต่กลับจะทำให้ผู้ใช้งานสับสนหรือไม่ค่อยกล้าใช้งาน แน่นอนล่ะว่าต้องเพิ่มความยากในการเขียนโปรแกรมขึ้นมาอยู่แล้วครับ สำหรับ Control ของค่ายนี้ค่อนข้างจะมีโค้ดของตารางกริด ที่แตกต่างไปจากค่ายอื่นๆเขาเยอะมาก ทำให้เราเรียนรู้ได้ช้า และจดจำได้ยาก ดังนั้นควรจะศึกษาจากโค้ดแบบ Run Time ทดแทนการจดจำด้วยวิธีการ Design Time ครับผม ...
Add References ... ต้องใช้ Net FrameWork 4.0 ชุดเต็มด้วยน่ะครับ
http://www.g2gnet.com/webboard/images/vbnet/SyncfusionGridGroupRef.png
มาดูโค้ดต้นฉบับกันเถอะ ...
'// https://help.syncfusion.com/windowsforms/classic/gridgroupingcontrol/overview
Imports Syncfusion.Windows.Forms
Imports Syncfusion.Windows.Forms.Grid
Imports Syncfusion.Grouping
'// DataBase
Imports System.Data.OleDb
Public Class frmGroupGGC
Private Sub frmGGCSample_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
Me.Dispose()
Application.Exit()
End Sub
Private Sub frmGGCSample_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call ConnectDataBase()
'//
With cmbGroup
.Items.Add("Show all records")
.Items.Add("Zone Name")
End With
cmbGroup.SelectedIndex = 0
End Sub
' / --------------------------------------------------------------------------------
Private Sub cmbGroup_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cmbGroup.SelectedIndexChanged
strSQL = _
" SELECT Countries.CountryPK, Countries.A2, Countries.Country, Countries.Capital, " & _
" Countries.Population, Zones.ZoneName " & _
" FROM Countries INNER JOIN Zones ON Countries.ZoneFK = Zones.ZonePK " & _
" ORDER BY Countries.A2 "
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)
'//
Select Case cmbGroup.SelectedIndex
'// Show all records.
Case 0
'// Delete Grouping
GGC.TableDescriptor.GroupedColumns.Remove("ZoneName")
'// Show ZoneName Column
GGC.TableDescriptor.VisibleColumns.Add("ZoneName")
'// Make group.
Case 1
'// Grouping with ZoneName
GGC.TableDescriptor.GroupedColumns.Add("ZoneName")
'// Hidden ZoneName
GGC.TableDescriptor.VisibleColumns.Remove("ZoneName")
End Select
DA.Dispose()
DS.Dispose()
Conn.Close()
'//
Call InitGridGroup()
End Sub
' / --------------------------------------------------------------------------------
Private Sub InitGridGroup()
'// Initialize Columns GridGroup
With Me.GGC.TableDescriptor
'// Hidden Primary Key Column
.VisibleColumns.Remove("CountryPK")
'/ Using Column Name
.Columns("A2").HeaderText = "A2"
.Columns("Country").HeaderText = "Country"
.Columns("Capital").HeaderText = "Capital"
'// Format Population
With .Columns("Population")
.HeaderText = "Population"
.Appearance.AnyRecordFieldCell.CellValueType = GetType(Double)
.Appearance.AnyRecordFieldCell.Format = "N2"
End With
.Columns("ZoneName").HeaderText = "Zone Name"
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
' // Set Font any Columns.
.TableDescriptor.Columns(i).Appearance.AnyRecordFieldCell.Font = New Syncfusion.Windows.Forms.Grid.GridFontInfo(New Font("Tahoma", 11.0F, FontStyle.Regular))
End With
Next
'// Initialize normal GridGrouping
With Me.GGC
'// Font Style Column Headers.
.Appearance.ColumnHeaderCell.Font = New Syncfusion.Windows.Forms.Grid.GridFontInfo(New Font("Tahoma", 12.0F, FontStyle.Bold))
'// Font Style Caption Cells.
.TableDescriptor.Appearance.GroupCaptionCell.Font = New GridFontInfo(New Font("Tahoma", 12.0F, FontStyle.Bold))
.Appearance.GroupCaptionCell.TextColor = Color.FromArgb(192, 64, 0)
'/ Allows GroupDropArea to be visible
.ShowGroupDropArea = False' Disable
'// Hidden Top Level of Grouping
.TopLevelGroupOptions.ShowCaption = False
'// Styles
.GridVisualStyles = Syncfusion.Windows.Forms.GridVisualStyles.SystemTheme
'/ Disables editing in GridGroupingControl
.ActivateCurrentCellBehavior = GridCellActivateAction.None
'// Metro Styles
.GridVisualStyles = Syncfusion.Windows.Forms.GridVisualStyles.Metro
'/ Disables editing in GridGroupingControl
.TableDescriptor.AllowNew = False
'// Autofit Columns
.AllowProportionalColumnSizing = False ' True
'// Row Height
.Table.DefaultRecordRowHeight = 28 '25
'//
.Table.DefaultCaptionRowHeight = 28 '25
.Table.DefaultColumnHeaderRowHeight = 36 '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
End With
With Me.GGC
'/ 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 PrimaryKey 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("CountryPK").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
End Class
โมดูลหากินในการเชื่อมต่อกับฐานข้อมูล ... modDataBase.vb
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)
'// Images Path
Public strPathImages As String = MyPath(Application.StartupPath)
' / --------------------------------------------------------------------
' / Connect Database.
Sub ConnectDataBase()
strPathData = MyPath(Application.StartupPath) & "Data\"
'strPathImages = MyPath(Application.StartupPath) & "Images\"
Dim strConn As String = _
" Provider = Microsoft.ACE.OLEDB.12.0; " & _
" Data Source = " & strPathData & "Countries.accdb"
Try
Conn = New OleDbConnection(strConn)
'// Test Connection
Conn.Open()
'MsgBox("Connection Complete.")
Catch ex As Exception
MessageBox.Show(ex.Message, "ERROR")
End
End Try
End Sub
' / --------------------------------------------------------------------------------
' / 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 Microsoft.VisualBasic.Right(MyPath, 1) <> Chr(92) Then MyPath = MyPath & Chr(92)
End Function
End Module
ดาวน์โหลดโค้ดต้นฉบับ VB.NET (2010) ได้ที่นี่ ...
ขอบคุณครับ
หน้า:
[1]