Category / Section
How to implement a fill paste in the WinForms GridControl?
2 mins read
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.

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