假定, 当你在DataGridView中按下回车键时,这个光标会移动到相同列的 所在单元格下面的单元格(下图红色箭头所示),但是当输入多行数据时,更好的响应回车键的方式是移动到下一行的第一个单元格中(蓝色箭头).
为了做到这样,你能使用派生自 DataGridView的类 :
public
class
Grid : DataGridView
{
然后覆写(override) 这个 OnKeyUp 受保护的方法:
{
然后覆写(override) 这个 OnKeyUp 受保护的方法:
protected
override
void
OnKeyUp( KeyEventArgs e )
{
if (e.KeyCode == Keys.Enter)
{
int currentRow = this .CurrentRow.Index;
if (currentRow >= 0 )
this .CurrentCell = this .Rows[currentRow].Cells[ 0 ];
}
base .OnKeyUp( e );
}
当然 , 如果你希望在已存在的
DataGridView 提供这种能力, 你能简单的签名
KeyUp 事件并且执行上面相同的代码在这个事件处理程序中(event handler).
{
if (e.KeyCode == Keys.Enter)
{
int currentRow = this .CurrentRow.Index;
if (currentRow >= 0 )
this .CurrentCell = this .Rows[currentRow].Cells[ 0 ];
}
base .OnKeyUp( e );
}