2009-01-31 97 views

回答

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

我找到了我需要的帮助,以便让我进入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); 

    } 

} 
0

DataGridView的焦点到下一个单元或列时,输入键被按下VB.net:

私人小组DataGridView1_CellEndEdit(BYVAL发件人为对象,BYVALË作为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( “{右}”) 结束如果

结束子

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 
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 

末级