上下键未触发datagridview单元格中的按键事件文本框



我试图在datagridview cell上弹出一个选择框按键事件。选择框弹出工作,但上下键不触发我想上下移动选择框数据,同时从datagridview cell按上下键

这些代码使用

    private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        DataGridView dttmp = (DataGridView)sender;
        csearch = "";
        var txtbox = e.Control as TextBox;
        if (txtbox != null)
        {
            txtbox.KeyPress -= new KeyPressEventHandler(textBoxPart_TextChanged);
            txtbox.KeyPress += new KeyPressEventHandler(textBoxPart_TextChanged);
            txtbox.Validating -= new CancelEventHandler(txtbox_Validating);
            txtbox.Validating += new CancelEventHandler(txtbox_Validating);
            txtbox.LostFocus += new EventHandler(txtbox_LostFocus);
        }
    }
 private void textBoxPart_TextChanged(object sender, KeyPressEventArgs e)
    {
        if (dataGridView1.CurrentCell.ColumnIndex == 1)
        {
            if (e.KeyChar == Convert.ToChar(Keys.Up))
            {
                if (srchbox.Rows.Count > 1)
                {
                    int curind = srchbox.CurrentRow.Index;
                    //srchbox.Rows[curind - 1].Selected = true;
                    if (curind - 1 >= 0)
                    {
                        srchbox.CurrentCell = srchbox.Rows[curind - 1].Cells[1];
                        srchbox.Refresh();
                    }
                }
                e.Handled = true;
            }

}

尝试使用DataGridView.CellEnter事件:

DataGridView控件中的当前单元格发生变化或当控件接收到输入焦点时发生。

DataGridView.CellLeave事件

当单元格失去输入焦点并且不再是当前单元格时发生。

使用keydown或keyup事件代替。

控制。KeyPress文档说明:

KeyPress事件不会由非字符键引发;但是,非字符键确实会引发KeyDown和KeyUp事件。

最新更新