【发布时间】:2015-06-16 17:42:18
【问题描述】:
我想让DataGridView 在操作KeyDown 事件时更方便,例如KeyEnter 用于向右移动一个单元格,或者使用向左、向下或向上。我有一些特殊的用例,我想首先检查单元格,然后我可以丢弃这个事件,或者将它指向另一个单元格。在我将“多选”功能设置为 true 之前,这一切正常。然后离开焦点并设置正确焦点的完整过程,每个单元格不再正常工作。请在下面找到一些示例代码:
private bool GridNavigation(Keys keys)
{
if (dataGridView1.CurrentRow == null)
{
return true;
}
int iColumn = dataGridView1.CurrentCell.ColumnIndex;
int iRow = dataGridView1.CurrentCell.RowIndex;
if (keys == Keys.Enter || keys == Keys.Right)
{
for (int i = iColumn + 1; i <= dataGridView1.Columns.Count - 4; i++)
{
if (!dataGridView1.Rows[iRow].Cells[i].ReadOnly)
{
dataGridView1.Rows[iRow].Cells[i].Selected = true;
break;
}
if (i == dataGridView1.Columns.Count - 4 && dataGridView1.Rows.Count - 1 != iRow)
{
if (((IDictionary<int, int>)dataGridView1.CurrentRow.Tag).SingleOrDefault(p => p.Key == (int)clsDefinitions.ZeilenIndikator.Typ).Value == (int)clsDefinitions.ZeilenTyp.PassLängeAusgangswert)
dataGridView1.CurrentCell = dataGridView1[0, iRow + 2];
else
dataGridView1.CurrentCell = dataGridView1[0, iRow + 1];
return true;
}
}
return true;
}
else if (keys == Keys.Left)
{
for (int i = iColumn - 1; i >= 0; i--)
{
if (!dataGridView1.Rows[iRow].Cells[i].ReadOnly)
{
dataGridView1.Rows[iRow].Cells[i].Selected = true;
break;
}
}
return true;
}
else
return false;
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Enter || keyData == Keys.Right || keyData == Keys.Left)
{
return GridNavigation(keyData);
}
return base.ProcessCmdKey(ref msg, keyData);
}
那么这里可以做什么。您可以通过打开一个新项目轻松重现,只需在表单中输入 DataGridView 并添加例如 10 列或更多列。
如果还有什么我可以提供的,请告诉我。
【问题讨论】:
-
if (((IDictionary<int, int>)dataGridView1.CurrentRow.Tag).SingleOrDefault(p => p.Key == (int)clsDefinitions.ZeilenIndikator.Typ).Value == (int)clsDefinitions.ZeilenTyp.PassLängeAusgangswert)在复制您的输出时我们需要知道什么特别之处吗? -
不,没什么特别的。它检查该行是否具有存储在 Row.Tag 下的特殊标志集。因此,如果有一个特殊的标签,例如你提到的那个,它会跳两行而不是一行。但这对我描述的问题并没有真正的影响。希望这会有所帮助。
标签: c# winforms datagridview multi-select