RadGridView exposes several useful properties and events, which can help you control the keyboard interaction and get notified when keyboard events occur. Moreover, using the keyboard you can perform some common tasks, such as:
- Change the selection - use the arrow keys to change your selection. To select multiple rows hold the Shift\Ctrl key pressed and use the arrow keys or the mouse to select the desired rows.
- Sort by multiple columns - just hold the Shift key pressed and click all column headers by which you wish to sort your data.
- Start cell edit - press F2 and the current cell will enter in edit mode.
- End cell edit - press Enter while the cell is in edit mode. This will confirm the changes you've made.
- Cancel cell edit - press Escape while the cell is in edit mode. This will rollback the changes you've made.
- Add new row - press Insert and new row will be inserted automatically.
- Delete existing row - users can delete (if supported by the grid ItemsSource) selected items using DELETE key. This feature can be controlled with RadGridView's CanUserDeleteRows property.
- Set the IsTabStop property to include/exclude the control in the tab navigation cycle. If this property is set to True, then the control will be included; if it is False it will be skipped.
- The TabIndex property defines the index of the control in the tab navigation cycle. The lower the number is, the earlier the control will be focused while navigating using the Tab key. If you set this property, do not forget to set IsTabStop to True.
- If you are looking for a better way to control how the tab navigation cycles inside the RadGridView, use the property TabNavigation. You can set it to one of these three possible values:
- KeyboardNavigationMode.Cycle – depending on the direction of the navigation, when the end or the beginning of the container is reached the focus returns to the first or the last item. The focus cannot leave the container using logical navigation.
- KeyboardNavigationMode.Once – the container and all of its child elements as a whole receive focus only once. Either the first tree child or the last focused element in the group receives focus.
- KeyboardNavigationMode.Local – tab Indexes are considered on local subtree only inside this container and navigation leaves the containing element when an edge is reached.
- KeyboardNavigationMode.Contained - depending on the direction of the navigation, focus returns to the first or the last item when the end or the beginning of the container is reached, but does not move past the beginning or end of the container.
- KeyboardNavigationMode.Continue - each element receives keyboard focus, as long as it is a navigation stop.Navigation leaves the containing element when an edge is reached.
Here is a list of the common keyboard events exposed by the RadGridView, GridViewRow and GridViewCell objects:
- KeyUp - occurs when the user releases a keyboard key. The type of the passed event arguments is KeyEventArgs.
- KeyDown - occurs when the user presses a keyboard key. The type of the passed event arguments is KeyEventArgs.
In the example below you can see how to attach to KeyDown and KeyUp events and how to specify a value for the TabNavigation property from your XAML.
<telerik:RadGridView x:Name="radGridView" TabNavigation="Local" KeyDown="radGridView_KeyDown" KeyUp="radGridView_KeyUp"/>
|It is always a good practice to attach your event handlers in the XAML, whenever your application logic allows this.|
The implementation of both event handlers radGridView_KeyDown and radGridView_KeyUp is located in the code-behind file (C# or VB.NET) and looks like this:
private void radGridView_KeyDown( object sender, KeyEventArgs e )
MessageBox.Show( "The pressed key is: " + e.Key.ToString() );
private void radGridView_KeyUp( object sender, KeyEventArgs e )
MessageBox.Show( "The released key is: " + e.Key.ToString() );
Private Sub radGridView_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
MessageBox.Show("The pressed key is: " & e.Key.ToString())
Private Sub radGridView_KeyUp(ByVal sender As Object, ByVal e As KeyEventArgs)
MessageBox.Show("The released key is: " & e.Key.ToString())
You can attach to the other keyboard events in the same way.