BaseView.KeyDown Event
Fires when a user presses a key while the View has focus.
Namespace: DevExpress.XtraGrid.Views.Base
Assembly: DevExpress.XtraGrid.v26.1.dll
NuGet Packages: DevExpress.Win.Grid, DevExpress.Win.Navigation
Declaration
Event Data
The KeyDown event's data class is KeyEventArgs. The following properties provide information specific to this event:
| Property | Description |
|---|---|
| Alt | Gets a value indicating whether the ALT key was pressed. |
| Control | Gets a value indicating whether the CTRL key was pressed. |
| Handled | Gets or sets a value indicating whether the event was handled. |
| KeyCode | Gets the keyboard code for a KeyDown or KeyUp event. |
| KeyData | Gets the key data for a KeyDown or KeyUp event. |
| KeyValue | Gets the keyboard value for a KeyDown or KeyUp event. |
| Modifiers | Gets the modifier flags for a KeyDown or KeyUp event. The flags indicate which combination of CTRL, SHIFT, and ALT keys was pressed. |
| Shift | Gets a value indicating whether the SHIFT key was pressed. |
| SuppressKeyPress | Gets or sets a value indicating whether the key event should be passed on to the underlying control. |
Remarks
If a user holds a key for a while, the event occurs repeatedly with a short interval. Thus you can handle the event to implement recurrent actions. If you don’t want to process recurrent KeyDown events, set a flag once you’ve performed the desired actions. You will reset this flag in the BaseView.KeyUp event handler when a user releases the key:
bool keyDownReleased = true;
gridView1.KeyDown += (sender, e) =>
{
if (keyDownReleased)
{
//Perform the necessary actions
keyDownReleased = false;
}
};
gridView1.KeyUp += (sender, e) =>
{
keyDownReleased = true;
};
As soon as the KeyDown event has occurred, the View generates a BaseView.KeyPress event if a character key is pressed. The BaseView.KeyPress event does not occur for non-character keys.
If an in-place editor is active, it handles key presses. In this case, the View’s key events do not occur.
The following code deletes the focused row when the end-user presses the Ctrl+Del shortcut.
To process key press events, we handle the BaseView.KeyDown event. The row is deleted by calling the ColumnView.DeleteRow method.
private void gridView1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) {
if (e.KeyCode == Keys.Delete && e.Modifiers == Keys.Control) {
if (MessageBox.Show("Delete row?", "Confirmation", MessageBoxButtons.YesNo) !=
DialogResult.Yes)
return;
GridView view = sender as GridView;
view.DeleteRow(view.FocusedRowHandle);
}
}