|
1 | | -# How to improve the performance for clipboard paste operation |
2 | | -This example demonstrates that how to improve the performance for clipboard paste operation |
| 1 | +# How to Improve the Performance of Clipboard Paste Operation in WinForms GridControl? |
| 2 | + |
| 3 | +This example demonstrates that how to improve the performance for clipboard paste operation in [WinForms GridControl](https://www.syncfusion.com/winforms-ui-controls/grid-control). |
| 4 | + |
| 5 | +Pasting performance can be improved by using the [Model.ClipboardPaste](https://help.syncfusion.com/cr/windowsforms/Syncfusion.Windows.Forms.Grid.GridModel.html#Syncfusion_Windows_Forms_Grid_GridModel_ClipboardPaste) event and pasting the copied text manually for the selected range. This method of pasting avoids the iteration of cells while getting the values from the Clipboard. |
| 6 | + |
| 7 | +``` csharp |
| 8 | +//Event Triggering |
| 9 | +this.Model.ClipboardPaste += model_ClipboardPaste; |
| 10 | + |
| 11 | +//Event Customization |
| 12 | +public void model_ClipboardPaste(object sender, GridCutPasteEventArgs e) |
| 13 | +{ |
| 14 | + GridRangeInfoList rangeList; |
| 15 | + GridModel model = sender as GridModel; |
| 16 | + model.Selections.GetSelectedRanges(out rangeList, true); |
| 17 | + GridRangeInfo range = rangeList.GetOuterRange(rangeList.ActiveRange); |
| 18 | + //Getting data from clipboard. |
| 19 | + string psz = GetClipboardText(); |
| 20 | + this.PasteTextFromBuffer(psz, range, e.ClipboardFlags); |
| 21 | + e.Handled = true; |
| 22 | +} |
| 23 | + |
| 24 | +private string GetClipboardText() |
| 25 | +{ |
| 26 | + string buffer = null; |
| 27 | + IDataObject iData = null; |
| 28 | + if (GridUtil.IsSet(this.Model.CutPaste.ClipboardFlags, GridDragDropFlags.Styles | GridDragDropFlags.Text)) |
| 29 | + { |
| 30 | + iData = Clipboard.GetDataObject(); |
| 31 | + } |
| 32 | + if (GridUtil.IsSet(this.Model.CutPaste.ClipboardFlags, GridDragDropFlags.Text) |
| 33 | + && iData != null) |
| 34 | + { |
| 35 | + if (iData.GetDataPresent(DataFormats.UnicodeText)) |
| 36 | + { |
| 37 | + buffer = iData.GetData(DataFormats.UnicodeText) as string; |
| 38 | + } |
| 39 | + else if (iData.GetDataPresent(DataFormats.Text)) |
| 40 | + { |
| 41 | + buffer = iData.GetData(DataFormats.Text) as string; |
| 42 | + } |
| 43 | + } |
| 44 | + return buffer; |
| 45 | +} |
| 46 | + |
| 47 | +private bool PasteTextFromBuffer(string psz, GridRangeInfo range, int dragDropFlags) |
| 48 | +{ |
| 49 | + bool canceled = false; |
| 50 | + |
| 51 | + OperationFeedback op = new OperationFeedback(Model); |
| 52 | + try |
| 53 | + { |
| 54 | + op.AllowRollback = true; |
| 55 | + op.AllowNestedProgress = false; |
| 56 | + |
| 57 | + int rowIndex, colIndex; |
| 58 | + |
| 59 | + Model.ConfirmChanges(); |
| 60 | + Model.CommandStack.BeginTrans("Paste"); |
| 61 | + |
| 62 | + rowIndex = range.Top; |
| 63 | + colIndex = range.Left; |
| 64 | + |
| 65 | + |
| 66 | + int nLastCol = colIndex; |
| 67 | + int size = psz.Length; |
| 68 | + |
| 69 | + try |
| 70 | + { |
| 71 | + string[] copiedValue = psz.Split(new[] { "\r\n" }, StringSplitOptions.None); |
| 72 | + |
| 73 | + for (int i = 0; i < copiedValue.Length; i++) |
| 74 | + { |
| 75 | + string[] value = copiedValue[i].Split(new[] { "\t" }, StringSplitOptions.None); |
| 76 | + |
| 77 | + for (int j = 0; j < value.Length; j++) |
| 78 | + { |
| 79 | + GridStyleInfo style = null; |
| 80 | + style = Model[rowIndex, colIndex]; |
| 81 | + this.Model.TextDataExchange.PasteTextRowCol(rowIndex, colIndex, value[j]); |
| 82 | + colIndex++; |
| 83 | + } |
| 84 | + rowIndex++; |
| 85 | + colIndex = range.Left; |
| 86 | + } |
| 87 | + if (size > 0) |
| 88 | + { |
| 89 | + op.PercentComplete = (int)((rowIndex * colIndex) * 100 / size); |
| 90 | + } |
| 91 | + |
| 92 | + if (op.ShouldCancel) |
| 93 | + { |
| 94 | + throw new GridUserCanceledException(); |
| 95 | + } |
| 96 | + } |
| 97 | + catch (GridUserCanceledException ex) |
| 98 | + { |
| 99 | + TraceUtil.TraceExceptionCatched(ex); |
| 100 | + if (!ExceptionManager.RaiseExceptionCatched(this, ex)) |
| 101 | + { |
| 102 | + throw; |
| 103 | + } |
| 104 | + |
| 105 | + canceled = true; |
| 106 | + } |
| 107 | + |
| 108 | + if (canceled && op.RollbackConfirmed) |
| 109 | + { |
| 110 | + Model.CommandStack.Rollback(); |
| 111 | + } |
| 112 | + else |
| 113 | + { |
| 114 | + Model.CommandStack.CommitTrans(); |
| 115 | + } |
| 116 | + //// Also formula refresh cells that have references to the pasted cells. |
| 117 | + Model.Refresh(); |
| 118 | + |
| 119 | + return !canceled; |
| 120 | + } |
| 121 | + finally |
| 122 | + { |
| 123 | + op.Close(); |
| 124 | + Model.EndUpdate(); |
| 125 | + } |
| 126 | +} |
| 127 | +``` |
| 128 | + |
| 129 | +Take a moment to peruse the WinForms [GridControl - Clipboard support](https://help.syncfusion.com/windowsforms/grid-control/copy-and-paste#paste) documentation, where you can find about GridControl clipboard operation with code examples. |
0 commit comments