| 
 | 
 
Krypton Toolkit สำหรับ .NET Winforms ก็คือ Component ที่คล้ายๆกับ Control มาตรฐานของไมโครซอฟท์ เช่น Label TextBox หรือ DataGridView Control แต่จะมีรูปร่างหน้าตาที่ดูดีสวยงามกว่า และยังมี Control พิเศษตัวอื่นๆเสริมเข้ามาอีก ในปัจจุบันเราสามารถนำมาใช้งานได้ฟรีทั้งแบบส่วนบุคคล หรือในทางธุรกิจได้ ... 
 
โค้ดตัวอย่างของจริงนี้จะนำเสนอการควบคุม DataGridView Control ของตัว Krypton Toolkit ซึ่งโค้ดหลักๆจะนำมาจากการควบคุม DataGridView ของไมโครซอฟท์ แต่จะแก้ไขโค้ดบางส่วนจากของเดิมในเรื่องของการทำ Validate Data หรือการเช็คความถูกต้องของข้อมูล ก่อนที่จะทำการบันทึกข้อมูลลงไปในฐานข้อมูล เช่น การป้อนค่าเลขจำนวนเต็ม หรือจำนวนเลขทศนิยม เป็นต้น ซึ่งเราจะสามารถแก้ไขข้อมูลในแต่ละเซลล์ได้เอง ในลักษณะที่เรียกกันว่า In Line Edit ทำให้อำนวยความสะดวกต่อผู้ใช้งานได้ค่อนข้างดี และในโปรเจคนี้จะใช้การเขียนด้วยโค้ดขึ้นมาเกือบทั้งหมด เราจะเห็นผลลัพธ์ของการทำงานก็ต่อเมื่อสั่งรันให้โปรแกรมทำงาน (Run Time) ซึ่งจะมีประสิทธิภาพมากกว่าการใช้ Design Time ...  
 
คลิปวิดีโอการสร้างกลุ่ม Control ของฟรีจาก Krypton Toolkit แบบ Manual ลงใน ToolBox และการทำ Skin (Palette)  
 
  
 
  
 
  
 
  
 
  
 
มาดูโค้ดฉบับเต็มกันเถอะ ... .Net Framework 4.0 - '// Last updated 18/9/2564
 
 - '// for .Net Framework 3.5+
 
 - '// https://www.nuget.org/packages/Krypton.Toolkit/5.550.2108.1
 
 - '// 
 
 - '// for .Net Framework 4.6.2+ (80.23.11.321)
 
 - '// https://www.nuget.org/packages/Krypton.Toolkit
 
  
- Imports Krypton.Toolkit
 
  
- Public Class frmKryptonDataGridView
 
  
-     Private Sub frmKryptonDataGridView_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
 
 -         '// The form must be set to Press KeyPreview = True.
 
 -         Select Case e.KeyCode
 
 -             Case Keys.F7
 
 -                 Call AddRow()
 
 -             Case Keys.F8
 
 -                 Call DeleteRow()
 
 -         End Select
 
 -     End Sub
 
  
-     ' / --------------------------------------------------------------------------------------------
 
 -     ' / S T A R T ... H E R E
 
 -     ' / --------------------------------------------------------------------------------------------
 
 -     Private Sub frmKryptonDataGridView_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
 
 -         Me.KeyPreview = True
 
 -         Call InitializeGrid()
 
 -         Call AddRow()
 
 -         '// KryptonToolkit Palette.
 
 -         With cmbPalette
 
 -             .Items.Add("Office2003")
 
 -             .Items.Add("Office2007Blue")
 
 -             .Items.Add("Office2007Silver")
 
 -             .Items.Add("Office2007Black")
 
 -             .Items.Add("Office2010Blue")
 
 -             .Items.Add("Office2010Silver")
 
 -             .Items.Add("Office2010Black")
 
 -         End With
 
 -         cmbPalette.SelectedIndex = 4
 
 -         '// Set to KryptonDataGridView By changing the Palette according to the main form.
 
 -         Me.PaletteMode = Krypton.Toolkit.PaletteMode.Global
 
 -         '// If you want to specify a specific display, you can select PaletteMode.
 
 -         Me.dgvData.PaletteMode = Krypton.Toolkit.PaletteMode.Global
 
 -         'Me.dgvData.PaletteMode = Krypton.Toolkit.PaletteMode.Office2007Black
 
 -     End Sub
 
  
-     ' / --------------------------------------------------------------------------------------------
 
 -     ' / Change Palette Mode.
 
 -     ' / --------------------------------------------------------------------------------------------
 
 -     Private Sub cmbPalette_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cmbPalette.SelectedIndexChanged
 
 -         Dim manager As New KryptonManager()
 
 -         Select Case cmbPalette.SelectedIndex
 
 -             Case 0
 
 -                 'KryptonManager.GlobalPaletteMode = PaletteModeManager.ProfessionalOffice2003
 
 -                 manager.GlobalPaletteMode = PaletteModeManager.ProfessionalOffice2003
 
 -             Case 1
 
 -                 manager.GlobalPaletteMode = PaletteModeManager.Office2007Blue
 
 -             Case 2
 
 -                 manager.GlobalPaletteMode = PaletteModeManager.Office2007Silver
 
 -             Case 3
 
 -                 manager.GlobalPaletteMode = PaletteModeManager.Office2007Black
 
 -             Case 4
 
 -                 manager.GlobalPaletteMode = PaletteModeManager.Office2010Blue
 
 -             Case 5
 
 -                 manager.GlobalPaletteMode = PaletteModeManager.Office2010Silver
 
 -             Case 6
 
 -                 manager.GlobalPaletteMode = PaletteModeManager.Office2010Black
 
 -         End Select
 
 -     End Sub
 
  
-     ' / --------------------------------------------------------------------------------------------
 
 -     ' / Initialized DataGridView @Run Time.
 
 -     ' / --------------------------------------------------------------------------------------------
 
 -     Private Sub InitializeGrid()
 
 -         With dgvData
 
 -             .RowHeadersVisible = False
 
 -             .AllowUserToAddRows = False
 
 -             .AllowUserToDeleteRows = False
 
 -             .AllowUserToResizeRows = False
 
 -             .MultiSelect = False
 
 -             '// Need to modify each cell.
 
 -             .SelectionMode = DataGridViewSelectionMode.CellSelect
 
 -             .ReadOnly = False
 
 -             '// Font for RowTemplate.
 
 -             .RowTemplate.DefaultCellStyle.Font = New Font("Tahoma", 11, FontStyle.Regular)
 
 -             .RowTemplate.MinimumHeight = 32
 
 -             '// Header.
 
 -             .ColumnHeadersDefaultCellStyle.Font = New Font("Tahoma", 11, FontStyle.Bold)
 
 -             '// Show alternating colors in even and odd rows.
 
 -             .AlternatingRowsDefaultCellStyle.BackColor = Color.AliceBlue
 
 -             ' Automatically set the width.
 
 -             .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
 
 -             '// Set ColumnHeadersHeightSizeMode before adjusting row heights.
 
 -             'dgvData.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
 
 -             dgvData.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing
 
 -             dgvData.ColumnHeadersHeight = 36
 
 -             '// GridStyles
 
 -             '.GridStyles.Style = DataGridViewStyle.List
 
 -             '.GridStyles.Style = DataGridViewStyle.Mixed
 
 -             .GridStyles.Style = DataGridViewStyle.Sheet
 
 -         End With
 
  
-         '// Create columns dynamically.
 
 -         Dim colString As New KryptonDataGridViewTextBoxColumn() '// Index 0
 
 -         dgvData.Columns.Add(colString)
 
 -         With dgvData.Columns(0) '// OR colString
 
 -             .Name = "colString"
 
 -             .HeaderText = "String"
 
 -         End With
 
 -         '//
 
 -         Dim colInteger As New KryptonDataGridViewTextBoxColumn()   '// Index 1
 
 -         dgvData.Columns.Add(colInteger)
 
 -         With colInteger
 
 -             .Name = "colInteger"
 
 -             .HeaderText = "Integer"
 
 -             .DefaultCellStyle.Format = "0"
 
 -         End With
 
 -         '//
 
 -         Dim colDouble As New KryptonDataGridViewTextBoxColumn()   '// Index 2
 
 -         dgvData.Columns.Add(colDouble)
 
 -         With colDouble
 
 -             .Name = "colDouble"
 
 -             .HeaderText = "Double"
 
 -             .DefaultCellStyle.Format = "0.00"
 
 -         End With
 
 -         '//
 
 -         Dim colCombo As New KryptonDataGridViewComboBoxColumn()   '// Index 3
 
 -         dgvData.Columns.Add(colCombo)
 
 -         With colCombo
 
 -             .Name = "colCombo"
 
 -             .HeaderText = "ComboBox"
 
 -             .DropDownStyle = ComboBoxStyle.DropDownList
 
 -             .ReadOnly = False
 
 -             .DisplayMember = "Name"
 
 -             .DataSource = GetDataTable()
 
 -         End With
 
 -         '//
 
 -         '// Create a KryptonDataGridViewDateTimePickerColumn.
 
 -         Dim dateTimePickerColumn As New KryptonDataGridViewDateTimePickerColumn()  '// Index 4
 
 -         With dateTimePickerColumn
 
 -             .HeaderText = "Date"
 
 -             .Name = "colDate"
 
 -             '// Set the format of the DateTimePicker.
 
 -             .Format = DateTimePickerFormat.Short
 
 -         End With
 
 -         '// Add the column to the KryptonDataGridViewCheckBoxColumn.
 
 -         dgvData.Columns.Add(dateTimePickerColumn)
 
 -         '//
 
 -         Dim colCheckBox As New KryptonDataGridViewCheckBoxColumn()    '// Index 5
 
 -         dgvData.Columns.Add(colCheckBox)
 
 -         With colCheckBox
 
 -             .Name = "colCheckBox"
 
 -             .HeaderText = "CheckBox"
 
 -         End With
 
 -         '//
 
 -         Dim colUpDown As New KryptonDataGridViewNumericUpDownColumn()  '// Index 6
 
 -         dgvData.Columns.Add(colUpDown)
 
 -         With colUpDown
 
 -             .Name = "colUpDown"
 
 -             .HeaderText = "UpDown"
 
 -             .Maximum = 100
 
 -             .Minimum = 0
 
 -             .Increment = 1
 
 -         End With
 
 -         '// Add 8th column (Index = 7), It's Button.
 
 -         Dim btnAddRow As New KryptonDataGridViewButtonColumn()    '// Index 7
 
 -         dgvData.Columns.Add(btnAddRow)
 
 -         With btnAddRow
 
 -             .HeaderText = "Add-F7"
 
 -             .Text = "Add"
 
 -             .Name = "btnAddRow"
 
 -             .UseColumnTextForButtonValue = True
 
 -             .Width = 80
 
 -         End With
 
 -         '// Add 9th column (Index = 8), It's Button.
 
 -         Dim btnRemoveRow As New KryptonDataGridViewButtonColumn()     '// Index 8
 
 -         dgvData.Columns.Add(btnRemoveRow)
 
 -         With btnRemoveRow
 
 -             .HeaderText = "Del-F8"
 
 -             .Text = "Delete"
 
 -             .Name = "btnRemoveRow"
 
 -             .UseColumnTextForButtonValue = True
 
 -             .Width = 80
 
 -         End With
 
 -         '// Alignment header and cell any columns.
 
 -         For iCol As Byte = 1 To 8
 
 -             If iCol = 1 Or iCol = 2 Then
 
 -                 dgvData.Columns(iCol).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
 
 -                 dgvData.Columns(iCol).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
 
 -             ElseIf iCol = 3 Or iCol = 4 Or iCol = 6 Then
 
 -                 dgvData.Columns(iCol).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleLeft
 
 -                 dgvData.Columns(iCol).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
 
 -             Else
 
 -                 dgvData.Columns(iCol).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
 
 -                 dgvData.Columns(iCol).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
 
 -             End If
 
 -         Next
 
 -     End Sub
 
  
-     ' / --------------------------------------------------------------------------------------------
 
 -     ' / Sample DataTable for ComboBox.
 
 -     ' / --------------------------------------------------------------------------------------------
 
 -     Function GetDataTable() As DataTable
 
 -         Dim DT As DataTable = New DataTable
 
 -         '// Add new columns.
 
 -         With DT.Columns
 
 -             .Add("PK", GetType(Integer))
 
 -             .Add("Name", GetType(String))
 
 -         End With
 
 -         '// Add sample data rows.
 
 -         With DT.Rows
 
 -             .Add(1, "M100")
 
 -             .Add(2, "M150")
 
 -             .Add(3, "M16")
 
 -             .Add(4, "M4")
 
 -             .Add(5, "AK47")
 
 -             .Add(6, "RPG")
 
 -         End With
 
 -         Return DT
 
 -     End Function
 
  
-     ' / --------------------------------------------------------------------------------------------
 
 -     ' / SAMPLE DATA FOR ADD TO ROW.
 
 -     ' / --------------------------------------------------------------------------------------------
 
 -     Private Sub AddRow()
 
 -         Dim RandGen As New Random()
 
 -         '// Random Double Value.
 
 -         Dim minValue As Double = 0.0 ' Minimum value for the range
 
 -         Dim maxValue As Double = 9999.0 ' Maximum value for the range
 
 -         '// Generate a random double between minValue and maxValue.
 
 -         Dim randomDouble As Double = RandGen.NextDouble() * (maxValue - minValue) + minValue
 
 -         '// ComboBox.
 
 -         'Dim ls As New List(Of String) ({"M100", "M150", "M16", "M4", "AK47", "RPG"})
 
 -         '// Get sample data from DataTable.
 
 -         Dim dt As DataTable = GetDataTable()
 
 -         Dim ls As New List(Of String)()
 
 -         For Each row As DataRow In dt.Rows
 
 -             ls.Add(row("Name").ToString())
 
 -         Next
 
 -         '// Random value from List.
 
 -         Dim RandArray As String = ls(RandGen.Next(0, ls.Count))
 
 -         '// Sample data for each columns.
 
 -         '// String, Integer, Double, ComboBox, Date, CheckBox, UpDown
 
 -         dgvData.Rows.Add( _
 
 -             "Product " & RandGen.Next(1, 999), _
 
 -             RandGen.Next(1, 999), _
 
 -             randomDouble, _
 
 -             RandArray, _
 
 -             DateTime.Today.AddDays(-RandGen.Next(365)), _
 
 -             IIf(RandGen.Next(0, 2) > 0, "True", "False"), _
 
 -             RandGen.Next(1, 100) _
 
 -             )
 
 -     End Sub
 
  
-     ' / --------------------------------------------------------------------------------------------
 
 -     ' / Sub program to delete selected rows.
 
 -     ' / --------------------------------------------------------------------------------------------
 
 -     Private Sub DeleteRow()
 
 -         If dgvData.RowCount = 0 Then Return
 
 -         '// Delete selected row items.
 
 -         dgvData.Rows.Remove(dgvData.CurrentRow)
 
 -     End Sub
 
  
-     ' / --------------------------------------------------------------------------------------------
 
 -     ' / Click mouse event in grid cell.
 
 -     ' / --------------------------------------------------------------------------------------------
 
 -     Private Sub dgvData_CellClick(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvData.CellClick
 
 -         Select Case e.ColumnIndex
 
 -             '// Add Row.
 
 -             Case 7
 
 -                 Call AddRow()
 
  
-                 '// Delete Row.
 
 -             Case 8
 
 -                 'MsgBox(("Row : " + e.RowIndex.ToString & "  Col : ") + e.ColumnIndex.ToString)
 
 -                 Call DeleteRow()
 
 -         End Select
 
 -     End Sub
 
  
-     ' / --------------------------------------------------------------------------------------------
 
 -     ' / Enter press event in grid cell.
 
 -     ' / --------------------------------------------------------------------------------------------
 
 -     Private Sub dgvData_CellEndEdit(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvData.CellEndEdit
 
 -         '/ change occurred in the 1st or 2nd index digit.
 
 -         Select Case e.ColumnIndex
 
 -             Case 1  '/ Integer Value.
 
 -                 '/ Index = 1 (Integer)
 
 -                 '/ To trap errors in the case of having a Null Value, enter the value 0 instead.
 
 -                 If IsNothing(dgvData.Rows(e.RowIndex).Cells(1).Value) Then dgvData.Rows(e.RowIndex).Cells(1).Value = "0"
 
 -                 '/ Handle the CellFormatting event to format numeric values.
 
 -                 AddHandler dgvData.CellFormatting, AddressOf KryptonDataGridView_CellFormatting
 
  
-             Case 2 '/ Double Value.
 
 -                 '/ Index = 2 (Double)
 
 -                 '/ If Null Value
 
 -                 If IsNothing(dgvData.Rows(e.RowIndex).Cells(2).Value) Then dgvData.Rows(e.RowIndex).Cells(2).Value = "0.00"
 
 -                 '/ Handle the CellFormatting event to format numeric values.
 
 -                 AddHandler dgvData.CellFormatting, AddressOf KryptonDataGridView_CellFormatting
 
 -         End Select
 
 -     End Sub
 
  
-     ' / --------------------------------------------------------------------------------------------
 
 -     ' / When you start pressing the keys in digits (Index) 1 and 2.
 
 -     ' / --------------------------------------------------------------------------------------------
 
 -     Private Sub dgvData_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles dgvData.EditingControlShowing
 
 -         '// Or use the index columns of DataGridView.
 
 -         'Select Case dgvData.Columns(dgvData.CurrentCell.ColumnIndex).Index
 
 -         '    Case 1
 
 -         '
 
 -         'End Select
 
  
-         '// Don't forget to specify the Column Name earlier as well (InitializeGrid sub program).
 
 -         Select Case dgvData.Columns(dgvData.CurrentCell.ColumnIndex).Name
 
 -             ' / Can use both Colume Index or Field Name
 
 -             Case "colInteger", "colDouble"
 
 -                 If TypeOf e.Control Is Krypton.Toolkit.KryptonDataGridViewTextBoxEditingControl Then
 
 -                     Dim editingControl As Krypton.Toolkit.KryptonDataGridViewTextBoxEditingControl = TryCast(e.Control, Krypton.Toolkit.KryptonDataGridViewTextBoxEditingControl)
 
 -                     '// Event Handler for intercepts keystrokes.
 
 -                     AddHandler editingControl.KeyPress, AddressOf KryptonDataGridViewKeyPress
 
 -                 End If
 
 -         End Select
 
 -     End Sub
 
  
-     ' / --------------------------------------------------------------------------------------------
 
 -     ' / Intercepts keystrokes only with numeric and point (.) only one.
 
 -     ' / --------------------------------------------------------------------------------------------
 
 -     Private Sub KryptonDataGridViewKeyPress(sender As Object, e As KeyPressEventArgs)
 
 -         Select Case dgvData.CurrentCell.ColumnIndex
 
 -             Case 1, 6   '// Integer
 
 -                 '// Allow numeric digits (0-9) in the TextBox.
 
 -                 If Not Char.IsControl(e.KeyChar) AndAlso Not Char.IsDigit(e.KeyChar) Then e.Handled = True
 
  
-             Case 2  '// Double
 
 -                 '// Allow numeric digits (0-9) and the decimal point (.) only one in the TextBox.
 
 -                 If Not Char.IsControl(e.KeyChar) AndAlso Not Char.IsDigit(e.KeyChar) AndAlso e.KeyChar <> "." Then e.Handled = True
 
 -                 '// Check for an existing decimal point in the cell.
 
 -                 Dim tb As KryptonDataGridViewTextBoxEditingControl = DirectCast(sender, KryptonDataGridViewTextBoxEditingControl)
 
 -                 If e.KeyChar = "." AndAlso tb.Text.Contains(".") Then e.Handled = True
 
 -         End Select
 
 -     End Sub
 
  
-     ' / --------------------------------------------------------------------------------------------
 
 -     ' / Event Handler from dgvData_CellEndEdit to format numbers.
 
 -     ' / --------------------------------------------------------------------------------------------
 
 -     Private Sub KryptonDataGridView_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs)
 
 -         If e.ColumnIndex = dgvData.Columns("colInteger").Index Then
 
 -             '// Format as an integer.
 
 -             If Not e.Value Is Nothing AndAlso IsNumeric(e.Value) Then e.Value = CInt(e.Value)
 
 -         ElseIf e.ColumnIndex = dgvData.Columns("colDouble").Index Then
 
 -             '// Format as a double with two decimal places.
 
 -             If Not e.Value Is Nothing AndAlso IsNumeric(e.Value) Then e.Value = CDbl(e.Value).ToString("0.00")
 
 -         End If
 
 -     End Sub
 
 - End Class
 
  คัดลอกไปที่คลิปบอร์ด 
ดาวน์โหลดโค้ดฉบับเต็ม VB.NET (2022) + .Net Framework 4.6.2+ ได้ที่นี่ ... 
 
ดาวน์โหลดโค้ดฉบับเต็ม VB.NET (2010) + .Net Framework 4.0 ได้ที่นี่ ... 
 
 |   
ขออภัย! โพสต์นี้มีไฟล์แนบหรือรูปภาพที่ไม่ได้รับอนุญาตให้คุณเข้าถึง
คุณจำเป็นต้อง ลงชื่อเข้าใช้ เพื่อดาวน์โหลดหรือดูไฟล์แนบนี้ คุณยังไม่มีบัญชีใช่ไหม? ลงทะเบียน  
 
x
 
 
 
 
 |