Skip to main content

Copy Data to the Clipboard

  • 2 minutes to read

PivotGridControl allows end-users to select multiple data cells using a mouse. Data displayed in selected cells can be copied to the Clipboard and pasted into other applications (e.g. MS Excel, MS Word). To do this, an end-user should use the CTRL+C or CTRL+INS keys.

Selected cells can also be copied to the Clipboard via code. Use the PivotGridCells.CopySelectionToClipboard method to do this.

If the PivotGridOptionsBehaviorBase.CopyToClipboardWithFieldValues option is set to true, cell values are copied to the Clipboard along with corresponding field values.

Example: How to Copy Data to the Clipboard

Assume that the PopupMenu component has already been placed on a form. It has a single menu item - “Copy to Clipboard”. This context menu is invoked when an end-user right-clicks within the Data Area. The item’s ItemClick event is handled to copy data to the Clipboard. To copy the selected cells to the Clipboard the PivotGridCells.CopySelectionToClipboard method is used.

To determine whether an end-user has right-clicked within the Data Area, the PivotGridControl.CalcHitInfo method is used. It returns the PivotGridHitInfo object whose PivotGridHitInfo.HitTest property identifies whether the cell has been clicked.

The image below shows the result.

cdCopyClpBrd

using DevExpress.XtraPivotGrid;
// ...
private void Form1_Load(object sender, EventArgs e) {
    BarButtonItem btnCopyToClipboard = new BarButtonItem(barManager1, "Copy to Clipboard");
    btnCopyToClipboard.Tag = pivotGridControl1;
    popupMenu1.AddItem(btnCopyToClipboard);
    btnCopyToClipboard.ItemClick += BtnCopyToClipboard_ItemClick;
}

private void BtnCopyToClipboard_ItemClick(object sender, ItemClickEventArgs e) {
    PivotGridControl pivot = (e.Item.Tag as PivotGridControl);
    pivot.Cells.CopySelectionToClipboard();
}

private void pivotGridControl1_MouseUp(object sender, MouseEventArgs e) {
    PivotGridControl pivot = sender as PivotGridControl;
    Point pt = new Point(e.X, e.Y);
    if (e.Button != MouseButtons.Right) return;
    if (pivot.CalcHitInfo(pt).HitTest != PivotGridHitTest.Cell) return;
    // Shows the context menu.
    popupMenu1.ShowPopup(pivot.PointToScreen(pt));
}