DataGridView KeyDown事件Keys.Enter

时间:2016-06-16 10:57:26

标签: c# winforms datagridview

请有人帮帮我?我的问题是:我使用下面的代码将焦点移到DataGridView行中的下一个单元格:

if (e.KeyData == Keys.Enter) 
{
    SendKeys.Send("{TAB}");
    e.Handled = true; 
}

这仅在关注单元格时有效,但如果我们在单元格中编辑“某事”然后按 Enter ,焦点将移动到所选单元格下方。

1 个答案:

答案 0 :(得分:0)

private void dataGridView1_SelectionChanged(object sender,EventArgs e)         {             尝试             {                 if(MouseButtons!= 0)返回;

            if (_celWasEndEdit != null && dataGridView1.CurrentCell != null)
            {
                // if we are currently in the next line of last edit cell
                if (dataGridView1.CurrentCell.RowIndex == _celWasEndEdit.RowIndex + 1 &&
                    dataGridView1.CurrentCell.ColumnIndex == _celWasEndEdit.ColumnIndex)
                {
                    int iColNew;
                    int iRowNew = 0;
                    if (_celWasEndEdit.ColumnIndex >= dataGridView1.ColumnCount - 1)
                    {
                        iColNew = 0;
                        iRowNew = dataGridView1.CurrentCell.RowIndex;
                    }
                    else
                    {
                        iColNew = _celWasEndEdit.ColumnIndex + 1;
                        iRowNew = _celWasEndEdit.RowIndex;
                    }
                    dataGridView1.CurrentCell = dataGridView1[iColNew, iRowNew];
                }
            }
            _celWasEndEdit = null;
        }
        catch(Exception)
        {
            //Invisible Column Exception
        }
    }