Copying from RadDataGrid

1 Answer 108 Views
DataGrid
Clint
Top achievements
Rank 1
Iron
Iron
Iron
Clint asked on 27 Nov 2023, 10:37 PM

Is there a way to allow copying from the RadDataGrid for the purpose of pasting into another application (Excel, text document, etc.?)

Would it be possible to copy an entire row or would it be restricted to the cell only?

1 Answer, 1 is accepted

Sort by
0
Lance | Senior Manager Technical Support
Telerik team
answered on 28 Nov 2023, 06:52 PM | edited on 28 Nov 2023, 06:53 PM

Hi Clint,

Currently, there is not a built-in "copy/paste" feature to do this. You can see what keys are supported in the Keyboard Support for WinUI article, if you want clipboard features, this belongs to your application rather than the DataGrid.

Options

What you could do instead is to export the desired items into the external format you want. In reality, you're not copying from the DataGrid, but rather using the backing ItemsSource (or SelectedItems) collections.

Once you have the list of objects you want to copy into another application, you have a couple options:

1) Use the native platform's Clipboard APIs

Once you have a selection (or a list of items) that you want to copy/paste, you can use the official clipboard APIs => Clipboard - .NET MAUI | Microsoft Learn.

We do not have an example of this, as this is unrelated to the Telerik controls specifically. Ultimately, the Microsoft .NET MAUI clipboard APIs will interact with the respective data packages you want to copy/paste.  You can open a Feature Request and ask the team to add clipboard support to the control or at least add an official demo that works off the SelectedItems collection.

To get you started, here is what I am referring to:

 

private async Task CopyDataGridSelection()
{
    string jsonString = JsonSerializer.Serialize(DataGrid1.SelectedItems);

    await Clipboard.Default.SetTextAsync(jsonString);
}

 

Important Note: The Microsoft .NET MAUI cross-platform clipboard API only has support for text right now (SetText, GetText and HasText). If you need something more robust that can take a list of objects, rather than a serialized text of that list, then you'll need to write your own helper class and use the WinUI 3 clipboard APIs Copy and paste | Microsoft Learn, here's a demo Windows-universal-samples/Samples/Clipboard

Option 2) Export the data programmatically to Excel

My CRM demo app shows how you can do this with Telerik Document Processing=> https://github.com/telerik/telerik-xamarin-forms-samples/blob/671b6c7292f2566e353d9351868f4402592067fc/ArtGalleryCRM/ArtGalleryCRM.Forms/ViewModels/OrderViewModels/OrdersViewModel.cs#L123-L143.

The trick is to have a source list of items (your ItemsSource or SelectedItems collections) and then create an Excel file from it. My code iterates over each item, inserts the value into a spreadsheet cell, then finally exports an xlsx file.

Paste

The other side of what you need to consider is how the receiving application accepts data form the clipboard. If it's an app that understands serialized text and then deserializes it (Excel can do this), then you're good to go.

Wrapping Up

Although it feels simple when using copy/paste, it is not always an easy thing to implement. You will need to design a plan for when the selection is copied and how it is going to be received by the other app.

I hope this helps point you in a direction that you want to take.

Regards,
Lance | Manager Technical Support
Progress Telerik

A brand new .NET MAUI course was just added to the Virtual Classroom. The training course is developed to help you get started with the Telerik UI for .NET MAUI components and features. It aims to put you in the shoes of an engineer who adds new features to an existing application. You can check it out at https://learn.telerik.com
Clint
Top achievements
Rank 1
Iron
Iron
Iron
commented on 28 Nov 2023, 07:01 PM

Lance,

Thanks. I felt like this was going to be the case, but wanted to make sure before I started writing something on my own.

Tags
DataGrid
Asked by
Clint
Top achievements
Rank 1
Iron
Iron
Iron
Answers by
Lance | Senior Manager Technical Support
Telerik team
Share this question
or