【问题标题】:DataGridView KeyDown Event Keys.EnterDataGridView KeyDown 事件 Keys.Enter
【发布时间】:2016-06-16 10:57:25
【问题描述】:

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

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

这仅在将焦点放在单元格上时有效,但如果我们在单元格中编辑“某物”,然后按 Enter,焦点将移至所选单元格下方。

【问题讨论】:

  • if (e.KeyData == Keys.Enter) { SendKeys.Send("{TAB}"); e.Handled = true; }
  • 我的问题在哪里?
  • 我将其复制为 Enter As a tab key 当焦点在一个单元格上时它可以正常工作但是当一个单元格开始编辑并且在编辑或结束编辑之后焦点将转到当前单元格但我希望将注意力转移到关于这种情况的下一个单元格,请帮助我......谢谢
  • 关注链接的帖子并使用继承的DataGridView。

标签: c# winforms datagridview


【解决方案1】:

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
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-06
    • 2011-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多