Back to Devexpress

GridControl.ProcessGridKey Event

windowsforms-devexpress-dot-xtragrid-dot-gridcontrol-573a022a.md

latest7.6 KB
Original Source

GridControl.ProcessGridKey Event

Enables you to process key presses before they are processed by the grid control’s focused View and the active in-place editor.

Namespace : DevExpress.XtraGrid

Assembly : DevExpress.XtraGrid.v25.2.dll

NuGet Packages : DevExpress.Win.Grid, DevExpress.Win.Navigation

Declaration

csharp
[DXCategory("Grid")]
public event KeyEventHandler ProcessGridKey
vb
<DXCategory("Grid")>
Public Event ProcessGridKey As KeyEventHandler

Event Data

The ProcessGridKey event's data class is KeyEventArgs. The following properties provide information specific to this event:

PropertyDescription
AltGets a value indicating whether the ALT key was pressed.
ControlGets a value indicating whether the CTRL key was pressed.
HandledGets or sets a value indicating whether the event was handled.
KeyCodeGets the keyboard code for a KeyDown or KeyUp event.
KeyDataGets the key data for a KeyDown or KeyUp event.
KeyValueGets the keyboard value for a KeyDown or KeyUp event.
ModifiersGets the modifier flags for a KeyDown or KeyUp event. The flags indicate which combination of CTRL, SHIFT, and ALT keys was pressed.
ShiftGets a value indicating whether the SHIFT key was pressed.
SuppressKeyPressGets or sets a value indicating whether the key event should be passed on to the underlying control.

Remarks

To process key presses within a View, use the events provided by View objects (BaseView.KeyPress, BaseView.KeyDown and BaseView.KeyUp). Note that these events do not fire when a cell’s in-place editor is active.

To handle key presses within in-place editors, use dedicated events provided by in-place editors (RepositoryItem.KeyPress, RepositoryItem.KeyDown and RepositoryItem.KeyUp) or by the GridControl (EditorContainer.EditorKeyPress, EditorContainer.EditorKeyDown and EditorContainer.EditorKeyUp).

The ProcessGridKey event is designed for advanced cases that are not covered by these events.

The ProcessGridKey event is raised when the user presses a key but before this key press is processed by the grid control. After your ProcessGridKey event handler is complete, the current key press will, by default, be sent to the focused View or to the active in-place editor. To prevent this, set the Handled parameter to true.

Example

The following example shows how to use the GridControl.ProcessGridKey event to delete the selected record(s) in the currently focused View when the CTRL+DELETE shortcut is pressed.

csharp
private void gridControl1_ProcessGridKey(object sender, KeyEventArgs e) {
    ColumnView view = (sender as GridControl).FocusedView as ColumnView;
    if (view == null) return;
    if (e.KeyCode == Keys.Delete && e.Control && view.Editable && view.SelectedRowsCount > 0) {
        if (view.ActiveEditor != null) return; //Prevent record deletion when an in-place editor is invoked
        e.Handled = true;
        if (DevExpress.XtraEditors.XtraMessageBox.Show("Record deletion", "Delete?", MessageBoxButtons.YesNo) == DialogResult.Yes)
            view.DeleteSelectedRows();
    }
}
vb
Private Sub GridControl1_ProcessGridKey(sender As Object, e As KeyEventArgs) Handles GridControl1.ProcessGridKey
    Dim view As ColumnView = TryCast(TryCast(sender, GridControl).FocusedView, ColumnView)
    If view Is Nothing Then
        Return
    End If
    If e.KeyCode = Keys.Delete AndAlso e.Control AndAlso view.Editable AndAlso view.SelectedRowsCount > 0 Then
        'Prevent record deletion when an in-place editor is invoked:
        If view.ActiveEditor IsNot Nothing Then
            Return
        End If
        e.Handled = True
        If DevExpress.XtraEditors.XtraMessageBox.Show("Record deletion", "Delete?", MessageBoxButtons.YesNo) = DialogResult.Yes Then
            view.DeleteSelectedRows()
        End If
    End If
End Sub

See Also

KeyPress

FocusedView

GridControl Class

GridControl Members

DevExpress.XtraGrid Namespace