Skip to main content
A newer version of this page is available. .

BaseView.KeyPress Event

Fires when a character key is pressed while the View has focus.

Namespace: DevExpress.XtraGrid.Views.Base

Assembly: DevExpress.XtraGrid.v19.2.dll

Declaration

[DXCategory("Key")]
public event KeyPressEventHandler KeyPress

Event Data

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

Property Description
Handled Gets or sets a value indicating whether the KeyPress event was handled.
KeyChar Gets or sets the character corresponding to the key pressed.

Remarks

When pressing character a key, the KeyPress event is raised. If holding the key pressed for a while, the event is raised repeatedly with a short interval. Thus, you can handle the event to implement repeated actions. However, if you do not want to perform repeated actions, you need to have a flag indicating whether the desired actions have already been performed. This flag must be reset in the BaseView.KeyUp event handler. The BaseView.KeyUp event is raised when the pressed key is released.

The KeyPress event is raised immediately after the BaseView.KeyDown event if a character key is pressed. If pressing a non-character key, only the BaseView.KeyDown event is rased.

If an inplace editor is active, it processes key presses. In this case, the View’s key events are not raised.

Example

The following example shows how to prevent numeric characters (‘0’-‘9’) from being entered within a grid’s CustomerID column. Two events are handled to prevent entering these characters:

  • the BaseView.KeyPress event - fires when an alpha numeric character is pressed while an in-place editor is not active;
  • the grid control’s EditorContainer.EditorKeyPress event - fires when an alpha numeric character is pressed while an in-place editor is active.

If a specific character needs to be discarded in these event handlers, the Handled parameter must be set to true.

using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;

// Fires when no in-place editor is active
private void gridView1_KeyPress(object sender, KeyPressEventArgs e) {
    GridView view = sender as GridView;
    string s = "0123456789";
    if (view.FocusedColumn.FieldName == "CustomerID" && s.IndexOf(e.KeyChar) >= 0)
        e.Handled = true;
}
// Fires when an in-place editor is active
private void gridControl1_EditorKeyPress(object sender, KeyPressEventArgs e) {
    GridControl grid = sender as GridControl;
    gridView1_KeyPress(grid.FocusedView, e);
}
See Also