Articles in this section
Category / Section

How to improve the performance of clipboard paste operation in WinForms GridControl?

2 mins read

Performance of the Clipboard paste operation


Pasting performance can be improved by using the Model.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.

//Event Triggering
this.Model.ClipboardPaste += model_ClipboardPaste;
 
//Event Customization
public void model_ClipboardPaste(object sender, GridCutPasteEventArgs e)
{    
    GridRangeInfoList rangeList;
    GridModel model = sender as GridModel;
    model.Selections.GetSelectedRanges(out rangeList, true);
    GridRangeInfo range = rangeList.GetOuterRange(rangeList.ActiveRange);
    //Getting data from clipboard.
    string psz = GetClipboardText();
    this.PasteTextFromBuffer(psz, range, e.ClipboardFlags);
    e.Handled = true;
}
 
private string GetClipboardText()
{
    string buffer = null;
    IDataObject iData = null;
    if (GridUtil.IsSet(this.Model.CutPaste.ClipboardFlags, GridDragDropFlags.Styles | GridDragDropFlags.Text))
    {
        iData = Clipboard.GetDataObject();
    }
    if (GridUtil.IsSet(this.Model.CutPaste.ClipboardFlags, GridDragDropFlags.Text)
                    && iData != null)
    {
        if (iData.GetDataPresent(DataFormats.UnicodeText))
        {
            buffer = iData.GetData(DataFormats.UnicodeText) as string;
        }
        else if (iData.GetDataPresent(DataFormats.Text))
        {
            buffer = iData.GetData(DataFormats.Text) as string;
        }
    }
    return buffer;
}
 
private bool PasteTextFromBuffer(string psz, GridRangeInfo range, int dragDropFlags)
{
    bool canceled = false;
 
    OperationFeedback op = new OperationFeedback(Model);
    try
    {
        op.AllowRollback = true;
        op.AllowNestedProgress = false;
 
        int rowIndex, colIndex;
 
        Model.ConfirmChanges();
        Model.CommandStack.BeginTrans("Paste");
 
        rowIndex = range.Top;
        colIndex = range.Left;
 
 
        int nLastCol = colIndex;
        int size = psz.Length;
 
        try
        {
            string[] copiedValue = psz.Split(new[] { "\r\n" }, StringSplitOptions.None);
 
            for (int i = 0; i < copiedValue.Length; i++)
            {
                string[] value = copiedValue[i].Split(new[] { "\t" }, StringSplitOptions.None);
 
                for (int j = 0; j < value.Length; j++)
                {
                    GridStyleInfo style = null;
                    style = Model[rowIndex, colIndex];
                    this.Model.TextDataExchange.PasteTextRowCol(rowIndex, colIndex, value[j]);
                    colIndex++;
                }
                rowIndex++;
                colIndex = range.Left;
            }
            if (size > 0)
            {
                op.PercentComplete = (int)((rowIndex * colIndex) * 100 / size);
            }
 
            if (op.ShouldCancel)
            {
                throw new GridUserCanceledException();
            }
        }
        catch (GridUserCanceledException ex)
        {
            TraceUtil.TraceExceptionCatched(ex);
            if (!ExceptionManager.RaiseExceptionCatched(this, ex))
            {
                throw;
            }
 
            canceled = true;
        }
 
        if (canceled && op.RollbackConfirmed)
        {
            Model.CommandStack.Rollback();
        }
        else
        {
            Model.CommandStack.CommitTrans();
        }
        //// Also formula refresh cells that have references to the pasted cells.
        Model.Refresh();
 
        return !canceled;
    }
    finally
    {
        op.Close();
        Model.EndUpdate();
    }
}

Sample: Clipboard paste operation

 

Reference Link: Clipboard Support

 

Conclusion

I hope you enjoyed learning about how to improve the performance of the clipboard paste operation in WinForms GridControl.


You can refer to our WinForms GridControl feature tour page to know about its other groundbreaking feature representations and WinForms GridControl documentation, and how to quickly get started for configuration specifications.


For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.


If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied