Quick and Effective Clipboard Support in the XamGrid 10.3

Kiril Matev / Tuesday, October 19, 2010

One of the most requested features we’ve had for the XamGrid has been clipboard support, and we’re excited to make this feature available in the 10.3 release. Clipboard support makes the XamGrid a true first-class citizen of your application, as this makes it easy for your user to take data in and out of the XamGrid and Excel. Copy-pasting data is fully supported within the XamGrid, and from and to Excel. Watch a video describing in detail this functionality using a sample project.

You can download the sample project (including the extensions) used in the video here. The project has been built with Visual Studio 2010, and Silverlight 4. You will need to download and install NetAdvantage for Silverlight Line of Business 10.3 to run this project. A trial version is freely available here.

Enabling Copy and Paste Support

To enable copy and paste support, you can use the AllowCopy and AllowPaste properties of the ClipboardSettings property of the XamGrid. Here’s a simple example of how both operations can be enabled:

<ig:XamGrid x:Name="xamGrid" … > <ig:XamGrid.ClipboardSettings> <ig:ClipboardSettings AllowCopy="True" AllowPaste="True"/> </ig:XamGrid.ClipboardSettings> </ig:XamGrid>

Copy-Paste Settings

You can set settings to controlling whether headers are included in the copy operations (CopyOptions), or whether only the selected cells or the selected rows will be copied (CopyType).

Clipboard Interaction

When CTRL+C, or CTRL+V is pressed, no default behavior will be invoked in the XamGrid. Instead, in the case of copying, the XamGrid will raise the ClipboardCopyingItem and ClipboardCopying events to allow you to handle copying at the item level, if you require to change certain items formatting before being placed on the clipboard. The specifics of these events are described here. In the case of pasting, the ClipboardPasting event is raised, described here.

Clipboard Extensions

We recognize that however valuable these copy and paste events may be to the implementation of custom scenarios, most of you are working with more or less a standard copy-paste scenario, which uses a certain set of validations when copying and pastes the data in a standard fashion. This is why, we’ve made available clipboard extension methods, which implement standard copy-paste behaviors that you can use immediately in your applications. Of course, if your requirements call for handling copying of items differently, you can implement this specific functionality using the events raised when copying and pasting, as described above. You can get additional information about the clipboard extensions used in the sample project from this blog post.

Copy region validations

The clipboard extensions implement a validation of the region being copied. It only allows rectangular cells, all belonging to a single band to be copied. It displays a message to the user, in case that is not the case. These validation behaviors follow closely how this functionality is implemented in Excel, providing your users with a familiar experience, which will make them more productive using your applications.

Paste region and type validations

When pasting, the extensions include a number of validations, advising the user of incorrect pasting in the following cases:

  • The value to be pasted in a column cannot be converted to the data type of the column
  • The column where a value is to be pasted is read-only
  • The region to the right and below of the active cell is not large enough to accommodate the values to be pasted

All of the text messages are in resource files, so you can easily customize them to provide additional guidance to your users.

Summary

Clipboard support is essential for any grid component. The 10.3 release of the XamGrid provides you with the flexibility to implement your own copy-pasting logic. However, clipboard extensions as described above are freely available for the generic scenarios you are dealing with in your daily work. These extensions implement copy-pasting behaviors and validations following closely how copy-paste validations are implemented in Excel. This allows you to provide your users with a familiar copy-paste experience at no extra cost.