Skip to content

DevExpress-Examples/blazor-grid-custom-keyboard-shortcuts

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Blazor Grid - Custom Keyboard Shortcuts

The DevExpress Blazor Grid supports a number of keyboard shortcuts out-of-the-box. You can also define custom key combinations to further enhance interaction speed and streamline workflows according to business requirements or user preferences.

This example binds custom keyboard shortcuts to the Grid component:

  • Ctrl + A: select all rows
  • Shift + Enter: open row details

Blazor Grid Custom Keyboard Shortcuts

Note

For keyboard shortcuts to operate, the Grid component must be focused. To bring the grid into focus, click anywhere within its area.

Implementation Details

Add DxGrid and DxPopup components to the page. The popup will display the selected cell's details when a user presses Shift + Enter.

Create a JavaScript file in the project. This file implements the following functions for managing keyboard shortcuts:

  • addCaptureKeyListener - Attaches a keyboard event listener to the grid and defines custom shortcuts. To prevent default web browser actions for the same key combinations, call event.stopPropagation(); within the event handler.
  • removeCaptureKeyListener - Removes the previously attached keyboard event listener.
  • focusGrid - Focus the grid programmatically on page load. This ensures shortcuts are instantly available.

Bind JavaScript with .NET Code

In the @code section of the Index.razor page:

  1. Register the JavaScript code in OnAfterRenderAsync lifecycle method.
  2. Call the focusGrid JavaScript function.
  3. Call the addCaptureKeyListener JavaScript function. Pass the DotNetObjectReference (for interoperability from JavaScript to .NET) and a reference to the <div> element that surrounds the grid (for capturing keyboard events.)
  4. Implement SelectAllRows and HandleKeyDown JSInvokable methods to handle operations triggered by keyboard shortcuts.

For technical details, read this article.

Release Resources

In the @code section of the Index.razor page, implement a DisposeAsync method. It removes the keyboard event listener, cleans up JavaScript resources, and frees allocated memory.

For technical details, read this article.

Files to Review

Documentation

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

About

Define custom keyboard shortcuts for the DevExpress Blazor Grid component.

Topics

Resources

License

Stars

Watchers

Forks

Contributors 5