Back to Devexpress

PivotGridControl.CopySelectionToClipboard() Method

wpf-devexpress-dot-xpf-dot-pivotgrid-dot-pivotgridcontrol-a1ee408a.md

latest7.0 KB
Original Source

PivotGridControl.CopySelectionToClipboard() Method

Copies the selected cells to the clipboard.

Namespace : DevExpress.Xpf.PivotGrid

Assembly : DevExpress.Xpf.PivotGrid.v25.2.dll

NuGet Package : DevExpress.Wpf.PivotGrid

Declaration

csharp
public void CopySelectionToClipboard()
vb
Public Sub CopySelectionToClipboard

Remarks

The CopySelectionToClipboard method copies the data displayed in the selected cells to the clipboard. End-users can to this by pressing CTRL+C or CTRL+INS. After cell values are copied to the clipboard, they can be pasted into other applications (e.g. MS Excel, MS Word).

If the PivotGridControl.CopyToClipboardWithFieldValues property is set to true , cell values are copied to the clipboard along with the corresponding field values.

Example

The following example demonstrates how to copy data from the PivotGridControl into the system clipboard.

First, it is necessary to add the ContextMenu with a single “Copy to Clipboard” menu item to a PivotGridControl‘s ContextMenu property. This context menu is invoked when an end-user right-clicks within the Data Area. Then this menu’s Click event can be handled to copy data to the clipboard. To copy the selected cells to the clipboard, the PivotGridControl.CopySelectionToClipboard method is used.

To determine whether the selection is not empty, and so whether it is necessary to invoke the context menu, the PivotGridControl.ContextMenuOpening event should be handled.

xaml
<Window x:Class="HowToBindToMDB.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:dxpg="http://schemas.devexpress.com/winfx/2008/xaml/pivotgrid"
        Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
    <Grid>
        <dxpg:PivotGridControl ContextMenuOpening="pivotGridControl1_ContextMenuOpening"
                               HorizontalAlignment="Left" Name="pivotGridControl1" 
                               VerticalAlignment="Top" 
                               Selection="0,0,3,4">
            <dxpg:PivotGridControl.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="Copy Selection to Clipboard" 
                              Click="CopyToClipboard_Click"/>
                </ContextMenu>
            </dxpg:PivotGridControl.ContextMenu>
            <dxpg:PivotGridControl.Fields>
                <dxpg:PivotGridField Name="fieldCountry" FieldName="Country" 
                                     Area="RowArea" />
                <dxpg:PivotGridField Name="fieldCustomer" FieldName="Sales Person" 
                                     Area="RowArea" Caption="Customer" />
                <dxpg:PivotGridField Name="fieldYear" FieldName="OrderDate" 
                                     Area="ColumnArea" Caption="Year" 
                                     GroupInterval="DateYear" />
                <dxpg:PivotGridField Name="fieldCategoryName" FieldName="CategoryName" 
                                     Area="ColumnArea" Caption="Product Category" />
                <dxpg:PivotGridField Name="fieldProductName" FieldName="ProductName" 
                                     Area="FilterArea" Caption="Product Name" />
                <dxpg:PivotGridField Name="fieldExtendedPrice" FieldName="Extended Price" 
                                     Area="DataArea" CellFormat="c0" />
            </dxpg:PivotGridControl.Fields>
        </dxpg:PivotGridControl>
    </Grid>
</Window>
csharp
using System.Windows;
using System.Windows.Controls;
using HowToBindToMDB.NwindDataSetTableAdapters;

namespace HowToBindToMDB {
    public partial class MainWindow : Window {
        NwindDataSet.SalesPersonDataTable salesPersonDataTable = 
            new NwindDataSet.SalesPersonDataTable();
        SalesPersonTableAdapter salesPersonDataAdapter = new SalesPersonTableAdapter();
        public MainWindow() {
            InitializeComponent();
            pivotGridControl1.DataSource = salesPersonDataTable;
        }
        private void Window_Loaded(object sender, RoutedEventArgs e) {
            salesPersonDataAdapter.Fill(salesPersonDataTable);
        }
        private void CopyToClipboard_Click(object sender, RoutedEventArgs e) {
            pivotGridControl1.CopySelectionToClipboard();
        }
        private void pivotGridControl1_ContextMenuOpening(object sender, ContextMenuEventArgs e) {
            if(pivotGridControl1.Selection.IsEmpty) 
                e.Handled = true;
        }
    }
}
vb
Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Data.OleDb
Imports System.Windows
Imports System.Windows.Controls
Imports DevExpress.Xpf.PivotGrid
Imports HowToBindToMDB.NwindDataSetTableAdapters

Namespace HowToBindToMDB
    Partial Public Class MainWindow
        Inherits Window
        Private salesPersonDataTable As New NwindDataSet.SalesPersonDataTable()
        Private salesPersonDataAdapter As New SalesPersonTableAdapter()

        Public Sub New()
            InitializeComponent()
            pivotGridControl1.DataSource = salesPersonDataTable
        End Sub

        Private Sub Window_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
            salesPersonDataAdapter.Fill(salesPersonDataTable)
        End Sub

        Private Sub CopyToClipboard_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
            pivotGridControl1.CopySelectionToClipboard()
        End Sub

        Private Sub pivotGridControl1_ContextMenuOpening(ByVal sender As Object, _
                                                         ByVal e As ContextMenuEventArgs)
            If pivotGridControl1.Selection.IsEmpty Then
                e.Handled = True
            End If
        End Sub
    End Class
End Namespace

See Also

CopyToClipboardWithFieldValues

PivotGridControl Class

PivotGridControl Members

DevExpress.Xpf.PivotGrid Namespace