【问题标题】:how to assign hot key for datagridview combobox column?如何为 datagridview 组合框列分配热键?
【发布时间】:2014-02-02 10:12:22
【问题描述】:

我有网格视图有组合框列 ["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;

【问题讨论】:

标签: winforms c#-4.0 datagridview datagridviewcombobox


【解决方案1】:

您不能直接将ShortcutKey 分配给单个单元格。处理组合键的DataGridViewKeyPress 事件。在事件处理程序中,输入以下代码

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

【讨论】:

  • 我想通过快捷键更改此列的索引!
  • @KOKO 抱歉,我无法理解你的意图……你想达到什么目的?
  • 不仅选择组合框单元格,我还想更改单元格的索引
  • @KOKO 如果可行,请您接受答案并点赞
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-16
  • 2011-05-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多