Clipboard Extensions for xamGrid

Nikolay Zhekov / Tuesday, October 19, 2010

Starting from 2010.3 XamGrid has Clipboard support.

You can read in more details about this new feature in our documentation or get a head start by watching this video.

In this post I will show you few helper methods that you can use for copying and pasting. Links to the source code and binaries are below.

There are two public extension methods available: PasteData and IsSelectionValid.

The purpose of the IsSelectionValid extension method is to check whether the selection made by the user is valid. Selection is considered as valid if the selected cells form a rectangular region in a single band. This behavior mimics excel, where you can only copy a rectangular region.

For example the following selections are not valid:

You can use it like that in ClipboardCopying event handler:

private void XwGridClipboardCopying(object sender, ClipboardCopyingEventArgs e)
{
	XamGrid xamGrid = sender as XamGrid;

	if (xamGrid != null && !xamGrid.IsSelectionValid())
	{
		MessageBox.Show("Only rectangular single band regions are allowed.");
		e.Cancel = true;
	}
}

To paste data on the XamGrid use the PasteData method in ClipboardPasting event handler:

private void XwGridClipboardPasting(object sender, ClipboardPastingEventArgs e)
{
	XamGrid xamGrid = sender as XamGrid;

	if (xamGrid != null)
	{
		xamGrid.PasteData(e.Values);
	}
}

If a value can't be converted to the target type a message box will be displayed to warn the user.

Source and binaries: