Data Grid - Column pinning
Pin columns to keep them visible at all time.
Pinned (or frozen, locked, or sticky) columns are columns that are visible at all time while the user scrolls the grid horizontally. They can be pinned either to the left or right side and cannot be reordered.
To pin a column, there are a few ways:
- Using the
initialState
prop - Controlling the
pinnedColumns
andonPinnedColumnsChange
props - Dedicated buttons in the column menu
- Accessing the imperative API
To set pinned columns via initialState
, pass an object with the following shape to this prop:
interface GridPinnedColumns {
left?: string[]; // Optional field names to pin to the left
right?: string[]; // Optional field names to pin to the right
}
The following demos illustrates how this approach works:
<DataGridPro
rows={rows}
columns={columns}
initialState={{ pinnedColumns: { left: ['name'], right: ['actions'] } }}
/>
Note: The column pinning feature can be completely disabled with disableColumnPinning
.
<DataGridPro disableColumnPinning />
You may encounter issues if the sum of the widths of the pinned columns is larger than the width of the grid. Make sure that the grid can accommodate properly, at least, these columns.
Controlling the pinned columns
While the initialState
prop only works for setting pinned columns during the initialization, the pinnedColumns
prop allows you to modify which columns are pinned at any time.
The value passed to it follows the same shape from the previous approach.
Use it together with onPinnedColumnsChange
to know when a column is pinned or unpinned.
<Alert severity="info" style={{ marginBottom: 8 }}>
<code>pinnedColumns: {JSON.stringify(pinnedColumns)}</code>
</Alert>
<div style={{ height: 400, width: '100%' }}>
<DataGridPro
rows={rows}
columns={columns}
pinnedColumns={pinnedColumns}
onPinnedColumnsChange={handlePinnedColumnsChange}
/>
</div>
Blocking column unpinning
It may be desirable to not allow a column to be unpinned. The only thing required to achieve that is to hide the buttons added to the column menu. This can be done in two ways:
Per column, by setting
pinnable
tofalse
in eachGridColDef
:<DataGrid columns={[{ field: 'id', pinnable: false }]} /> // Default is `true`.
By providing a custom menu, as demonstrated below:
<DataGridPro
rows={rows}
columns={columns}
components={{ ColumnMenu: CustomColumnMenu }}
initialState={{ pinnedColumns: { left: ['name'], right: ['actions'] } }}
/>
Note: Using the disableColumnMenu
prop also works, however, you disable completely the column menu with this approach.
Pinning the checkbox selection column
To pin the checkbox column added when using checkboxSelection
, add GRID_CHECKBOX_SELECTION_COL_DEF.field
to the list of pinned columns.
<DataGridPro
rows={rows}
columns={columns}
checkboxSelection
initialState={{
pinnedColumns: {
left: [GRID_CHECKBOX_SELECTION_COL_DEF.field],
right: ['actions'],
},
}}
/>
getPinnedColumns()
Returns which columns are pinned.
Signature:
getPinnedColumns: () => GridPinnedColumns
isColumnPinned()
Returns which side a column is pinned to.
Signature:
isColumnPinned: (field: string) => GridPinnedPosition | false
pinColumn()
Pins a column to the left or right side of the grid.
Signature:
pinColumn: (field: string, side: GridPinnedPosition) => void
setPinnedColumns()
Changes the pinned columns.
Signature:
setPinnedColumns: (pinnedColumns: GridPinnedColumns) => void
unpinColumn()
Unpins a column.
Signature:
unpinColumn: (field: string) => void