我有网格视图有组合框列[“column2”]
if (keyData == (Keys.F11))
{
for (int i = 0; i < dataGridView1.RowCount - 1; i++)
{
//here i want to change index automatically using hot key (keyboard short cut )
}
return true;
答案 0 :(得分:1)
您无法直接将ShortcutKey
分配给单个单元格。处理KeyPress
事件DataGridView
以获取组合键。在事件处理程序中,输入以下代码
void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyData == (Keys.Alt | Keys.E))
{
dataGridView1.Rows[RowIndex].Cells[ColumnIndex].Selected = true;
dataGridView1.CurrentCell = dataGridView1.Rows[RowIndex].Cells[ColumnIndex];
dataGridView1.BeginEdit(false);
}
}
如果任何单元格已处于编辑模式,则编辑控件将获得KeyPress
事件而不是DataGridView
。如果你想克服这个问题,你必须继承现有的DataGridView控件并覆盖它的ProcessCmdKey
函数。请参阅this SO的答案。
要更改编辑组合的选定索引,请订阅EditingControlShowing
事件,并在事件处理程序中更改combobox
索引。
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
ComboBox control = e.Control as ComboBox;
if (control !=null)
{
// set the selected index of the combo here.
}
}