'Material UI Datagrid Sticky Header

Is it possible to make the Material UI Datagrid Header sticky? Seems this is possible with the Material UI Tables. I am not able to find an option to do it with a data grid.



Solution 1:[1]

As mentioned in comments, it is a default behavior. However few mistakes can be made to prevent this default functionality. In my case it was property that I've missed: autoHeight={true}, it should be false or just remove it.

Here is an extensive code example and bellow there is link to SandBox:

import { DataGrid, GridToolbar } from "@mui/x-data-grid";
import React from "react";

export default function AdvanceTable() {
  
  const [pageSize, setPageSize] = React.useState(5);
  const [editRowsModel, setEditRowsModel] = React.useState({});

  const handleEditRowsModelChange = React.useCallback((model) => {
    // Set current edited cell
    setEditRowsModel(model);
  });

  return (
    <div>
      <div style={{ width: "100%", height: "80vh" }}>
        <DataGrid
          components={{
            Toolbar: GridToolbar
          }}
          rows={rows}
          columns={columns}
          disableSelectionOnClick
          checkboxSelection
          onEditRowsModelChange={handleEditRowsModelChange}
          pagination
          pageSize={pageSize}
          onPageSizeChange={(newPageSize) => setPageSize(newPageSize)}
          rowsPerPageOptions={[5, 10, 15]}
        />
      </div>
      <div style={{ marginBottom: 8 }}>
        <code>Edited table values: {JSON.stringify(editRowsModel)}</code>
      </div>
    </div>
  );
  
  // mock data:
  const columns = [
    { field: "id", headerName: "ID", width: 70 },
    {
      field: "firstName",
      headerName: "First name",
      editable: true,
      width: 180
    },
    { field: "lastName", headerName: "Last name", editable: true, width: 180 },
    {
      field: "age",
      headerName: "Age",
      type: "number",
      editable: true,
      width: 150
    }
  ];

  const rows = [
    { id: 1, lastName: "Snow", firstName: "Jon", age: 35 },
    { id: 2, lastName: "Lannister", firstName: "Cersei", age: 42 },
    { id: 3, lastName: "Lannister", firstName: "Jaime", age: 45 },
    { id: 4, lastName: "Stark", firstName: "Arya", age: 16 },
    { id: 5, lastName: "Targaryen", firstName: "Daenerys", age: null },
    { id: 6, lastName: "Melisandre", firstName: null, age: 150 },
    { id: 7, lastName: "Clifford", firstName: "Ferrara", age: 44 },
    { id: 8, lastName: "Frances", firstName: "Rossini", age: 36 },
    { id: 9, lastName: "Roxie", firstName: "Harvey", age: 65 },
    { id: 41, lastName: "Stark", firstName: "Arya", age: 16 },
    { id: 51, lastName: "Targaryen", firstName: "Daenerys", age: null },
    { id: 61, lastName: "Melisandre", firstName: null, age: 150 },
    { id: 71, lastName: "Clifford", firstName: "Ferrara", age: 44 },
    { id: 81, lastName: "Frances", firstName: "Rossini", age: 36 },
    { id: 91, lastName: "Roxie", firstName: "Harvey", age: 65 }
  ];
}

SandBox example

Solution 2:[2]

It seems like the official solution is to either specify a fixed-height, or to place the table inside a flex container.

Neither of these are great options if you want to place the table part-way down a scrollable page/container, as you'll likely end up with a nested scrollbar.

Typically in this case you want to use autoHeight to display the full grid, and have a true position: "sticky" header like you would with a regular HTML <table>.

DataGrid doesn't support this out-the-box at time of writing, but you can still achieve it by tweaking the internal .css classes. I do want to add a disclaimer that this solution overwrites a few internal layout properties, so there's a risk this could break in future versions.

export const StickyDataGrid = styled(DataGrid)(({theme}) => ({
    '& .MuiDataGrid-columnHeaders': {
        position: "sticky",
        // Replace background colour if necessary
        backgroundColor: theme.palette.background.paper,
        // Display header above grid data, but below any popups
        zIndex: theme.zIndex.mobileStepper - 1,
    },
    '& .MuiDataGrid-virtualScroller': {
        // Undo the margins that were added to push the rows below the previously fixed header
        marginTop: "0 !important"
    },
    '& .MuiDataGrid-main': {
        // Not sure why it is hidden by default, but it prevented the header from sticking
        overflow: "visible"
    }
}))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Mike Rippon