15

I want to prevent the MUI X DataGrid multiple checkbox section. When I select the checkbox section, a particular row should be select and the other rows are remain unselected. I tried the disableMultipleSelection option but it would not work.

<DataGrid
  rows={cycle}
  columns={columns}
  pageSize={10}
  checkboxSelection
  disableMultipleSelection
  onRowSelected={({ data, isSelected }) => {
    setDisplay(isSelected);
    setCycleId(data.key);
    setCycleImg(data.storageRef);
  }}
/>          

enter image description here

1
  • Do you have a tutorial, how you build this project? Your project looks huge! I am missing some parts unfortunately. Commented Jun 26, 2021 at 1:00

1 Answer 1

24

To disable multiple row selection, you have to set checkboxSelection props to false. disableMultipleSelection is only available in DataGridPro, not DataGrid.

<DataGrid
  {...props}
  checkboxSelection={false} // or remove it because it's false by default
/>

If you want both selection checkboxes and single row selection, you may need to control the selection state yourself and remove all but the latest selection when the selection model changes:

const [selectionModel, setSelectionModel] = React.useState<GridRowId[]>([]);

return (
  <div style={{ height: 400, width: '100%' }}>
    <DataGrid
      rows={rows}
      columns={columns}
      pageSize={5}
      checkboxSelection
      selectionModel={selectionModel}
      hideFooterSelectedRowCount
      onSelectionModelChange={(selection) => {
        if (selection.length > 1) {
          const selectionSet = new Set(selectionModel);
          const result = selection.filter((s) => !selectionSet.has(s));

          setSelectionModel(result);
        } else {
          setSelectionModel(selection);
        }
      }}
    />
  </div>
);

Live Demo

V5

Codesandbox Demo

V4

Edit 66828464/material-ui-data-grid

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you very much for your valuable time and for this solution. But I am facing an error when I try to implement this code. The Error is exactly showing on the state declaration time i.e An element access expression should take an argument.ts(1011). Can you give any suggestions for this error? ` const [selectionModel, setSelectionModel] = React.useState<GridRowId[]>([]);` The error is showing on GridRowId[] And thank you again for your time and solution.
The codesandbox is forked from the old Material-UI version. I updated the correct type in my answer @Niku1998
RowId[] still showing error. Make 'React.useState<RowId[]>([]);' to 'React.useState([])'
@MrOrhan they changed all the type names in the newer version some time ago. See this PR. Replace RowId with GridRowId should fix it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.