• North American Sales: 1-800-231-8588
  • Global Contacts
  • My Account
Infragistics Infragistics
Menu
  • North American Sales: 1-800-321-8588
  • My Account
    • Sign In/Register
  • Design & DevelopmentDesign & Develop
    • Best Value
      Infragistics Ultimate The complete toolkit for building high performing web, mobile and desktop apps.
      Indigo.Design Use a unified platform for visual design, UX prototyping, code generation and application development.
    • Web
      Ignite UI for Angular Ignite UI for JavaScript Ignite UI for React Ultimate UI for ASP.NET Indigo.Design
    • Desktop
      Ultimate UI for Windows Forms Ultimate UI for WPF
      Prototyping
      Indigo.Design
    • Mobile
      Ultimate UI for Xamarin Ultimate UI for iOS Ultimate UI for Android
    • Automated Testing Tools
      Test Automation for Micro Focus UFT: Windows Forms Test Automation for Micro Focus UFT: WPF Test Automation for IBM RFT: Windows Forms
  • UX
    • Indigo.Design Desktop Collaborative prototyping and remote usability testing for UX & usability professionals
    • Indigo.Design A Unified Platform for Visual Design, UX Prototyping, Code Generation, and App Development
  • Business Intelligence
    • Reveal Embedded Accelerate your time to market with powerful, beautiful dashboards into your apps
    • Reveal App Empower everyone in your organization to use data to make smarter business decisions
  • Team Productivity
  • Learn & Support Support
    • Help & Support Documents
    • Blogs
    • Forums
    • Product Ideas
    • Reference Applications
    • Customer Stories
    • Webinars
    • eBook & Whitepapers
    • Events
  • Free Trials
  • Pricing
    • Product Pricing / Buy Online
    • Renew Existing License
    • Contact Us
WPF
  • Product Platforms
  • More
WPF
WPF XamDataGrid :: Copying to Excel via the Clipboard
  • Blog
  • Files
  • Wiki
  • Mentions
  • Tags
  • More
  • Cancel
  • New
WPF requires membership for participation - click to join
  • WPF
  • Configuring the XamTab Control
  • Creating a Custom Summary for the XamDataGrid
  • Defining a Custom Path in the XamCarousel
  • Enabling Row Summaries in the XamDataGrid
  • Exporting the XamDataGrid to Excel
  • Hosting a WPF Control in a Windows Forms Application
  • Printing the XamDataGrid with Infragistics.Reports
  • Spell Checking in the XamDataGrid
  • Tangerine -- A WPF Reference Application
  • Using the Infragistics WinGrid in a WPF Application
  • Validation in the XamDataGrid
  • XamDataGrid :: Copying to Excel via the Clipboard
  • XML Databinding with the XamDataGrid

XamDataGrid :: Copying to Excel via the Clipboard

One of the common tasks that many end-users have is to copy data from an application to Excel or Word or any other Windows Apps.  The way to do this is to copy items to the Clipboard and Microsoft provides a really nice class that makes this simple to do.  The point of this article is to offer a brief glimpse on how to copy cell values from the XamDataGrid to the clipboard.  It will focus on three different selection strategies range select (only for contiguous ranges), record selection (rows), or field selections (columns).

Handling Selection in the XamDataGrid

To enable selection in the above three scenarios you may also want to make some modifications to the XamDataGrid that allow for the user to easily select a range of cells and allow them to select all of the cells in a field by clicking on a field header.  The code to do this looks like this:

<igDP:XamDataGrid.FieldSettings>
	<igDP:FieldSettings LabelClickAction="SelectField" CellClickAction="SelectCell" />
</igDP:XamDataGrid.FieldSettings>

 

Managing Ctrl+C

To ensure that the XamDataGrid responds to Ctrl+C you need to add input bindings to the XamDataGrid that point to that particular keystroke.  To do this you need to find a way to point the CTRL+C keystroke to an event or command that fires off the XamDataGrid.  The code to handle this gesture looks like this:

<igDP:XamDataGrid.InputBindings>
    <KeyBinding Command="{x:Static myApp:Window1.CopyCommand}" Gesture="CTRL+C" />
</igDP:XamDataGrid.InputBindings>

 

Setting up the Copy Command

In the procedural code you now need to define this copy command.  To do so, you need to declare it:

public static RoutedCommand CopyCommand = new RoutedCommand();

 

 

I hooked up this command in the constructor for our Window:

public Window1()
{
    CommandBinding copyCommandBinding = 
		new CommandBinding(CopyCommand, ExecutedCopyCommand);
    this.CommandBindings.Add(copyCommandBinding);
    InitializeComponent();
}

 

After you have the command bindings set up you can handle the ExecutedCopyCommand and this is what will actually be called by the CTRL+C method.  In this method, I look at the XamDataGrid that calls the command and check to see which of the SelectedItemsCollections hold items and call a method to handle them based on the actual selected collection:

private void ExecutedCopyCommand(object sender, ExecutedRoutedEventArgs e)
{
    Infragistics.Windows.DataPresenter.XamDataGrid myGrid = 
		e.Source as Infragistics.Windows.DataPresenter.XamDataGrid;

    if (myGrid.SelectedItems.Cells.Count > 0)
        CopySelectedCells(myGrid);
    else if (myGrid.SelectedItems.Records.Count > 0)
        CopySelectedRecords(myGrid);
    else if (myGrid.SelectedItems.Fields.Count > 0)
        CopySelectedFields(myGrid);

}

 

Copy a Range of Cells

This method will enable the end-user to select a range of cells and format them appropriately to copy their text to clipboard.  This formatting also enables the copying to be direct to rows/columns in Excel.  One caveat is that it only allows for a contiguous range of cells (as this is the only capabilities enabled by Excel):

private static void CopySelectedCells(Infragistics.Windows.DataPresenter.XamDataGrid myGrid)
{
    string myInternalClipboard = null;
    int currentRecordLevel = 0;
    Infragistics.Windows.DataPresenter.SelectedCellCollection mySelectedCells = myGrid.SelectedItems.Cells;

    foreach (Cell myCell in mySelectedCells)
    {
        if (myInternalClipboard == null)
        {
            currentRecordLevel = myCell.Record.DataItemIndex;
            myInternalClipboard = myCell.Value.ToString();
        }
        else if (myCell.Record.DataItemIndex == currentRecordLevel)
            myInternalClipboard = myInternalClipboard + "\t" + myCell.Value.ToString();
        else
        {
            currentRecordLevel = myCell.Record.DataItemIndex;
            myInternalClipboard = myInternalClipboard + "\r\n" + myCell.Value.ToString();
        }
    }
    myInternalClipboard = myInternalClipboard + "\r\n";
    Clipboard.Clear();
    Clipboard.SetText(myInternalClipboard);
}

 

Copy a Range of Records

This method allows the end-user to select a record(row) or a range of rows and copy them to the clipboard and then past them to a particular app while still distinguishing different columns:

private static void CopySelectedRecords(Infragistics.Windows.DataPresenter.XamDataGrid myGrid)
{
    string myInternalClipboard = null;
    bool IsNewRecord = true;
    Infragistics.Windows.DataPresenter.SelectedRecordCollection mySelectedRecords = 
		myGrid.SelectedItems.Records;

    foreach (DataRecord myRecord in mySelectedRecords)
    {
        foreach(Cell myCell in myRecord.Cells)
        {
            if (myInternalClipboard == null || IsNewRecord == true)
            {
                myInternalClipboard = myInternalClipboard + myCell.Value.ToString();
                IsNewRecord = false;
            }
            else
                myInternalClipboard = myInternalClipboard + "\t" + myCell.Value.ToString();
        }
        IsNewRecord = true;
        myInternalClipboard = myInternalClipboard + "\r\n";
    }
    Clipboard.Clear();
    Clipboard.SetText(myInternalClipboard);
}

 

Copy a Range of Fields

This allows the end-user to select a column or a range of columns and copy those values to the clipboard.  It was interesting to implement as when you select a field, you’re not actually selecting the cells in that field, but the entire field.  So, you actually have to loop through all the records and build your clipboard string based on the parameters of the selected fields:

private static void CopySelectedFields(Infragistics.Windows.DataPresenter.XamDataGrid myGrid)
{
    string myInternalClipboard = null;
    bool IsNewRecord = true;
    Infragistics.Windows.DataPresenter.SelectedFieldCollection mySelectedFields = 
		myGrid.SelectedItems.Fields;

    foreach (DataRecord myRecord in myGrid.Records)
    {
        foreach (Field myField in mySelectedFields)
        {
            if (myInternalClipboard == null || IsNewRecord == true)
            {
                myInternalClipboard = myInternalClipboard + myRecord.Cells[myField].Value.ToString();
                IsNewRecord = false;
            }
            else
            {
                myInternalClipboard = myInternalClipboard + "\t" + myRecord.Cells[myField].Value.ToString();
            }
        }
        IsNewRecord = true;
        myInternalClipboard = myInternalClipboard + "\r\n";
    }
    Clipboard.Clear();
    Clipboard.SetText(myInternalClipboard);
}

 

Conclusion

That’s it.  After adding this simple code to your application, you can now allow your end-users to copy from the XamDataGrid to an Excel file or to any Windows Application.  You can download the code for this example here. 

  • WPF
  • XamDataGrid
  • Excel
  • Share
  • History
  • More
  • Cancel
Related
Recommended