DataGridViewCell trap CTRL+Up or CTRL+Down

99 Views Asked by At

C# form has a datagridview. I need to trap an event when CTRL+UP Arrow or CTRL+Down Arrow is pressed in a cell in edit mode.

Not sure which event to handle and how it should be handled.

1

There are 1 best solutions below

4
jsanalytics On BEST ANSWER

Handle the KeyUp event for your DataGridView like this :

private void dataGridView1_KeyUp(object sender, KeyEventArgs e)
{
    if (e.Control && e.KeyCode == Keys.Up)
        MessageBox.Show(string.Format("Ctrl+{0}",e.KeyCode.ToString()));
    else if (e.Control && e.KeyCode == Keys.Down)
        MessageBox.Show(string.Format("Ctrl+{0}", e.KeyCode.ToString()));
}

Adding the print screen i get when i actually run the code:

enter image description here

From MSDN:

The conditional-AND operator (&&) performs a logical-AND of its bool operands, but only evaluates its second operand if necessary.

https://msdn.microsoft.com/en-us/library/2a723cdk%28v=vs.140%29.aspx

The & operator can function as either a unary or a binary operator.

https://msdn.microsoft.com/en-us/library/sbf85k1c%28v=vs.140%29.aspx

Because both operands are bool in my code, && is the preferred option, although i tested with & and it worked as well. Also && is more efficient because it tests the second operand only if necessary.