Data Grid - Copy and paste
Copy and paste data using clipboard.
Clipboard copy
You can copy selected grid data to the clipboard using the Ctrl+C (⌘ Command+C on macOS) keyboard shortcut.
The copied cell values are separated by a tab (\t
) character and the rows are separated by a new line (\n
) character.
The priority of the data copied to the clipboard is the following, from highest to lowest:
- If more than one cell is selected (see Cell selection), the selected cells are copied
- If one or more rows are selected (see Row selection), the selected rows are copied
- If there is a single cell selected, the single cell is copied
Clipboard paste
You can paste data from clipboard using the Ctrl+V (⌘ Command+V on macOS) keyboard shortcut.
The paste operation only affects cells in the columns that are editable
.
Same as with editing, you can use valueParser
to modify the pasted value and valueSetter
to update the row with new values.
See Value parser and value setter section of the editing documentation for more details.
The behavior of the clipboard paste operation depends on the selection state of the data grid and the data pasted from clipboard. The priority is the following, from highest to lowest:
- If multiple cells are selected (see Cell selection), the selected cells are updated with the pasted values.
- If one or more rows are selected (see Row selection), the selected rows are updated with the pasted values.
- If a single cell is selected, the values are pasted starting from the selected cell.
Persisting pasted data
Clipboard paste uses the same API for persistence as Editing—use the processRowUpdate
prop to persist the updated row in your data source:
processRowUpdate?: (newRow: R, oldRow: R) => Promise<R> | R;
The row will be updated with a value returned by the processRowUpdate
callback.
If the callback throws or returns a rejected promise, the row will not be updated.
The demo below shows how to persist the pasted data in the browser's sessionStorage
.
Events
The following events are fired during the clipboard paste operation:
clipboardPasteStart
- fired when the clipboard paste operation startsclipboardPasteEnd
- fired when all row updates from clipboard paste have been processed
For convenience, you can also listen to these events using their respective props:
onClipboardPasteStart
onClipboardPasteEnd
Additionally, there is the onBeforeClipboardPasteStart
prop, which is called before the clipboard paste operation starts
and can be used to cancel or confirm the paste operation:
const onBeforeClipboardPasteStart = async () => {
const confirmed = window.confirm('Are you sure you want to paste?');
if (!confirmed) {
throw new Error('Paste operation cancelled');
}
};
<DataGridPremium onBeforeClipboardPasteStart={onBeforeClipboardPasteStart} />;
The demo below uses the Dialog
component for paste confirmation.
If confirmed, the Data Grid displays a loading indicator during the paste operation.
Format of the clipboard data
By default, the clipboard copy and paste operations use the following format:
- The cell values are separated by a tab (
\t
) character. - The rows are separated by a new line (
\n
) character.
You can use clipboardCopyCellDelimiter
and splitClipboardPastedText
props to change the format:
<DataGridPremium
{...otherProps}
// support comma separated values
clipboardCopyCellDelimiter={','}
splitClipboardPastedText={(text) => text.split('\n').map((row) => row.split(','))}
/>
The demo below uses ,
(comma) character as a cell delimiter for both copy and paste operations: