【发布时间】:2015-06-05 20:27:12
【问题描述】:
我通过按标签按钮以编程方式跳转到下一行。 如果我想跳回去,我使用 tab + shift 键。 如果按下 tab + shift,则行数减少 2。 当我想从最后一行返回时,索引会跳转到第一个控件,该控件的选项卡索引为 0。 最后一行有什么问题?
private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab)
{
int row = dataGridView1.CurrentCell.RowIndex;
row++;
if (row > dataGridView1.RowCount - 1)
{
menuStrip1.Select();
datensatzToolStripMenuItem.Select();
dataGridView1.CurrentCell = dataGridView1[0, 0];
}
else dataGridView1.CurrentCell = dataGridView1[0, row];
e.Handled = true;
}
if (e.Modifiers == Keys.Shift && e.KeyCode == Keys.Tab)
{
int row = dataGridView1.CurrentCell.RowIndex;
row -= 2;
if (row < 0)
{
menuStrip1.Select();
datensatzToolStripMenuItem.Select();
dataGridView1.CurrentCell = dataGridView1[0, 0];
}
else dataGridView1.CurrentCell = dataGridView1[0, row];
e.Handled = true;
}
}
【问题讨论】:
-
两件事:首先,我不认为 Tab 键被 KeyDown 事件捕获。其次,如果按下 SHIFT+Tab,则该方法中的两个条件都将满足 - 即 (e.KeyCode == Keys.Tab) 和 (e.Modifiers == Keys.Shift && e.KeyCode == Keys.Tab ) 将是真的。
-
我的问题是,标准选项卡分配跳转到下一个单元格,但我需要跳转到下一行,因此我使用 keydown 事件对其进行了编程。
-
你测试过Tab键是否真的触发了KeyDown事件吗?
-
是的,工作正常。但是当我在 dgv 的最后一行按 shift + tab 时,它会聚焦第一行并选择第一个控件(标签索引 0)
标签: c# indexing datagridview tabs