Copy Operations
- 3 minutes to read
The GridControl allows copying data displayed within a grid’s view to the Clipboard. This topic consists of the following sections:
- Copying At Runtime
- Copying In Code
- Copying With Formatting
- Copying Customization
- Processing Copy Operations
Copying At Runtime
End-users can copy the GridControl’s selected data to the clipboard using the Ctrl+C or Ctrl+Insert shortcuts. This data can then be pasted in another control or application (for example, MS Word, Excel, etc.).
The following image shows a copy operation from the GridControl to the Excel:
The copy operations have the following specifics:
- Only the visible columns’ content is copied.
- Only the grouping columns’ values are copied for group rows. Hidden rows in collapsed group rows are not included.
Copying In Code
End-users can copy only selected rows or cells. However, the GridControl provides the following methods that allow you to copy any visible rows or cells:
Method | Description |
---|---|
DataControlBase.CopyRowsToClipboard | Copies values displayed within the specified rows/cards to the clipboard. |
DataControlBase.CopyRangeToClipboard | Copies values displayed within the specified row/card range to the clipboard. |
DataControlBase.CopySelectedItemsToClipboard | Copies values displayed within the selected rows to the clipboard. |
DataControlBase.CopyCurrentItemToClipboard | Copies values displayed within the focused row to the clipboard. |
DataControlBase.CopyToClipboard | Copies values displayed within the selected rows or cells to the clipboard. |
TableView.CopyCellsToClipboard | Copies values displayed within the specified cells to the clipboard. |
TableView.CopySelectedCellsToClipboard | Copies values displayed within the selected cells to the clipboard. |
The following code sample demonstrates how to copy all visible rows to the clipboard:
<dxg:GridControl Name="grid" />
<Button Click="OnClick">Copy to Clipboard</Button>
void OnClick(object sender, RoutedEventArgs e) {
grid.CopyRangeToClipboard(0, grid.VisibleRowCount - 1);
}
Copying With Formatting
Row values are copied to the clipboard as text by default:
- The newline strings (“\r\n”) separate rows in the clipboard.
- The TAB character (“\t”) separates cell values within a row.
The GridControl allows end-users to copy data and its format settings (fonts, cell background, and foreground) to the clipboard. When pasting this data in MS Word, MS Excel, or MS Outlook, the data retains its original formatting:
Set the DataViewBase.ClipboardMode property to Formatted to specify whether copying data and format settings is enabled:
<dxg:GridControl>
<dxg:GridControl.View>
<dxg:TableView ClipboardMode="Formatted" />
</dxg:GridControl.View>
</dxg:GridControl>
Copying Customization
You can use the following properties to customize copy operations:
Property | Description |
---|---|
DataControlBase.ClipboardCopyMode | Gets or sets how data is copied to the clipboard. |
DataViewBase.ClipboardCopyOptions | Specifies formats (Csv, Excel, Html, Rtf, Txt) the data copied from this Grid Control is compatible with. |
GridViewBase.ClipboardCopyMaxRowCountInServerMode | Gets or sets the maximum number of rows/cards that can be copied to the clipboard in Server Mode. |
The following code sample demonstrates how to copy data without column headers. This data is compatible with the Csv and Excel formats:
<dxg:GridControl ClipboardCopyMode="ExcludeHeader">
<dxg:GridControl.View>
<dxg:TableView ClipboardCopyOptions="Csv, Excel" />
</dxg:GridControl.View>
</dxg:GridControl>
Processing Copy Operations
The GridControl provides the GridControl.CopyingToClipboard event that allows you to process copy operations manually. This event is raised before row values are copied to the clipboard by an end-user or in code.
The following code sample demonstrates how to process data that is copied to the clipboard:
<dxg:GridControl CopyingToClipboard="OnCopyingToClipboard" />
void OnCopyingToClipboard(object sender, CopyingToClipboardEventArgs e) {
Clipboard.Clear();
// You should manually implement the GetRowData() method
// that returns data to be copied to the clipboard.
Clipboard.SetData(DataFormats.Text, GetRowData());
e.Handled = true;
}
The GridControl.CopyingToClipboard event is raised only if the GridControl does not have an active cell editor. Use the DataViewBase.ActiveEditor property to detect whether the GridControl contains an active cell editor.
Note
The GridControl.CopyingToClipboard event is not raised if the DataControlBase.ClipboardCopyMode property is set to ClipboardCopyMode.None.