)
We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to implement a fill paste in the WinForms GridControl?

Platform: WinForms |
Control: GridControl |
Published Date: April 30, 2011 |
Last Revised Date: December 18, 2019

Fill paste in grid

You can copy the cell value and replicate it by selecting a range. Then paste this value into that range. This is called Fill Paste.

You can try to handle this in the ClipboardPaste event.

C#

void gridControl1_ClipboardPaste(object sender, Syncfusion.Windows.Forms.Grid.GridCutPasteEventArgs e)
{
    DataObject data = (DataObject)Clipboard.GetDataObject();
    string[] rows = null;
    int numRows = 0;
    int numCols = 0;
    //Get the size of the paste
    if(data.GetDataPresent(DataFormats.Text))
    {
        string s = (string)data.GetData(DataFormats.Text);
        rows = s.Split(new char[]{'\n'});
        numRows = rows.GetLength(0);
        if(numRows > 0 && rows[numRows - 1].Length == 0)
            numRows--; //remove extra empty row if present
        if(numRows > 0)
        {
            string[] cols = rows[0].Split(new char[]{'\t'});
            numCols = cols.GetLength(0);
        }
    }
    //paste one to many
    if(numRows == 1 && numCols == 1 &&   !this.gridControl1.Selections.Ranges.ActiveRange.IsEmpty)
    {
        this.gridControl1.ChangeCells(this.gridControl1.Selections.Ranges.ActiveRange,
rows[0]);
        e.Handled = true;
        e.Result = true;
    }
}

 

VB

Private Sub gridControl1_ClipboardPaste(ByVal sender As Object, ByVal e As Syncfusion.Windows.Forms.Grid.GridCutPasteEventArgs)
    Dim data As DataObject = CType(Clipboard.GetDataObject(), DataObject)
    Dim rows() As String = Nothing
    Dim numRows As Integer = 0
    Dim numCols As Integer = 0
    'Get the size of the paste
    If data.GetDataPresent(DataFormats.Text) Then
        Dim s As String = CStr(data.GetData(DataFormats.Text))
        rows = s.Split(New Char(){ControlChars.Lf})
        numRows = rows.GetLength(0)
        If numRows > 0 AndAlso rows(numRows - 1).Length = 0 Then
            numRows -= 1 'remove extra empty row if present
        End If
        If numRows > 0 Then
            Dim cols() As String = rows(0).Split(New Char(){ControlChars.Tab})
            numCols = cols.GetLength(0)
        End If
    End If
    'paste one to many
    If numRows = 1 AndAlso numCols = 1 AndAlso (Not Me.gridControl1.Selections.Ranges.ActiveRange.IsEmpty) Then
        Me.gridControl1.ChangeCells(Me.gridControl1.Selections.Ranges.ActiveRange,    rows(0))
        e.Handled = True
        e.Result = True
    End If
End Sub

After applying the properties, the Grid is as follows.

Fill the paste in the selected cells

Figure 1: Fill the paste in the selected cells

Samples:

C#: Fill By Paste

VB: Fill By Paste

Reference link: https://help.syncfusion.com/windowsforms/grid-control/copy-and-paste#paste

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon