Data Grid - Column dimensions
Customize the dimensions and resizing behavior of your columns.
Column width
By default, the columns have a width of 100px.
This is an arbitrary, easy-to-remember value.
To change the width of a column, use the width
property available in GridColDef
.
Minimum width
By default, the columns have a minimum width of 50px.
This is an arbitrary, easy-to-remember value.
To change the minimum width of a column, use the minWidth
property available in GridColDef
.
Fluid width
Column fluidity or responsiveness can be achieved by setting the flex
property in GridColDef
.
The flex
property accepts a value between 0 and ∞.
It works by dividing the remaining space in the data grid among all flex columns in proportion to their flex
value.
For example, consider a grid with a total width of 500px that has three columns: the first with width: 200
; the second with flex: 1
; and the third with flex: 0.5
.
The first column will be 200px wide, leaving 300px remaining. The column with flex: 1
is twice the size of flex: 0.5
, which means that final sizes will be: 200px, 200px, 100px.
To set a minimum and maximum width for a flex
column set the minWidth
and the maxWidth
property in GridColDef
.
Resizing
By default, Data Grid allows all columns to be resized by dragging the right portion of the column separator.
To prevent the resizing of a column, set resizable: false
in the GridColDef
.
Alternatively, to disable all columns resize, set the prop disableColumnResize={true}
.
To restrict resizing a column under a certain width set the minWidth
property in GridColDef
.
To restrict resizing a column above a certain width set the maxWidth
property in GridColDef
.
To capture changes in the width of a column there are two callbacks that are called:
onColumnResize
: Called while a column is being resized.onColumnWidthChange
: Called after the width of a column is changed, but not during resizing.
Autosizing
Data Grid allows to autosize the columns' dimensions based on their content. Autosizing is enabled by default. To turn it off, pass the disableAutosize
prop to the Data Grid.
Autosizing can be used by one of the following methods:
- Adding the
autosizeOnMount
prop, - Double-clicking a column header separator on the grid,
- Calling the
apiRef.current.autosizeColumns(options)
API method.
You can pass options directly to the API method when you call it. To configure autosize for the other two methods, provide the options in the autosizeOptions
prop.
Note that for the separator double-click method, the autosizeOptions.columns
will be replaced by the respective column user double-clicked on.
In all the cases, the colDef.minWidth
and colDef.maxWidth
options will be respected.
<DataGrid
{...otherProps}
autosizeOptions={{
columns: ['name', 'status', 'createdBy'],
includeOutliers: true,
includeHeaders: false,
}}
/>
Autosizing asynchronously
The autosizeColumns
method from the apiRef
can be used as well to adjust the column size on specified events, for example when receiving row data from the server.
Autosizing with dynamic row height
Column autosizing is compatible with the Dynamic row height feature.
Autosizing with grouped rows
When using row grouping you can utilize the autosizeColumns
method to adjust the column width of the expanded rows dynamically.
The demo below shows how you can subscribe to the rowExpansionChange
event. The provided handler function then calls the autosizeColumns
method from the gridApi.