This is a mirror of official site: http://jasper-net.blogspot.com/

Implementing Copy & Paste for WPF DataGrid .NET 4

| Sunday, August 28, 2011
Introduction

Using the standard DataGrid that was added with .net4, I found that I needed to implement copy and paste.  Searching the internet came up with solutions that either worked with Silverlight or with some sort of pre-release version that you could add to Visual Studio 2008 (.net3).

To my horror, I had to actually figure something out and write some code.  I'm very new to WPF and have spent the last few months copying example code off the internet and modifying it for my use but I think I have finally written something that might be of use to other people so here it is.

Credit goes to the following two pages that helped and some code is taken from the second one (credited in comments) -

http://weblogs.asp.net/manishdalal/archive/2008/11/12/cross-browser-copy-and-paste-in-datagrid-with-excel-support-part-1.aspx

http://blogs.msdn.com/b/vinsibal/archive/2008/09/25/pasting-content-to-new-rows-on-the-wpf-datagrid.aspx

The solution allows you to do some limited copy and pasting between cells.  In the example, I have have a table of people with First name, Second name and Legs (a number - usually 2).  In the application I was working on, it made no sense to copy from one column to another.  So in this example you can't copy the number of legs column to the first name column (for example).  This is intentional and the restriction could be removed easily by deleting some checks.

Another restriction (not so intentional!) is that it only deals with strings or integers. It should be easy to expand on the types allowed.

Using the code

I will go over some of the code you have to copy into your project but you'll have to see the entire solution to get all the required code.  This is a taster to show you the main principles so you can decide if it's for you.

I intend to write another version where I inherit from DataGrid and manipulate the DataGrid content directly rather than in the view model.  This will make it much easier to re-use.

The DataGrid is declared like this:

<DataGrid Name="dataGrid1" ItemsSource="{Binding PeopleList}"
                  SelectionUnit="Cell"
                  AutoGenerateColumns="True"
                  KeyDown="dataGrid1_KeyDown"
                  CopyingRowClipboardContent="dataGrid1_CopyingRowClipboardContent"
                  Loaded="dataGrid1_Loaded" />

Note that the SelectionUnit is set to Cell. This allows individual cells to be selected rather than the whole row.  The items are taken from an ObserverableCollection and the columns are automatically generated.

The KeyDown event is set to detect the paste operation:

    void dataGrid1_KeyDown(object sender, KeyEventArgs e)
    {
      if (e.Key == Key.V &&
          (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
      {
        if (ValidatePaste() == false)
          return;

        viewModel.Paste(dataGrid1.CurrentColumn.DisplayIndex,
             GetTargetRow(),
             dataGrid1.SelectedCells.Count,
             Clipboard.GetData(DataFormats.Text) as string);
      }
    }

As you can see, a View Model is used.

Read more: Codeproject
QR: DataGrid_Copy_and_Paste.aspx

Posted via email from Jasper-net

0 comments: