Hello,
I'm aware of a solution for pasting into an empty SfDataGrid in UWP here: https://www.syncfusion.com/kb/12129/how-to-paste-copied-rows-to-the-empty-uwp-datagrid-sfdatagrid . However, I cannot seem to find a solution for the WinForms SfDataGrid. I want to be able to paste multiple rows into an empty SfDataGrid without adding an empty row beforehand. I am already using this solution (https://www.syncfusion.com/kb/4812/how-to-add-the-copied-rows-as-new-rows-in-the-sfdatagrid-while-pasting ) to paste multiple rows into a non-empty grid but when empty the paste events don't seem to fire.
Is this possible? Any help would be appreciated.
Thanks,
Robert
Hi Robert van der Leek,
Your requirement of pasting multiple rows into an empty SfDataGrid can be
achieved by programmatically calling the customized PasteTextToRows
function within the PreviewKeyDown event handler of SfDataGrid.TableControl.
Refer to the below code snippet,
//Event Subscription sfDataGrid1.TableControl.PreviewKeyDown += OnPreviewKeyDown;
//Event customization private void OnPreviewKeyDown(object sender, PreviewKeyDownEventArgs e) { //Check SfDataGrid is empty or not if (sfDataGrid1.View != null && sfDataGrid1.View.Records != null && sfDataGrid1.View.Records.Count == 0) { //Check paste short cut key is pressed or not if ((Control.ModifierKeys == Keys.Control || Control.ModifierKeys == Keys.ControlKey || Control.ModifierKeys == Keys.LControlKey || Control.ModifierKeys == Keys.RControlKey) && e.KeyCode == Keys.V) { //Paste the rows in empty SfDataGrid by using the PasteTextToRows method (this.sfDataGrid1.ClipboardController as CustomClipboardController).PasteTextToRows(); return; } } } |
Code snippet related to customization of DataGridClipboardController:
//Using the Custom CustomClipboardController. this.sfDataGrid1.ClipboardController = new CustomClipboardController(this.sfDataGrid1);
//Customize the DataGridClipboardController public class CustomClipboardController : DataGridClipboardController { SfDataGrid dataGrid; public CustomClipboardController(SfDataGrid dataGrid) : base(dataGrid) { this.dataGrid = dataGrid; }
protected override void PasteToRows(object clipboardRows) { //Check SfDataGrid is empty or not, Otherwise perform the default paste operations in SfDataGrid if (this.dataGrid.View != null && this.dataGrid.View.Records != null && this.dataGrid.View.Records.Count() == 0) { var copiedRecord = (string[])clipboardRows; int copiedRecordsCount = copiedRecord.Count(); //Based on the clipboard count added the new record to be pasted. if (copiedRecordsCount > 0) { for (int i = 0; i < copiedRecordsCount; i++) { // Creates new record. this.dataGrid.View.AddNew(); for (int j = 0; j < this.dataGrid.Columns.Count; j++) { var record = copiedRecord[i]; string[] splitRecord = Regex.Split(record, @"\t");
//Adds the new record by using the PasteToCell method, by passing the created data, particular column, and clipboard value. this.PasteToCell(this.dataGrid.View.CurrentAddItem, this.dataGrid.Columns[j], splitRecord[j]); }
// Commits the pasted record. this.dataGrid.View.CommitNew(); } } } else base.PasteToRows(clipboardRows); }
//Customize method for pasting rows in empty SfDataGrid public void PasteTextToRows() { IDataObject dataObject = null; dataObject = Clipboard.GetDataObject(); if (dataObject.GetDataPresent(DataFormats.UnicodeText)) { var clipboardContent = dataObject.GetData(DataFormats.UnicodeText) as string; if (clipboardContent == null) return;
var copiedRecords = Regex.Split(clipboardContent.ToString(), @"\r\n"); if (dataGrid.PasteOption.HasFlag(PasteOptions.ExcludeFirstLine)) copiedRecords = copiedRecords.Skip(1).ToArray(); PasteToRows(copiedRecords); } } } |
Find the sample in the attachment.
Note:
By default, the events SfDataGrid.PasteContent and SfDataGrid.PasteCellContent
only listens for UI interactions, such as keyboard and mouse actions. The
SfDataGrid does not directly support pasting multiple rows into an empty
SfDataGrid. However, we have achieved this functionality by programmatically
calling the customized PasteTextToRows method. As a result, there is no
notification generated for paste events. It is worth noting that the PasteContent
and PasteCellContent events are triggered solely by UI interactions.
Regards,
Vijayarasan S
If this post is helpful, please consider Accepting it as the solution so that other members can locate it more quickly.
This is perfect, thank you Vijayarasan!
Robert
van der Leek,
We are glad to know that the reported problem has been resolved at your end.
Please let us know if you have any further queries on this. We are happy to
help you😊.