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

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

[VB.NET] Krypton Toolkit DataGridView for Visual Basic .NET (2010/2022)

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

311

กระทู้

502

โพสต์

6068

เครดิต

ผู้ดูแลระบบ

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

Rank: 9Rank: 9Rank: 9

เครดิต
6068

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
  1. '// Last updated 18/9/2564
  2. '// for .Net Framework 3.5+
  3. '// https://www.nuget.org/packages/Krypton.Toolkit/5.550.2108.1
  4. '//
  5. '// for .Net Framework 4.6.2+ (80.23.11.321)
  6. '// https://www.nuget.org/packages/Krypton.Toolkit

  7. Imports Krypton.Toolkit

  8. Public Class frmKryptonDataGridView

  9.     Private Sub frmKryptonDataGridView_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
  10.         '// The form must be set to Press KeyPreview = True.
  11.         Select Case e.KeyCode
  12.             Case Keys.F7
  13.                 Call AddRow()
  14.             Case Keys.F8
  15.                 Call DeleteRow()
  16.         End Select
  17.     End Sub

  18.     ' / --------------------------------------------------------------------------------------------
  19.     ' / S T A R T ... H E R E
  20.     ' / --------------------------------------------------------------------------------------------
  21.     Private Sub frmKryptonDataGridView_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
  22.         Me.KeyPreview = True
  23.         Call InitializeGrid()
  24.         Call AddRow()
  25.         '// KryptonToolkit Palette.
  26.         With cmbPalette
  27.             .Items.Add("Office2003")
  28.             .Items.Add("Office2007Blue")
  29.             .Items.Add("Office2007Silver")
  30.             .Items.Add("Office2007Black")
  31.             .Items.Add("Office2010Blue")
  32.             .Items.Add("Office2010Silver")
  33.             .Items.Add("Office2010Black")
  34.         End With
  35.         cmbPalette.SelectedIndex = 4
  36.         '// Set to KryptonDataGridView By changing the Palette according to the main form.
  37.         Me.PaletteMode = Krypton.Toolkit.PaletteMode.Global
  38.         '// If you want to specify a specific display, you can select PaletteMode.
  39.         Me.dgvData.PaletteMode = Krypton.Toolkit.PaletteMode.Global
  40.         'Me.dgvData.PaletteMode = Krypton.Toolkit.PaletteMode.Office2007Black
  41.     End Sub

  42.     ' / --------------------------------------------------------------------------------------------
  43.     ' / Change Palette Mode.
  44.     ' / --------------------------------------------------------------------------------------------
  45.     Private Sub cmbPalette_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cmbPalette.SelectedIndexChanged
  46.         Dim manager As New KryptonManager()
  47.         Select Case cmbPalette.SelectedIndex
  48.             Case 0
  49.                 'KryptonManager.GlobalPaletteMode = PaletteModeManager.ProfessionalOffice2003
  50.                 manager.GlobalPaletteMode = PaletteModeManager.ProfessionalOffice2003
  51.             Case 1
  52.                 manager.GlobalPaletteMode = PaletteModeManager.Office2007Blue
  53.             Case 2
  54.                 manager.GlobalPaletteMode = PaletteModeManager.Office2007Silver
  55.             Case 3
  56.                 manager.GlobalPaletteMode = PaletteModeManager.Office2007Black
  57.             Case 4
  58.                 manager.GlobalPaletteMode = PaletteModeManager.Office2010Blue
  59.             Case 5
  60.                 manager.GlobalPaletteMode = PaletteModeManager.Office2010Silver
  61.             Case 6
  62.                 manager.GlobalPaletteMode = PaletteModeManager.Office2010Black
  63.         End Select
  64.     End Sub

  65.     ' / --------------------------------------------------------------------------------------------
  66.     ' / Initialized DataGridView @Run Time.
  67.     ' / --------------------------------------------------------------------------------------------
  68.     Private Sub InitializeGrid()
  69.         With dgvData
  70.             .RowHeadersVisible = False
  71.             .AllowUserToAddRows = False
  72.             .AllowUserToDeleteRows = False
  73.             .AllowUserToResizeRows = False
  74.             .MultiSelect = False
  75.             '// Need to modify each cell.
  76.             .SelectionMode = DataGridViewSelectionMode.CellSelect
  77.             .ReadOnly = False
  78.             '// Font for RowTemplate.
  79.             .RowTemplate.DefaultCellStyle.Font = New Font("Tahoma", 11, FontStyle.Regular)
  80.             .RowTemplate.MinimumHeight = 32
  81.             '// Header.
  82.             .ColumnHeadersDefaultCellStyle.Font = New Font("Tahoma", 11, FontStyle.Bold)
  83.             '// Show alternating colors in even and odd rows.
  84.             .AlternatingRowsDefaultCellStyle.BackColor = Color.AliceBlue
  85.             ' Automatically set the width.
  86.             .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
  87.             '// Set ColumnHeadersHeightSizeMode before adjusting row heights.
  88.             'dgvData.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
  89.             dgvData.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing
  90.             dgvData.ColumnHeadersHeight = 36
  91.             '// GridStyles
  92.             '.GridStyles.Style = DataGridViewStyle.List
  93.             '.GridStyles.Style = DataGridViewStyle.Mixed
  94.             .GridStyles.Style = DataGridViewStyle.Sheet
  95.         End With

  96.         '// Create columns dynamically.
  97.         Dim colString As New KryptonDataGridViewTextBoxColumn() '// Index 0
  98.         dgvData.Columns.Add(colString)
  99.         With dgvData.Columns(0) '// OR colString
  100.             .Name = "colString"
  101.             .HeaderText = "String"
  102.         End With
  103.         '//
  104.         Dim colInteger As New KryptonDataGridViewTextBoxColumn()   '// Index 1
  105.         dgvData.Columns.Add(colInteger)
  106.         With colInteger
  107.             .Name = "colInteger"
  108.             .HeaderText = "Integer"
  109.             .DefaultCellStyle.Format = "0"
  110.         End With
  111.         '//
  112.         Dim colDouble As New KryptonDataGridViewTextBoxColumn()   '// Index 2
  113.         dgvData.Columns.Add(colDouble)
  114.         With colDouble
  115.             .Name = "colDouble"
  116.             .HeaderText = "Double"
  117.             .DefaultCellStyle.Format = "0.00"
  118.         End With
  119.         '//
  120.         Dim colCombo As New KryptonDataGridViewComboBoxColumn()   '// Index 3
  121.         dgvData.Columns.Add(colCombo)
  122.         With colCombo
  123.             .Name = "colCombo"
  124.             .HeaderText = "ComboBox"
  125.             .DropDownStyle = ComboBoxStyle.DropDownList
  126.             .ReadOnly = False
  127.             .DisplayMember = "Name"
  128.             .DataSource = GetDataTable()
  129.         End With
  130.         '//
  131.         '// Create a KryptonDataGridViewDateTimePickerColumn.
  132.         Dim dateTimePickerColumn As New KryptonDataGridViewDateTimePickerColumn()  '// Index 4
  133.         With dateTimePickerColumn
  134.             .HeaderText = "Date"
  135.             .Name = "colDate"
  136.             '// Set the format of the DateTimePicker.
  137.             .Format = DateTimePickerFormat.Short
  138.         End With
  139.         '// Add the column to the KryptonDataGridViewCheckBoxColumn.
  140.         dgvData.Columns.Add(dateTimePickerColumn)
  141.         '//
  142.         Dim colCheckBox As New KryptonDataGridViewCheckBoxColumn()    '// Index 5
  143.         dgvData.Columns.Add(colCheckBox)
  144.         With colCheckBox
  145.             .Name = "colCheckBox"
  146.             .HeaderText = "CheckBox"
  147.         End With
  148.         '//
  149.         Dim colUpDown As New KryptonDataGridViewNumericUpDownColumn()  '// Index 6
  150.         dgvData.Columns.Add(colUpDown)
  151.         With colUpDown
  152.             .Name = "colUpDown"
  153.             .HeaderText = "UpDown"
  154.             .Maximum = 100
  155.             .Minimum = 0
  156.             .Increment = 1
  157.         End With
  158.         '// Add 8th column (Index = 7), It's Button.
  159.         Dim btnAddRow As New KryptonDataGridViewButtonColumn()    '// Index 7
  160.         dgvData.Columns.Add(btnAddRow)
  161.         With btnAddRow
  162.             .HeaderText = "Add-F7"
  163.             .Text = "Add"
  164.             .Name = "btnAddRow"
  165.             .UseColumnTextForButtonValue = True
  166.             .Width = 80
  167.         End With
  168.         '// Add 9th column (Index = 8), It's Button.
  169.         Dim btnRemoveRow As New KryptonDataGridViewButtonColumn()     '// Index 8
  170.         dgvData.Columns.Add(btnRemoveRow)
  171.         With btnRemoveRow
  172.             .HeaderText = "Del-F8"
  173.             .Text = "Delete"
  174.             .Name = "btnRemoveRow"
  175.             .UseColumnTextForButtonValue = True
  176.             .Width = 80
  177.         End With
  178.         '// Alignment header and cell any columns.
  179.         For iCol As Byte = 1 To 8
  180.             If iCol = 1 Or iCol = 2 Then
  181.                 dgvData.Columns(iCol).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
  182.                 dgvData.Columns(iCol).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
  183.             ElseIf iCol = 3 Or iCol = 4 Or iCol = 6 Then
  184.                 dgvData.Columns(iCol).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleLeft
  185.                 dgvData.Columns(iCol).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
  186.             Else
  187.                 dgvData.Columns(iCol).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
  188.                 dgvData.Columns(iCol).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
  189.             End If
  190.         Next
  191.     End Sub

  192.     ' / --------------------------------------------------------------------------------------------
  193.     ' / Sample DataTable for ComboBox.
  194.     ' / --------------------------------------------------------------------------------------------
  195.     Function GetDataTable() As DataTable
  196.         Dim DT As DataTable = New DataTable
  197.         '// Add new columns.
  198.         With DT.Columns
  199.             .Add("PK", GetType(Integer))
  200.             .Add("Name", GetType(String))
  201.         End With
  202.         '// Add sample data rows.
  203.         With DT.Rows
  204.             .Add(1, "M100")
  205.             .Add(2, "M150")
  206.             .Add(3, "M16")
  207.             .Add(4, "M4")
  208.             .Add(5, "AK47")
  209.             .Add(6, "RPG")
  210.         End With
  211.         Return DT
  212.     End Function

  213.     ' / --------------------------------------------------------------------------------------------
  214.     ' / SAMPLE DATA FOR ADD TO ROW.
  215.     ' / --------------------------------------------------------------------------------------------
  216.     Private Sub AddRow()
  217.         Dim RandGen As New Random()
  218.         '// Random Double Value.
  219.         Dim minValue As Double = 0.0 ' Minimum value for the range
  220.         Dim maxValue As Double = 9999.0 ' Maximum value for the range
  221.         '// Generate a random double between minValue and maxValue.
  222.         Dim randomDouble As Double = RandGen.NextDouble() * (maxValue - minValue) + minValue
  223.         '// ComboBox.
  224.         'Dim ls As New List(Of String) ({"M100", "M150", "M16", "M4", "AK47", "RPG"})
  225.         '// Get sample data from DataTable.
  226.         Dim dt As DataTable = GetDataTable()
  227.         Dim ls As New List(Of String)()
  228.         For Each row As DataRow In dt.Rows
  229.             ls.Add(row("Name").ToString())
  230.         Next
  231.         '// Random value from List.
  232.         Dim RandArray As String = ls(RandGen.Next(0, ls.Count))
  233.         '// Sample data for each columns.
  234.         '// String, Integer, Double, ComboBox, Date, CheckBox, UpDown
  235.         dgvData.Rows.Add( _
  236.             "Product " & RandGen.Next(1, 999), _
  237.             RandGen.Next(1, 999), _
  238.             randomDouble, _
  239.             RandArray, _
  240.             DateTime.Today.AddDays(-RandGen.Next(365)), _
  241.             IIf(RandGen.Next(0, 2) > 0, "True", "False"), _
  242.             RandGen.Next(1, 100) _
  243.             )
  244.     End Sub

  245.     ' / --------------------------------------------------------------------------------------------
  246.     ' / Sub program to delete selected rows.
  247.     ' / --------------------------------------------------------------------------------------------
  248.     Private Sub DeleteRow()
  249.         If dgvData.RowCount = 0 Then Return
  250.         '// Delete selected row items.
  251.         dgvData.Rows.Remove(dgvData.CurrentRow)
  252.     End Sub

  253.     ' / --------------------------------------------------------------------------------------------
  254.     ' / Click mouse event in grid cell.
  255.     ' / --------------------------------------------------------------------------------------------
  256.     Private Sub dgvData_CellClick(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvData.CellClick
  257.         Select Case e.ColumnIndex
  258.             '// Add Row.
  259.             Case 7
  260.                 Call AddRow()

  261.                 '// Delete Row.
  262.             Case 8
  263.                 'MsgBox(("Row : " + e.RowIndex.ToString & "  Col : ") + e.ColumnIndex.ToString)
  264.                 Call DeleteRow()
  265.         End Select
  266.     End Sub

  267.     ' / --------------------------------------------------------------------------------------------
  268.     ' / Enter press event in grid cell.
  269.     ' / --------------------------------------------------------------------------------------------
  270.     Private Sub dgvData_CellEndEdit(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvData.CellEndEdit
  271.         '/ change occurred in the 1st or 2nd index digit.
  272.         Select Case e.ColumnIndex
  273.             Case 1  '/ Integer Value.
  274.                 '/ Index = 1 (Integer)
  275.                 '/ To trap errors in the case of having a Null Value, enter the value 0 instead.
  276.                 If IsNothing(dgvData.Rows(e.RowIndex).Cells(1).Value) Then dgvData.Rows(e.RowIndex).Cells(1).Value = "0"
  277.                 '/ Handle the CellFormatting event to format numeric values.
  278.                 AddHandler dgvData.CellFormatting, AddressOf KryptonDataGridView_CellFormatting

  279.             Case 2 '/ Double Value.
  280.                 '/ Index = 2 (Double)
  281.                 '/ If Null Value
  282.                 If IsNothing(dgvData.Rows(e.RowIndex).Cells(2).Value) Then dgvData.Rows(e.RowIndex).Cells(2).Value = "0.00"
  283.                 '/ Handle the CellFormatting event to format numeric values.
  284.                 AddHandler dgvData.CellFormatting, AddressOf KryptonDataGridView_CellFormatting
  285.         End Select
  286.     End Sub

  287.     ' / --------------------------------------------------------------------------------------------
  288.     ' / When you start pressing the keys in digits (Index) 1 and 2.
  289.     ' / --------------------------------------------------------------------------------------------
  290.     Private Sub dgvData_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles dgvData.EditingControlShowing
  291.         '// Or use the index columns of DataGridView.
  292.         'Select Case dgvData.Columns(dgvData.CurrentCell.ColumnIndex).Index
  293.         '    Case 1
  294.         '
  295.         'End Select

  296.         '// Don't forget to specify the Column Name earlier as well (InitializeGrid sub program).
  297.         Select Case dgvData.Columns(dgvData.CurrentCell.ColumnIndex).Name
  298.             ' / Can use both Colume Index or Field Name
  299.             Case "colInteger", "colDouble"
  300.                 If TypeOf e.Control Is Krypton.Toolkit.KryptonDataGridViewTextBoxEditingControl Then
  301.                     Dim editingControl As Krypton.Toolkit.KryptonDataGridViewTextBoxEditingControl = TryCast(e.Control, Krypton.Toolkit.KryptonDataGridViewTextBoxEditingControl)
  302.                     '// Event Handler for intercepts keystrokes.
  303.                     AddHandler editingControl.KeyPress, AddressOf KryptonDataGridViewKeyPress
  304.                 End If
  305.         End Select
  306.     End Sub

  307.     ' / --------------------------------------------------------------------------------------------
  308.     ' / Intercepts keystrokes only with numeric and point (.) only one.
  309.     ' / --------------------------------------------------------------------------------------------
  310.     Private Sub KryptonDataGridViewKeyPress(sender As Object, e As KeyPressEventArgs)
  311.         Select Case dgvData.CurrentCell.ColumnIndex
  312.             Case 1, 6   '// Integer
  313.                 '// Allow numeric digits (0-9) in the TextBox.
  314.                 If Not Char.IsControl(e.KeyChar) AndAlso Not Char.IsDigit(e.KeyChar) Then e.Handled = True

  315.             Case 2  '// Double
  316.                 '// Allow numeric digits (0-9) and the decimal point (.) only one in the TextBox.
  317.                 If Not Char.IsControl(e.KeyChar) AndAlso Not Char.IsDigit(e.KeyChar) AndAlso e.KeyChar <> "." Then e.Handled = True
  318.                 '// Check for an existing decimal point in the cell.
  319.                 Dim tb As KryptonDataGridViewTextBoxEditingControl = DirectCast(sender, KryptonDataGridViewTextBoxEditingControl)
  320.                 If e.KeyChar = "." AndAlso tb.Text.Contains(".") Then e.Handled = True
  321.         End Select
  322.     End Sub

  323.     ' / --------------------------------------------------------------------------------------------
  324.     ' / Event Handler from dgvData_CellEndEdit to format numbers.
  325.     ' / --------------------------------------------------------------------------------------------
  326.     Private Sub KryptonDataGridView_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs)
  327.         If e.ColumnIndex = dgvData.Columns("colInteger").Index Then
  328.             '// Format as an integer.
  329.             If Not e.Value Is Nothing AndAlso IsNumeric(e.Value) Then e.Value = CInt(e.Value)
  330.         ElseIf e.ColumnIndex = dgvData.Columns("colDouble").Index Then
  331.             '// Format as a double with two decimal places.
  332.             If Not e.Value Is Nothing AndAlso IsNumeric(e.Value) Then e.Value = CDbl(e.Value).ToString("0.00")
  333.         End If
  334.     End Sub
  335. End Class
คัดลอกไปที่คลิปบอร์ด

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

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

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

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

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

1

กระทู้

14

โพสต์

169

เครดิต

Member

Rank: 2

เครดิต
169
โพสต์ 2024-4-24 09:57:15 | ดูโพสต์ทั้งหมด

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

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

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

GMT+7, 2024-5-4 08:15 , Processed in 0.155362 second(s), 4 queries , File On.

Powered by Discuz! X3.4, Rev.62

Copyright © 2001-2020 Tencent Cloud.

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