ในวันที่ลูกชาย/ลูกสาวของผมไกล้ตาย แน่นอนว่า ผมอยู่บนสวงสวรรค์(นรกไม่ต้องการคนอย่างผม)
--- พ่อจะซื้อจักรยานให้ลองขี่นะลูก หมายถึงว่า เริ่มหัดเขียน Windows Forms Application
...
...
...
Date :
2019-04-17 18:02:51
By :
หน้าฮี
No. 6
Guest
ถ้าคุณอยากเล่นกับ DataGridView จริงฯ ลองไล่ดูครับ โลกของ Windows Application DataGridView คงไม่หนีพ้นไปจากนี้
Source Code
Code (VB.NET)
Public Class frmDGVTextButtonvb
Private btnSelector As New Button() With {.TabStop = False, .FlatStyle = FlatStyle.Flat}
'สร้าง Buttnon
Private pCase As Integer
'สำหรับ เก็บส่วนของการเลือก Column
Private x As String
'สำหรับสร้างหี ยังไม่ได้ทำ
Private หี
Private curColIndex As Integer
Private curRowIndex As Integer
Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
End Sub
Private Sub frmDGVTextButtonvb_Shown(sender As Object, e As EventArgs) Handles Me.Shown
DataGridView1.MultiSelect = False
DataGridView1.Select()
DataGridView1.CurrentCell = DataGridView1(0, DataGridView1.NewRowIndex) 'DataGridView1.Rows(0).Cells(DataGridView1.NewRowIndex)
DataGridView1.CurrentCell.Selected = True
'DataGridView1.BeginEdit(True)
End Sub
Private Sub frmDGVTextButtonvb_Load(sender As Object, e As EventArgs) Handles Me.Load
'DataGridView1.MultiSelect = False
Dim col1 As New DataGridViewTextBoxColumn With {.Name = "Column1", .Width = 100, .DisplayIndex = 0}
Dim col2 As New DataGridViewTextBoxColumn With {.Name = "Column2", .Width = 300, .DisplayIndex = 1}
Dim col3 As New DataGridViewTextBoxColumn With {.Name = "Column3", .Width = 300, .DisplayIndex = 2}
Dim col4 As New DataGridViewTextBoxColumn With {.Name = "Column4", .Width = 120, .DisplayIndex = 3}
'Dim col4 As New DataGridViewComboBoxColumn With {.Name = "cboXXX", .DisplayIndex = 3}
DataGridView1.Columns.AddRange(New DataGridViewColumn() {col1, col2, col3, col4})
'Me.DataGridView1.Rows.Clear()
'Me.DataGridView1.RowTemplate.MinimumHeight = 50
'Dim dgvCol As New Test_Win.TextBoxButtonColumn
'DataGridView1.Columns.Add(dgvCol)
'DataGridView1.Columns.Clear()
' DataGridView1.RowHeadersWidth = 50
CreateButton(btnSelector)
DataGridView1.Controls.Add(btnSelector)
'Fixed ความสูงของแถวแรกไม่เท่ากับแถวอื่นฯ
'DataGridView1.EditMode = DataGridViewEditMode.EditOnEnter
'For Each r In DataGridView1.Rows
' DataGridView1.CurrentCell = DataGridView1.Rows(0).Cells(2)
' DataGridView1.BeginEdit(False)
'Next
'DataGridView1.ClearSelection()
'DataGridView1.Select()
'DataGridView1.BeginEdit(True)
'DataGridView1.CurrentCell = DataGridView1.Rows(0).Cells(1)
'DataGridView1.CurrentCell.Value = "123"
'DataGridView1.CurrentCell.Selected = True
'ataGridView1.Focus()
' DataGridView1.Columns(0).DisplayIndex = 3
'DataGridView1.ClearSelection()
'DataGridView1.BeginEdit(True)
'For i As Integer = 0 To 100
' DataGridView1.Rows.Add("")
'Next
''Me.DataGridView1.Select()
''DataGridView1.Focus()
'DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
'DataGridView1.CurrentCell = DataGridView1.FirstDisplayedCell
'DataGridView1.BeginEdit(True)
'DataGridView1.ClearSelection()
'If DataGridView1.Rows.Count = 1 AndAlso DataGridView1.Rows(0).IsNewRow Then
' 'DataGridView1.CurrentCell = DataGridView1(0, 0)
' 'DataGridView1.BeginEdit(True)
' 'DataGridView1.Rows(0).Cells(0).Value = "aaa"
' ''DataGridView1.EndEdit()
' ''DataGridView1.UpdateCellValue(0, 0)
' ''DataGridView1.CausesValidation = True
' ''Me.ValidateChildren()
' ''Me.Validate()
' 'DataGridView1.CurrentCell.Selected() = True
' DataGridView1.CurrentCell = DataGridView1.Rows(0).Cells(0)
' 'DataGridView1.Rows(0).Cells(0).Value = "aaa"
' DataGridView1.CurrentCell.Value = "asdf"
' DataGridView1.CurrentCell.Selected = True
' DataGridView1.BeginEdit(True)
' 'MsgBox(DataGridView1.CurrentCell.IsInEditMode)
' 'TextBox1.Focus()
' 'DataGridView1.Focus()
'End If
'DataGridView1.CurrentCell.Selected = True
'DataGridView1.BeginEdit(True)
'DataGridView1.FirstDisplayedScrollingRowIndex = 0
''If DataGridView1.CurrentRow.IsNewRow Then
'' MsgBox("Yes")
''End If
'
'DataGridView1.Select()
'DataGridView1.CurrentCell = DataGridView1(0, DataGridView1.NewRowIndex) 'DataGridView1.Rows(0).Cells(DataGridView1.NewRowIndex)
'DataGridView1.CurrentCell.Selected = True
'DataGridView1.BeginEdit(True)
'My.Computer.Keyboard.SendKeys("{BACKSPACE}")
'DataGridView1.MultiSelect = False
'DataGridView1.Select()
'DataGridView1.CurrentCell = DataGridView1(0, DataGridView1.NewRowIndex) 'DataGridView1.Rows(0).Cells(DataGridView1.NewRowIndex)
'DataGridView1.CurrentCell.Selected = True
'DataGridView1.BeginEdit(True)
End Sub
Private Sub SelectorClick(ByVal sender As Object, ByVal e As EventArgs)
Select Case pCase
Case 1
'value from dataGridView1_CellEnter
If True Then
MessageBox.Show("Show Dialog here!!")
'Exit Select
End If
End Select
'DataGridView1.NotifyCurrentCellDirty(False)
'DataGridView1.CommitEdit(DataGridViewDataErrorContexts.CurrentCellChange)
'DataGridView1.CurrentCell = DataGridView1(curRowIndex, 0)
'DataGridView1.CurrentCell.Selected = True
DataGridView1.CurrentCell = DataGridView1(curColIndex, curRowIndex)
DataGridView1.CurrentCell.Selected = True
DataGridView1.BeginEdit(True)
'DataGridView1.CurrentCell.DetachEditingControl()
End Sub
Private Sub CreateButton(ByRef myButton As Button)
Dim dgvHeight As Integer = Me.DataGridView1.RowTemplate.Height
myButton.FlatStyle = FlatStyle.Flat
myButton.FlatAppearance.BorderSize = 0
myButton.Size = New Size(dgvHeight, dgvHeight) 'New Size(30, 30)
myButton.ImageAlign = ContentAlignment.MiddleCenter
myButton.FlatAppearance.MouseDownBackColor = Color.Transparent
myButton.FlatAppearance.MouseOverBackColor = Color.Transparent
myButton.BackColor = Color.Transparent
myButton.FlatAppearance.BorderColor = Color.FromArgb(0, 255, 255, 255) '//transparent
'myButton.Image = Properties.resources.search3
myButton.Image = My.Resources.file_new
'myButton.Image = Image.FromFile(CurDir() + "\pic\SearchBc.png")
'myButton.Anchor = AnchorStyles.Right
'myButton.Anchor = AnchorStyles.Top
'กำหนดรูปภาพ
myButton.TabStop = False
'myButton.Parent = DataGridView1
myButton.Hide()
AddHandler myButton.Click, New EventHandler(AddressOf Me.SelectorClick)
'--------------------------------------------
End Sub
Private Sub DataGridView1_CellEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEnter
If DataGridView1.Columns(e.ColumnIndex).Name = "Column1" Then
pCase = 1
Dim Loc As Rectangle = DataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, False)
Dim W As Integer = DataGridView1.CurrentCell.Size.Width
Dim H As Integer = DataGridView1.CurrentCell.Size.Height
'btnSelector.Location = New Point(Loc.X - 30 + W, Loc.Y)
Dim x = btnSelector.Height
btnSelector.Location = New Point(Loc.X - btnSelector.Width + W, Loc.Y) '(Loc.X - 30 + width,
btnSelector.Show()
curColIndex = e.ColumnIndex
curRowIndex = e.RowIndex
End If
End Sub
Private Sub DataGridView1_CellLeave(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellLeave
If btnSelector.Focused <> True Then
btnSelector.Hide()
End If
End Sub
Private Sub DataGridView1_ColumnWidthChanged(sender As Object, e As DataGridViewColumnEventArgs) Handles DataGridView1.ColumnWidthChanged
If e.Column.Index = 0 Then
'If DataGridView1.Columns(e.ColumnIndex).Name = "Column1" Then
'pCase = 1
Dim Loc As Rectangle = DataGridView1.GetCellDisplayRectangle(e.Column.Index, DataGridView1.CurrentCell.RowIndex, False)
Dim width As Integer = DataGridView1.CurrentCell.Size.Width
Dim height As Integer = DataGridView1.CurrentCell.Size.Height
btnSelector.Location = New Point(Loc.X - btnSelector.Width + width, Loc.Y) 'Loc.X - 30 'btnSelector.Width
btnSelector.Show()
'End If
End If
End Sub
Private Sub DataGridView1_CurrentCellDirtyStateChanged(sender As Object, e As EventArgs) Handles DataGridView1.CurrentCellDirtyStateChanged
'If DataGridView1.IsCurrentCellDirty Then
' DataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)
'End If
End Sub
'Executes when Cell Value on a DataGridView changes
Private Sub DataGridCellValueChanged(sender As DataGridView, e As DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
'check that row isn't -1, i.e. creating datagrid header
If e.RowIndex = -1 Then Exit Sub
'mark as dirty
'IsDirty = True
End Sub
Private Sub DataGridView1_DataError(sender As Object, e As DataGridViewDataErrorEventArgs) Handles DataGridView1.DataError
MsgBox(e.Exception.Message)
End Sub
Private Sub DataGridView1_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
'http://vbcity.com/forums/t/152435.aspx
If DataGridView1.CurrentCell.ColumnIndex = 3 Then
Dim txtedit As TextBox = DirectCast(e.Control, TextBox)
AddHandler txtedit.KeyPress, AddressOf txtEdit_KeyPress
End If
End Sub
Private Sub txtEdit_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
If DataGridView1.CurrentCell.ColumnIndex = 3 Then
If ("0123456789.".IndexOf(e.KeyChar) = -1) Then 'If ("0123456789.\b".IndexOf(e.KeyChar) = -1) Then
If e.KeyChar <> Convert.ToChar(Keys.Back) Then
e.Handled = True
End If
End If
End If
End Sub
Private Sub DataGridView1_KeyDown(sender As Object, e As KeyEventArgs) Handles DataGridView1.KeyDown
'If e.KeyCode = Keys.Enter Then
' e.Handled = True
' SendKeys.Send("{TAB}")
'End If
End Sub
End Class