dfgv
dfgv

Reputation: 97

up and down key not firing keypress event textbox in datagridview cell

i am tryint to popup one selection box on datagridview cell keypress event .selection box popup working but up and down key not firing i want to move down and up selection box data while pressing up and down key from datagridview cell

these code am using

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

}

Upvotes: 1

Views: 3593

Answers (2)

Ria
Ria

Reputation: 10347

Try to use DataGridView.CellEnter event:

Occurs when the current cell changes in the DataGridView control or when the control receives input focus.

or DataGridView.CellLeave event

Occurs when a cell loses input focus and is no longer the current cell.

Upvotes: 1

Jason Whitted
Jason Whitted

Reputation: 4024

Use the keydown or keyup events instead.

Control.KeyPress documentation states:

The KeyPress event is not raised by noncharacter keys; however, the noncharacter keys do raise the KeyDown and KeyUp events.

Upvotes: 1

Related Questions