DataGridView回车焦点横向移动

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == 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;
                }
            }
        }

posted @ 2016-12-09 17:38  Elcser  阅读(508)  评论(0编辑  收藏  举报