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

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

[VB.NET] ตรวจสอบการป้อนค่าในแต่ละเซลล์ GridGroupingControl ของฟรีจากค่าย Syncfusion

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

311

กระทู้

502

โพสต์

6072

เครดิต

ผู้ดูแลระบบ

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

Rank: 9Rank: 9Rank: 9

เครดิต
6072



แอดมินได้ปล่อยโค้ด 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

มาดูโค้ดกันเถอะ ...
  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: Validating cell in 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.Windows.Forms.Grid.Grouping
  18. Imports Syncfusion.Grouping
  19. '
  20. Imports System.Data.OleDb
  21. Imports System.Collections.Specialized

  22. Public Class frmGridValidateCell

  23.     Private Sub frmGridValidateCell_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  24.         Conn = ConnectDataBase()
  25.         '//
  26.         strSQL = _
  27.             " SELECT tblSample.PrimaryKey, tblSample.ID, tblSample.NumberField, tblSample.DoubleField, " & _
  28.             " tblSample.DateField, tblGroup.GroupName " & _
  29.             " FROM tblSample INNER JOIN tblGroup ON tblSample.GroupFK = tblGroup.GroupPK " & _
  30.             " ORDER BY PrimaryKey "
  31.         '//
  32.         If Conn.State = ConnectionState.Closed Then Conn.Open()
  33.         '// Creates Data Adapter.
  34.         DA = New OleDbDataAdapter(strSQL, Conn)
  35.         ' Creates and fills Data Set.
  36.         DS = New DataSet
  37.         DA.Fill(DS)
  38.         Me.GGC.DataSource = DS.Tables(0)
  39.         DA.Dispose()
  40.         DS.Dispose()
  41.         Conn.Close()
  42.         '//
  43.         Call InitGridGroup()

  44.         '// Specify the required list to be displayed in the drop down.
  45.         Dim list As New StringCollection()
  46.         list.Add("Group A")
  47.         list.Add("Group B")
  48.         list.Add("Group C")
  49.         list.Add("Group D")
  50.         list.Add("Group E")
  51.         list.Add("Group F")
  52.         list.Add("Group G")
  53.         '// OR AddRange
  54.         'list.AddRange(New String() {"Group A", "Group B", "Group C", "Group D", "Group E", "Group F", "Group G"})
  55.         Me.GGC.TableDescriptor.Columns("GroupName").Appearance.AnyRecordFieldCell.ChoiceList = list
  56.     End Sub

  57.     ' / --------------------------------------------------------------------------------
  58.     Private Sub InitGridGroup()
  59.         '// Initialize normal GridGroup
  60.         With Me.GGC
  61.             '// Styles
  62.             .GridVisualStyles = Syncfusion.Windows.Forms.GridVisualStyles.SystemTheme

  63.             '// Enables editing in GridGroupingControl
  64.             .ActivateCurrentCellBehavior = GridCellActivateAction.ClickOnCell
  65.             .TableOptions.AllowSelection = Syncfusion.Windows.Forms.Grid.GridSelectionFlags.None
  66.             '.TableOptions.ListBoxSelectionMode = SelectionMode.One
  67.             '// Not allows GroupDropArea to be visible
  68.             .ShowGroupDropArea = False

  69.             '// Disable Add New
  70.             .TableDescriptor.AllowNew = False
  71.             .TableDescriptor.AllowEdit = True
  72.             .TableDescriptor.AllowRemove = True

  73.             '// Autofit Columns
  74.             .AllowProportionalColumnSizing = True
  75.             '// Row Height
  76.             .Table.DefaultRecordRowHeight = 25
  77.         End With

  78.         '// Initialize Columns GridGroup
  79.         With Me.GGC
  80.             '// Column 1 - Hidden Primary Key Column
  81.             .TableDescriptor.VisibleColumns.Remove("PrimaryKey")
  82.             'Using Column Name
  83.             .TableDescriptor.Columns("ID").HeaderText = "ID"
  84.             '// 2
  85.             .TableDescriptor.Columns("NumberField").HeaderText = "Number Value"
  86.             .TableDescriptor.Columns("NumberField").Appearance.AnyRecordFieldCell.CellType = GridCellTypeName.NumericUpDown
  87.             '// 3
  88.             .TableDescriptor.Columns("DoubleField").HeaderText = "Double Value"
  89.             '// 4
  90.             .TableDescriptor.Columns("DateField").HeaderText = "Date"
  91.             .TableDescriptor.Columns("DateField").Appearance.AnyRecordFieldCell.Format = "dd/MM/yyyy"
  92.             '// 5
  93.             .TableDescriptor.Columns("GroupName").HeaderText = "Group Name"
  94.             .TableDescriptor.Columns("GroupName").Appearance.AnyRecordFieldCell.CellType = GridCellTypeName.ComboBox
  95.             '// Lock/Unlock this column.
  96.             .TableDescriptor.Columns("GroupName").Appearance.AnyRecordFieldCell.ReadOnly = True 'False
  97.         End With
  98.         For iRow = 0 To Me.GGC.TableModel.ColCount - 2
  99.             '// Set the display position.
  100.             Me.GGC.TableDescriptor.Columns(iRow).Appearance.ColumnHeaderCell.HorizontalAlignment = GridHorizontalAlignment.Right
  101.             Me.GGC.TableDescriptor.Columns(iRow).Appearance.AnyCell.VerticalAlignment = GridVerticalAlignment.Middle
  102.         Next
  103.         '//
  104.     End Sub

  105.     ' / --------------------------------------------------------------------------------
  106.     '// Preventing Invalid Key Press on Cell
  107.     Private Sub GridGroupingControl1_TableControlCurrentCellKeyPress(ByVal sender As Object, ByVal e As Syncfusion.Windows.Forms.Grid.Grouping.GridTableControlKeyPressEventArgs)
  108.         Dim currentCell As GridCurrentCell = Me.GGC.TableControl.CurrentCell
  109.         Select Case currentCell.ColIndex
  110.             '// Force any keystrokes to be pressed.
  111.             Case 5
  112.                 e.Inner.Handled = True
  113.         End Select

  114.     End Sub

  115.     ' / --------------------------------------------------------------------------------
  116.     '// Preventing Invalid Key Press.
  117.     '// This event will be raised when the user presses the key in the current cell and before it is accepted.
  118.     '// Allows users to limit the keys that are accepted for the current cell.
  119.     Private Sub gridGroupingControl1_TableControlCurrentCellValidateString(ByVal sender As Object, ByVal e As GridTableControlCurrentCellValidateStringEventArgs)
  120.         Dim numericValue As Double
  121.         Dim dateValue As Date
  122.         '// Focus each cell
  123.         Dim currentCell As GridCurrentCell = Me.GGC.TableControl.CurrentCell
  124.         Select Case currentCell.ColIndex
  125.             '// Integer & Double value
  126.             Case 2, 3
  127.                 '/ Checks the entered text is numeric
  128.                 If Not Double.TryParse(e.Inner.Text, numericValue) Then
  129.                     e.Inner.Cancel = True
  130.                 End If
  131.                 '// Date value
  132.             Case 4
  133.                 If Not Date.TryParse(e.Inner.Text, Date.FromOADate(dateValue.ToOADate)) Then
  134.                     e.Inner.Cancel = True
  135.                 End If
  136.             Case Else
  137.                 e.Inner.Cancel = False
  138.         End Select
  139.     End Sub

  140.     Private Sub frmGridValidateCell_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
  141.         Me.Dispose()
  142.         Application.Exit()
  143.     End Sub

  144. 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. ' / Purpose: This module for connect MS Access.
  8. ' / Microsoft Visual Basic .NET (2010) & MS Access 2007+
  9. ' /
  10. ' / This is open source code under @Copyleft by Thongkorn Tubtimkrob.
  11. ' / You can modify and/or distribute without to inform the developer.
  12. ' / --------------------------------------------------------------------------------
  13. Imports System.Data.OleDb
  14. Imports Microsoft.VisualBasic

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

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

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

  35.         Conn = New OleDb.OleDbConnection(strConn)
  36.         ' Create Connection
  37.         Conn.ConnectionString = strConn
  38.         ' Return
  39.         Return Conn
  40.     End Function

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

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

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

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

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

0

กระทู้

51

โพสต์

233

เครดิต

Full Member

Rank: 3Rank: 3

เครดิต
233
โพสต์ 2018-9-14 11:21:45 | ดูโพสต์ทั้งหมด

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

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

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

GMT+7, 2024-5-5 16:31 , Processed in 0.349460 second(s), 4 queries , File On.

Powered by Discuz! X3.4, Rev.62

Copyright © 2001-2020 Tencent Cloud.

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