如何覆盖datagridview中的回车键,以便将焦点设置为下一列而不是下一行?
答案 0 :(得分:3)
您需要做的就是处理DataGridView的KeyDown事件,并在处理程序中检查当前按下的键是否为Enter键。如果是这样,只需将DataGridView的CurrentCell设置为下一个单元格(同时检查这是否是行中的最后一个单元格,在这种情况下,移动到下一行的第一个单元格。)
Private Sub DataGridView1_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles DataGridView1.KeyDown
If e.KeyCode = Keys.Enter Then
Dim numCols As Integer = DataGridView1.ColumnCount
Dim numRows As Integer = DataGridView1.RowCount
Dim currCell As DataGridViewCell = DataGridView1.CurrentCell
If currCell.ColumnIndex = numCols - 1 Then
If currCell.RowIndex < numRows - 1 Then
DataGridView1.CurrentCell = DataGridView1.Item(0, currCell.RowIndex + 1)
End If
Else
DataGridView1.CurrentCell = DataGridView1.Item(currCell.ColumnIndex + 1, currCell.RowIndex)
End If
e.Handled = True
End If
End Sub
答案 1 :(得分:2)
Private Sub DbGDetail_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles DbGDetail.KeyDown
If e.KeyCode = Keys.Enter Then
SendKeys.Send("{Tab}")
e.Handled = True
End If
End Sub
Private Sub DbGDetail_CellEndEdit(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DbGDetail.CellEndEdit
SendKeys.Send("{up}")
SendKeys.Send("{Tab}")
End Sub
答案 2 :(得分:1)
我找到了帮助我明确进入this link所需的帮助
张荣春关于子类化DataGridView
以覆盖ProcessDialogKey
的反馈。我在下面的例子中改变了他的工作以满足我自己的要求。
目标:严格来说,只有在焦点离开第二行的任何单元格的情况下,才能保持网格中的焦点从最后一行末尾的最后一个单元格跳到按下ENTER键时的下一行。到最后一排。
背景:我启用了Grid选项,允许用户在数据库中创建新行,因此为了使它们变得简单,默认的Grid行为是总是在底部添加一个新的空行,并且尽快当用户甚至无意中移动到它上面时,会在数据库中创建一个新的空行。用户通常在任何单元格中完成编辑后点击ENTER,因此用户无法在不创建新空行的情况下将编辑结束到底部的最后一个单元格。
请注意,在这种情况下,我只是将CurrentCell保留在原来的位置,但可以在调用'return true'之前重新分配它。
public class DataGridViewAddRow : DataGridView
{
protected override bool ProcessDialogKey(Keys keyData)
{
//cell is in Edit mode
if (keyData == Keys.Enter)
{
if (this.CurrentCell.RowIndex == this.Rows.Count-2)
{
return true;
}
}
return base.ProcessDialogKey(keyData);
}
}
答案 3 :(得分:0)
当为VB.net按下Enter键时,DataGridView将焦点转移到下一个单元格或列:
Private Sub DataGridView1_CellEndEdit(ByVal sender As Object,ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)处理DataGridViewX1.CellEndEdit
如果DataGridView1.CurrentCell.ColumnIndex = DataGridView1.ColumnCount - 1则 DataGridView1.CurrentCell = DataGridView1.Item(0,DataGridView1.CurrentCell.RowIndex + 1) 其他 SendKeys.Send( “{向上}”) SendKeys.Send( “{右}”) 结束如果
End Sub
答案 4 :(得分:0)
公共类CustomDataGridView 继承DataGridView
<System.Security.Permissions.UIPermission( _
System.Security.Permissions.SecurityAction.LinkDemand, _
Window:=System.Security.Permissions.UIPermissionWindow.AllWindows)> _
Protected Overrides Function ProcessDialogKey( _
ByVal keyData As Keys) As Boolean
' Extract the key code from the key value.
Dim key As Keys = keyData And Keys.KeyCode
' Handle the ENTER key as if it were a RIGHT ARROW key.
If key = Keys.Enter Then
Return Me.ProcessTabKey(keyData)
End If
Return MyBase.ProcessDialogKey(keyData)
End Function
<System.Security.Permissions.SecurityPermission( _
System.Security.Permissions.SecurityAction.LinkDemand, Flags:= _
System.Security.Permissions.SecurityPermissionFlag.UnmanagedCode)> _
Protected Overrides Function ProcessDataGridViewKey( _
ByVal e As System.Windows.Forms.KeyEventArgs) As Boolean
' Handle the ENTER key as if it were a RIGHT ARROW key.
If e.KeyCode = Keys.Enter Then
Return Me.ProcessTabKey(e.KeyData)
End If
Return MyBase.ProcessDataGridViewKey(e)
End Function
结束班