Clipboard Operations
- 4 minutes to read
#Copy to the Clipboard
The GridControl allows you and your users to copy data to the clipboard in multiple formats (CSV, XLS, HTML, RTF, and TXT). The DataControlBase.AllowCopyToClipboard property controls whether to enable copy operations. You can use the DataControlBase.ClipboardCopyFormats property to reduce the number of clipboard formats and improve the operation’s performance.
The GridControl copies its selected rows/cells to the clipboard in the following cases:
- Users press the
Ctrl
+C
orCtrl
+Insert
shortcut. - You call the GridControl.CopyToClipboard method.
Use the DataControlBase.SelectionMode property to allow users to select multiple rows or cells. To add column headers to the clipboard, set the DataControlBase.CopyColumnHeadersToClipboard property to true.
#Copied Data Appearance
The following appearance settings apply to copied data:
- ColumnBase.CellStyleSettings (Background, Foreground, and font settings)
- DataControlBase.RowStyleSettings (Background)
- GridControl.GroupRowStyleSettings (Background)
- DataControlBase.AlternateRowBackground
- Conditional Formatting
The following image shows how the GridControl transfers appearance settings to an Excel document through the clipboard. You can find applied appearance settings in the XAML code below:
<dxg:GridControl ...
CopyColumnHeadersToClipboard="True"
AlternationCount="2" AlternateRowBackground="LightGray">
<dxg:GridControl.FormatConditions>
<dxg:TopBottomRuleFormatCondition FieldName="UnitPrice" Rule="TopItems" Threshold="3"
PredefinedFormatName="{x:Bind dxg:PredefinedFormatNames.LightRedFillWithDarkRedText}"/>
</dxg:GridControl.FormatConditions>
<dxg:GridControl.Columns>
<dxg:GridTextColumn FieldName="ProductName">
<dxg:GridTextColumn.CellStyleSettings>
<dxg:CellStyleSettings Foreground="DarkOrange" FontStyle="Italic"/>
</dxg:GridTextColumn.CellStyleSettings>
</dxg:GridTextColumn>
<dxg:GridSpinEditColumn FieldName="UnitPrice" Mask="c"/>
<dxg:GridCheckBoxColumn FieldName="Discounted"/>
<dxg:GridDateColumn FieldName="OrderDate"/>
</dxg:GridControl.Columns>
</dxg:GridControl>
Set the DataControlBase.ClipboardCopyFormats property to Txt to copy grid data as plain text without appearance settings.
#Handle Copy Operations
The GridControl raises the ClipboardRowCopying event for each copied row. The event considers the column header row as the first copied row if the CopyColumnHeadersToClipboard property is set to true.
This event allows you to modify the copy operation. For example, you can change copied values, skip a row, or customize row appearance.
The following code sample adds group summaries to the copied group rows and changes the background of group rows:
<dxg:GridControl ...
x:Name="grid"
CopyColumnHeadersToClipboard="True"
ClipboardRowCopying="grid_ClipboardRowCopying">
<dxg:GridControl.GroupSummary>
<dxg:GridGroupSummaryItem x:Name="groupSummary" FieldName="UnitPrice" SummaryType="Max"/>
</dxg:GridControl.GroupSummary>
<!-- ... -->
</dxg:GridControl>
using Microsoft.UI;
using Microsoft.UI.Xaml.Media;
// ...
void grid_ClipboardRowCopying(object sender, DevExpress.WinUI.Grid.ClipboardRowCopyingEventArgs e) {
if (e.Type == ClipboardInfoType.Group) {
var group = e.Values[0];
group.Value = group.DisplayValue + $" (Max of Unit Price is ${grid.GetGroupSummaryValue(e.RowHandle, groupSummary)})";
group.Background = new SolidColorBrush(Colors.LightGray);
}
}
#Usage Notes
- The GridControl copies only visible rows/cells. It omits data from hidden columns or rows hidden under collapsed groups.
- For the group rows, the GridControl copies to the clipboard only group values and ignores group summaries.
#Paste from the Clipboard
The GridControl allows you and your users to paste data from the clipboard in two different modes:
In Append mode (DataControlBase.PasteMode is set to Append), the GridControl adds pasted data as new rows:
In Update mode (DataControlBase.PasteMode is set to Update), the GridControl updates existing cells when you paste data:
The GridControl pastes data from the clipboard in the following cases:
- Users press the
Ctrl
+V
orShift
+Insert
shortcut. - You call the GridControl.PasteFromClipboard method.
#Handle Paste Operations
The GridControl raises the ClipboardRowPasting event for each pasted row. You can use this event to change pasted values or exclude a row from the result.
The following code sample handles the event to replace the Yes and No string values pasted into the Discounted column cells with Boolean values:
<dxg:GridControl ...
x:Name="grid"
PasteMode="Append"
ClipboardRowPasting="grid_ClipboardRowPasting">
<!-- ... -->
</dxg:GridControl>
void grid_ClipboardRowPasting(object sender, DevExpress.WinUI.Grid.ClipboardRowPastingEventArgs e) {
var discColumn = grid.Columns["Discounted"];
// Check if a user pastes any data in the Discounted column:
if (e.CellValues.ContainsKey(discColumn)) {
// Check if the pasted value does not match the column's type:
if (!e.IsRowValueValid(discColumn, e.CellValues[discColumn], out Exception exception)) {
if (Equals(e.CellValues[discColumn], "Yes"))
e.CellValues[discColumn] = true;
else if (Equals(e.CellValues[discColumn], "No"))
e.CellValues[discColumn] = false;
else throw exception;
}
}
}
#Usage Notes
- The GridControl skips the Append operation if a data source does not allow users to add new rows.
- The GridControl does not paste data in read-only columns.
- The GridControl omits a row if any of its pasted values do not match the column type.
- Virtual Sources are not supported.