Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

804
Views
Multiple row selection on checkbox and single selection on clicking the row in MUI DataGrid

I'm using MUI DataGrid version 4 component.

What I want are:

  1. to enable multiple selections from the checkbox in the Data Grid (if the user select multiple rows using the checkbox)
  2. to disable multiple selections from the row in the Data Grid (if the user selects multiple rows)
  3. to enable single selection from the row in the Data Grid (if the user selects another row after selecting a row).

But what I get are:

  1. multiple selections from the checkbox are enabled (what I want)
  2. multiple selections from the row are enabled (what I don't want).

For the row selection, I want a single selection only like this answer MUI - Disable multiple row selection in DataGrid but with the multiple selections from the checkbox is enabled.

Here's my code: https://codesandbox.io/s/material-ui-data-grid-selection-vq1ny

Here is the documentation for the selection on the Data Grid component.

Note: it's okay to use DataGridPro component.

Please let me know if the question is not clear.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can control the selectionModel state and check if the user clicks the cell (not the checkbox). If they click the checkbox, onCellClick won't be fired, if they click other cells, onCellClick will fire, then based on that info to update the selectionModel accordingly:

const cellClickRef = React.useRef(null);
const [selectionModel, setSelectionModel] = React.useState([]);

return (
  <div style={{ height: 400, width: '100%' }}>
    <DataGrid
      checkboxSelection
      selectionModel={selectionModel}
      onCellClick={() => (cellClickRef.current = true)}
      onSelectionModelChange={(selection, detail) => {
        if (cellClickRef.current) {
          if (selection.length > 1) {
            const selectionSet = new Set(selectionModel);
            const result = selection.filter((s) => !selectionSet.has(s));

            setSelectionModel(result);
          } else {
            setSelectionModel(selection);
          }
        } else {
          setSelectionModel(selection);
        }
        cellClickRef.current = null;
      }}
      {...data}
    />
  </div>
);

Codesandbox Demo

Related answer

  • MUI - Disable multiple row selection in DataGrid
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!