DatagridView中实现Enter键类似Tab功能

1 :重写打datagridview的ProcessCmdKey事件

 代码如下:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { DataGridView dv = (DataGridView)FromHandle(msg.HWnd); if (keyData == Keys.Enter) { if (dv.IsCurrentCellInEditMode) { if (dv.CurrentCell.RowIndex == dv.Rows.Count - 1) { SendKeys.Send("{Tab}"); } else { SendKeys.Send("{Up}"); SendKeys.Send("{Tab}"); } } } return base.ProcessCmdKey(ref msg, keyData); } 

2:画面上设置当前DatagridView的KeyDown事件:

private void dataGridView1_KeyDown(object sender, KeyEventArgs e) { if ((e.KeyCode == Keys.Return)) { SendKeys.Send("{Tab}"); e.Handled = true; if ((this.dataGridView1.FirstDisplayedScrollingColumnHiddenWidth > 0) && !xGrid1.Columns[this.dataGridView1.SelectedCells[0].ColumnIndex].Frozen) { this.dataGridView1.FirstDisplayedScrollingColumnIndex = this.dataGridView1.SelectedCells[0].ColumnIndex; } } } 

你可能感兴趣的:(object,UP)