Skip to content

Data grid - Events

You can subscribe to the events emitted by the data grid to trigger custom behavior.

Subscribing to events

You can subscribe to one of the events emitted by providing an event handler to the grid.

The handler is a method that will be called with three arguments:

  1. the parameters containing the information related to the event
  2. the MuiEvent containing the DOM event or the React synthetic event, when available
  3. the GridCallbackDetails containing the GridApi, only if DataGridPro or DataGridPremium is being used

For example, here is an event handler for the rowClick event:

const handleEvent: GridEventListener<'rowClick'> = (
  params, // GridRowParams
  event, // MuiEvent<React.MouseEvent<HTMLElement>>
  details, // GridCallbackDetails
) => {
  setMessage(`Movie "${params.row.title}" clicked`);
};

You can provide this event handler to the grid in several ways:

With the prop of the event

<DataGrid onRowClick={handleEvent} {...other} />

The following demo shows how to subscribe to the rowClick event using the onRowClick prop—try it out by clicking on any row:

Title
Gross
Company
Director
Year
Cinematic Universe
Avatar
$2,847,246,203
20th Century Fox
James Cameron
2,009
Avengers: Endgame
$2,797,501,328
Disney Studios
Anthony & Joe Russo
2,019
Marvel Cinematic Universe
Titanic
$2,187,425,379
20th Century Fox
James Cameron
1,997
Star Wars: The Force Awakens
$2,068,223,624
Disney Studios
J. J. Abrams
2,015
Star Wars
Avengers: Infinity War
$2,048,359,754
Disney Studios
Anthony & Joe Russo
2,018
Marvel Cinematic Universe
Spider-Man: No Way Home
$1,892,768,346
Disney Studios
Jon Watts
2,021
Marvel Cinematic Universe
Jurassic World
$1,671,713,208
Universal Pictures
Colin Trevorrow
2,015
Jurassic Park

Rows per page:

1–35 of 35

With useGridApiEventHandler

useGridApiEventHandler('rowClick', handleEvent);

The following demo shows how to subscribe to the rowClick event using useGridApiEventHandler—try it out by clicking on any row:

Title
Gross
Company
Director
Year
Cinematic Universe
Avatar
$2,847,246,203
20th Century Fox
James Cameron
2,009
Avengers: Endgame
$2,797,501,328
Disney Studios
Anthony & Joe Russo
2,019
Marvel Cinematic Universe
Titanic
$2,187,425,379
20th Century Fox
James Cameron
1,997
Star Wars: The Force Awakens
$2,068,223,624
Disney Studios
J. J. Abrams
2,015
Star Wars
Avengers: Infinity War
$2,048,359,754
Disney Studios
Anthony & Joe Russo
2,018
Marvel Cinematic Universe
Spider-Man: No Way Home
$1,892,768,346
Disney Studios
Jon Watts
2,021
Marvel Cinematic Universe
Jurassic World
$1,671,713,208
Universal Pictures
Colin Trevorrow
2,015
Jurassic Park
The Lion King
$1,656,943,394
Disney Studios
Jon Favreau
2,019

Rows per page:

1–35 of 35

With apiRef.current.subscribeEvent

apiRef.current.subscribeEvent('rowClick', handleEvent);

The following demo shows how to subscribe to the rowClick event using apiRef.current.subscribeEvent—try it out by clicking on any row:

Title
Gross
Company
Director
Year
Cinematic Universe
Avatar
$2,847,246,203
20th Century Fox
James Cameron
2,009
Avengers: Endgame
$2,797,501,328
Disney Studios
Anthony & Joe Russo
2,019
Marvel Cinematic Universe
Titanic
$2,187,425,379
20th Century Fox
James Cameron
1,997
Star Wars: The Force Awakens
$2,068,223,624
Disney Studios
J. J. Abrams
2,015
Star Wars
Avengers: Infinity War
$2,048,359,754
Disney Studios
Anthony & Joe Russo
2,018
Marvel Cinematic Universe
Spider-Man: No Way Home
$1,892,768,346
Disney Studios
Jon Watts
2,021
Marvel Cinematic Universe
Jurassic World
$1,671,713,208
Universal Pictures
Colin Trevorrow
2,015
Jurassic Park
Total Rows: 35

Disabling the default behavior

Depending on the use case, it might be necessary to disable the default action taken by an event. The MuiEvent passed to the event handler has a defaultMuiPrevented property to control when the default behavior can be executed or not. Set it to true to block the default handling of an event and implement your own.

<DataGrid
  onCellClick={(params: GridCellParams, event: MuiEvent<React.MouseEvent>) => {
    event.defaultMuiPrevented = true;
  }}
/>

Usually, double clicking a cell will put it into edit mode. The following example changes this behavior by also requiring Ctrl to be pressed.

No rows
Desk
Commodity
Trader Name
Trader Email
Quantity

Rows per page:

0–0 of 0

Press Enter to start editing

Catalog of events

Expand the rows to see how to use each event.

Name
Available on
Description
aggregationModelChange

Fired when the aggregation model changes.

cellClick

Fired when a cell is clicked.

cellDoubleClick

Fired when a cell is double-clicked.

cellEditCommit

Fired when the props of the edit input are committed.

cellEditStart

Fired when the cell turns to edit mode.

cellEditStop

Fired when the cell turns back to view mode.

cellFocusIn

Fired when a cell gains focus.

cellFocusOut

Fired when a cell loses focus.

cellKeyDown

Fired when a keydown event happens in a cell.

cellModesModelChange

Fired when the model that controls the cell modes changes.

cellMouseDown

Fired when a mousedown event happens in a cell.

cellMouseUp

Fired when a mouseup event happens in a cell.

columnHeaderClick

Fired when a column header is clicked

columnHeaderDoubleClick

Fired when a column header is double-clicked.

columnHeaderKeyDown

Fired when a key is pressed in a column header. It's mapped do the keydown DOM event.

columnOrderChange

Fired when the user ends reordering a column.

columnResize

Fired during the resizing of a column.

columnResizeStart

Fired when the user starts resizing a column.

columnResizeStop

Fired when the user stops resizing a column.

columnsChange

Fired when the columns state is changed.

columnVisibilityChange

Fired when a column visibility changes. It is not fired when the columnVisibilityModel is controlled or initialized. It is not fired when toggling all column's visibility at once.

columnVisibilityModelChange

Fired when the column visibility model changes.

columnWidthChange

Fired when the width of a column is changed.

componentError

Fired when an exception is thrown in the grid.

debouncedResize

Fired when the grid is resized with a debounced time of 60ms.

editCellPropsChange

Fired when the props of the edit cell changes.

editRowsModelChange

Fired when the row editing model changes.

fetchRows

Fired when a new batch of rows is requested to be loaded. Called with a GridFetchRowsParams object.

filterModelChange

Fired when the filter model changes.

headerSelectionCheckboxChange

Fired when the value of the selection checkbox of the header is changed

menuClose

Fired when the grid menu is closed.

menuOpen

Fired when the menu is opened.

pageChange

Fired when the page changes.

pageSizeChange

Fired when the page size changes.

preferencePanelClose

Fired when the preference panel is closed.

preferencePanelOpen

Fired when the preference panel is opened.

renderedRowsIntervalChange

Fired when the rendered rows index interval changes. Called with a GridRenderedRowsIntervalChangeParams object.

resize

Fired when the grid is resized.

rowClick

Fired when a row is clicked. Not fired if the cell clicked is from an interactive column (actions, checkbox, etc).

rowDoubleClick

Fired when a row is double-clicked.

rowEditCommit

Fired when the props of the edit input are committed.

rowEditStart

Fired when the row turns to edit mode.

rowEditStop

Fired when the row turns back to view mode.

rowGroupingModelChange

Fired when the row grouping model changes.

rowModesModelChange

Fired when the model that controls the row modes changes.

rowMouseEnter

Fired when the mouse enters the row. Called with a GridRowParams object.

rowMouseLeave

Fired when the mouse leaves the row. Called with a GridRowParams object.

rowOrderChange

Fired when the user ends reordering a row.

rowSelectionCheckboxChange

Fired when the value of the selection checkbox of a row is changed

rowsScroll

Fired during the scroll of the grid viewport.

rowsScrollEnd

Fired when scrolling to the bottom of the grid viewport.

selectionChange

Fired when the selection state of one or multiple rows changes.

sortModelChange

Fired when the sort model changes.

stateChange

Fired when the state of the grid is updated.

unmount

Fired when the grid is unmounted.

viewportInnerSizeChange

Fired when the inner size of the viewport changes. Called with an ElementSize object.