首页 > 代码库 > DataGridView回车焦点横向移动

DataGridView回车焦点横向移动

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData =http://www.mamicode.com/= Keys.Enter) //监听回车事件
            {
                if (dataGridViewX1.IsCurrentCellInEditMode)   //如果当前单元格处于编辑模式
                {
                    if (dataGridViewX1.CurrentCell.RowIndex == dataGridViewX1.Rows.Count - 1)
                    {
                        SendKeys.Send("{Tab}");
                    }
                    else
                    {
                        SendKeys.Send("{Up}");
                        SendKeys.Send("{Tab}");
                    }
                }
            }
            //继续原来base.ProcessCmdKey中的处理
            return base.ProcessCmdKey(ref msg, keyData);
        }

 

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

DataGridView回车焦点横向移动