DataGridViewを継承し、Enter押下時に改行しないDataGridViewEXクラスを作成する
DataGridViewEX.vb
Public Class DataGridViewEX
Inherits DataGridView
Public Event OnPress(rowIndex As Integer, olumnIndes As Integer, val As String)
Protected Overrides Function ProcessDialogkey(keyData As Keys) As Boolean
If (keyData And Keys.KeyCode) = Keys.Enter Then
Me.EndEdit()
RaiseEvent OnPress(CurrentCell.RowIndex, CurrentCell.ColumnIndex, CurrentCell.EditedFormattedValue)
Return True
End If
Return MyBase.ProcessDialogKey(keyData)
End Function
Protected Overrides Function ProcessDataGridViewKey(e As KeyEventArgs) As Boolean
If e.KeyCode = Keys.Enter Then
Me.EndEdit()
RaiseEvent OnPress(CurrentCell.RowIndex, CurrentCell.ColumnIndex, CurrentCell.EditedFormattedValue)
Return True
End If
Return MyBase.ProcessDataGridViewKey(e)
End Function
End Class
Enter押下を知らせるイベント
VB
Private Sub OnPress(rowIndex As Integer, columnIndex As Integer, val As String) Handles DataGridViewex1.OnPress
MessageBox.Show(val)
End Sub
動作確認用コード
VB
Dim dt As New DataTable
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
With dt
.Columns.Add("Col1", GetType(String))
.Columns.Add("Col2", GetType(String))
.Rows.Add("A001", "B001")
.Rows.Add("A002", "B002")
.Rows.Add("A003", "B003")
.Rows.Add("A004", "B004")
.Rows.Add("A005", "B005")
End With
With DataGridViewex1
.DataSource = dt
.AllowUserToAddRows = False
End With
End Sub
コメント