windowsforms-devexpress-dot-xtragrid-dot-views-dot-base-dot-baseview-1a858366.md
Fires when a user presses a key while the View has focus.
Namespace : DevExpress.XtraGrid.Views.Base
Assembly : DevExpress.XtraGrid.v25.2.dll
NuGet Packages : DevExpress.Win.Grid, DevExpress.Win.Navigation
[DXCategory("Key")]
public event KeyEventHandler KeyDown
<DXCategory("Key")>
Public Event KeyDown As KeyEventHandler
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. |
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;
};
Dim keyDownReleased As Boolean = True
AddHandler gridView1.KeyDown, Sub(sender, e)
If keyDownReleased Then
'Perform the necessary actions
keyDownReleased = False
End If
End Sub
AddHandler gridView1.KeyUp, Sub(sender, e)
keyDownReleased = True
End Sub
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);
}
}
Private Sub GridView1_KeyDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) Handles GridView1.KeyDown
If (e.KeyCode = Keys.Delete And e.Modifiers = Keys.Control) Then
If (MessageBox.Show("Delete row?", "Confirmation", _
MessageBoxButtons.YesNo) <> DialogResult.Yes) Then Return
Dim view As GridView = CType(sender, GridView)
view.DeleteRow(view.FocusedRowHandle)
End If
End Sub
The following code snippet (auto-collected from DevExpress Examples) contains a reference to the KeyDown event.
Note
The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.
winforms-grid-multiple-row-selection-web-style-checkboxes/CS/E1271/CheckMarkSelection.cs#L133
view.CustomUnboundColumnData += new CustomColumnDataEventHandler(view_CustomUnboundColumnData);
view.KeyDown += new KeyEventHandler(view_KeyDown);
view.RowStyle += new RowStyleEventHandler(view_RowStyle);
winforms-grid-multiple-row-selection-web-style-checkboxes/VB/E1271/CheckMarkSelection.vb#L161
AddHandler view.CustomUnboundColumnData, New CustomColumnDataEventHandler(AddressOf view_CustomUnboundColumnData)
AddHandler view.KeyDown, New KeyEventHandler(AddressOf view_KeyDown)
AddHandler view.RowStyle, New RowStyleEventHandler(AddressOf view_RowStyle)
See Also