[VB.NET] ตรวจสอบการป้อนค่าในแต่ละเซลล์ GridGroupingControl ของฟรีจากค่าย Syncfusion
http://www.g2gnet.com/webboard/images/vbnet/ggcvalidate.pngแอดมินได้ปล่อยโค้ด VB.Net ในการใช้งานกับ GridGroupingControl ของฟรีจากค่าย Syncfusion ออกไปแล้ว 1 ภาค ซึ่งเป็นส่วนหลักๆในการทำงานของตารางกริดทุกๆตัว คือการค้นคืนข้อมูล (Retrieve Data) อ่านรายละเอียดปฐมบทการใช้งานตารางกริด GridGroupControl ... วันนี้แอดมินขอเสนอหน้านำเสนอ การแก้ไขข้อมูลในแต่ละแถวรายการ หรือที่เรียกว่า In Line Edit ซึ่งจะต้องตรวจสอบการป้อนค่าในแต่ละเซลล์ หรือ Validate Cells เรามักจะใช้ในกรณีของการ Unbound Data เป็นหลัก คือสามารถแก้ไขข้อมูลในแต่ละเซลล์นั้นๆได้ แต่ทว่าในแต่ละเซลล์ก็จะเกิดการป้อนข้อมูลที่แตกต่างกันไป เช่น ช่องนี้ป้อนได้เฉพาะตัวเลข 0-9 เท่านั้น หรืออาจจะใส่เลขที่มีจุดทศนิยม หรือป้อนค่าวันที่ หรือแสดงผลแบบ Drop Down ให้เราเลือก ... ที่ว่ามาทั้งหมดนั้นเหตุผลก็เพื่อ อำนวยความสะดวกให้แก่ผู้ใช้งาน และป้องกันการเกิด Human Error ...
อนึ่ง!!! โค้ดชุดนี้แอดมินได้เปลี่ยน Syncfusion จากเวอร์ชั่น 15.2400.0.40 มาเป็นเวอร์ชั่น 16.2400.0.41 แต่ไม่ว่าคุณจะใช้เวอร์ชั่นไหนก็ตามที คุณสามารถสร้างฟอร์มขึ้นมาใหม่ แล้วนำเอา GridGroupingControl มาวางแปะลงบนฟอร์ม พร้อมกับเปลี่ยนชื่อ (Name) ให้เป็น GGC แล้วตัดโค้ดในการตั้งค่าไปวางแปะ คุณก็จะสามารถรันโปรแกรมได้เลยทันที นี่คือ ข้อดีของการเขียนโค้ดแบบ Run Time ...
ดาวน์โหลด Syncfusion Community ได้ฟรีที่ Syncfusion Essential รุ่น Community
มาดูโค้ดกันเถอะ ...
#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: Validating cell in 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.Windows.Forms.Grid.Grouping
Imports Syncfusion.Grouping
'
Imports System.Data.OleDb
Imports System.Collections.Specialized
Public Class frmGridValidateCell
Private Sub frmGridValidateCell_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Conn = ConnectDataBase()
'//
strSQL = _
" SELECT tblSample.PrimaryKey, tblSample.ID, tblSample.NumberField, tblSample.DoubleField, " & _
" tblSample.DateField, tblGroup.GroupName " & _
" FROM tblSample INNER JOIN tblGroup ON tblSample.GroupFK = tblGroup.GroupPK " & _
" ORDER BY PrimaryKey "
'//
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)
DA.Dispose()
DS.Dispose()
Conn.Close()
'//
Call InitGridGroup()
'// Specify the required list to be displayed in the drop down.
Dim list As New StringCollection()
list.Add("Group A")
list.Add("Group B")
list.Add("Group C")
list.Add("Group D")
list.Add("Group E")
list.Add("Group F")
list.Add("Group G")
'// OR AddRange
'list.AddRange(New String() {"Group A", "Group B", "Group C", "Group D", "Group E", "Group F", "Group G"})
Me.GGC.TableDescriptor.Columns("GroupName").Appearance.AnyRecordFieldCell.ChoiceList = list
End Sub
' / --------------------------------------------------------------------------------
Private Sub InitGridGroup()
'// Initialize normal GridGroup
With Me.GGC
'// Styles
.GridVisualStyles = Syncfusion.Windows.Forms.GridVisualStyles.SystemTheme
'// Enables editing in GridGroupingControl
.ActivateCurrentCellBehavior = GridCellActivateAction.ClickOnCell
.TableOptions.AllowSelection = Syncfusion.Windows.Forms.Grid.GridSelectionFlags.None
'.TableOptions.ListBoxSelectionMode = SelectionMode.One
'// Not allows GroupDropArea to be visible
.ShowGroupDropArea = False
'// Disable Add New
.TableDescriptor.AllowNew = False
.TableDescriptor.AllowEdit = True
.TableDescriptor.AllowRemove = True
'// Autofit Columns
.AllowProportionalColumnSizing = True
'// Row Height
.Table.DefaultRecordRowHeight = 25
End With
'// Initialize Columns GridGroup
With Me.GGC
'// Column 1 - Hidden Primary Key Column
.TableDescriptor.VisibleColumns.Remove("PrimaryKey")
'Using Column Name
.TableDescriptor.Columns("ID").HeaderText = "ID"
'// 2
.TableDescriptor.Columns("NumberField").HeaderText = "Number Value"
.TableDescriptor.Columns("NumberField").Appearance.AnyRecordFieldCell.CellType = GridCellTypeName.NumericUpDown
'// 3
.TableDescriptor.Columns("DoubleField").HeaderText = "Double Value"
'// 4
.TableDescriptor.Columns("DateField").HeaderText = "Date"
.TableDescriptor.Columns("DateField").Appearance.AnyRecordFieldCell.Format = "dd/MM/yyyy"
'// 5
.TableDescriptor.Columns("GroupName").HeaderText = "Group Name"
.TableDescriptor.Columns("GroupName").Appearance.AnyRecordFieldCell.CellType = GridCellTypeName.ComboBox
'// Lock/Unlock this column.
.TableDescriptor.Columns("GroupName").Appearance.AnyRecordFieldCell.ReadOnly = True 'False
End With
For iRow = 0 To Me.GGC.TableModel.ColCount - 2
'// Set the display position.
Me.GGC.TableDescriptor.Columns(iRow).Appearance.ColumnHeaderCell.HorizontalAlignment = GridHorizontalAlignment.Right
Me.GGC.TableDescriptor.Columns(iRow).Appearance.AnyCell.VerticalAlignment = GridVerticalAlignment.Middle
Next
'//
End Sub
' / --------------------------------------------------------------------------------
'// Preventing Invalid Key Press on Cell
Private Sub GridGroupingControl1_TableControlCurrentCellKeyPress(ByVal sender As Object, ByVal e As Syncfusion.Windows.Forms.Grid.Grouping.GridTableControlKeyPressEventArgs)
Dim currentCell As GridCurrentCell = Me.GGC.TableControl.CurrentCell
Select Case currentCell.ColIndex
'// Force any keystrokes to be pressed.
Case 5
e.Inner.Handled = True
End Select
End Sub
' / --------------------------------------------------------------------------------
'// Preventing Invalid Key Press.
'// This event will be raised when the user presses the key in the current cell and before it is accepted.
'// Allows users to limit the keys that are accepted for the current cell.
Private Sub gridGroupingControl1_TableControlCurrentCellValidateString(ByVal sender As Object, ByVal e As GridTableControlCurrentCellValidateStringEventArgs)
Dim numericValue As Double
Dim dateValue As Date
'// Focus each cell
Dim currentCell As GridCurrentCell = Me.GGC.TableControl.CurrentCell
Select Case currentCell.ColIndex
'// Integer & Double value
Case 2, 3
'/ Checks the entered text is numeric
If Not Double.TryParse(e.Inner.Text, numericValue) Then
e.Inner.Cancel = True
End If
'// Date value
Case 4
If Not Date.TryParse(e.Inner.Text, Date.FromOADate(dateValue.ToOADate)) Then
e.Inner.Cancel = True
End If
Case Else
e.Inner.Cancel = False
End Select
End Sub
Private Sub frmGridValidateCell_FormClosed(sender As Object, 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)
' / Purpose: This module for connect MS Access.
' / 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.
' / --------------------------------------------------------------------------------
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)
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;"
strConn += _
"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]