Articles in this section
Category / Section

How to copy the data from one row to many rows and one column to many columns in WinForms GridGroupingControl?

2 mins read

Clipboard operation


In order to copy the data from one row to many rows and one column to many columns, the ClipBoardCanPaste event can be used. The target row or column range has to be given based on the Row and Column cells pasting. The data can be pasted to grid by using PasteTextFromBuffer method.

 

C#

// To paste at all the selected columns
gridControl1.ClipboardCanPaste += new GridCutPasteEventHandler(gridControl1_ClipboardCanPaste);

private void gridControl1_ClipboardCanPaste(object sender, GridCutPasteEventArgs e)
{
    // For copying the data from one column to many columns
    if (this.gridControl1.Model.SelectedRanges.ActiveRange.IsCols)
    {
        for (int i = this.gridControl1.Model.SelectedRanges.ActiveRange.Left;
                 i <= this.gridControl1.Model.SelectedRanges.ActiveRange.Right; i++)
        {
            GridRangeInfo range = GridRangeInfo.Cells(1, i, this.gridControl1.RowCount, i);
            IDataObject iData = Clipboard.GetDataObject();
            string buffer = iData.GetData(DataFormats.UnicodeText) as string;

            if (buffer != null)
            {
                this.gridControl1.Model.TextDataExchange.PasteTextFromBuffer(
                    buffer, range, GridDragDropFlags.Text);
            }
        }
    }

    // For copying the data from one row to many rows
    else if (this.gridControl1.Model.SelectedRanges.ActiveRange.IsRows)
    {
        for (int i = this.gridControl1.Model.SelectedRanges.ActiveRange.Top;
                 i <= this.gridControl1.Model.SelectedRanges.ActiveRange.Bottom; i++)
        {
            GridRangeInfo range = GridRangeInfo.Cells(i, 1, i, this.gridControl1.ColCount);
            IDataObject iData = Clipboard.GetDataObject();
            string buffer = iData.GetData(DataFormats.UnicodeText) as string;

            if (buffer != null)
            {
                this.gridControl1.Model.TextDataExchange.PasteTextFromBuffer(
                    buffer, range, GridDragDropFlags.Text);
            }
        }
    }
} 

VB

' To paste at all the selected columns
AddHandler gridControl1.ClipboardCanPaste, AddressOf gridControl1_ClipboardCanPaste

Private Sub gridControl1_ClipboardCanPaste(sender As Object, e As GridCutPasteEventArgs)
    ' For copying the data from one column to many columns
    If Me.gridControl1.Model.SelectedRanges.ActiveRange.IsCols Then
        For i As Integer = Me.gridControl1.Model.SelectedRanges.ActiveRange.Left To
                           Me.gridControl1.Model.SelectedRanges.ActiveRange.Right

            Dim range As GridRangeInfo = GridRangeInfo.Cells(1, i, Me.gridControl1.RowCount, i)
            Dim iData As IDataObject = Clipboard.GetDataObject()
            Dim buffer As String = TryCast(iData.GetData(DataFormats.UnicodeText), String)

            If buffer IsNot Nothing Then
                Me.gridControl1.Model.TextDataExchange.PasteTextFromBuffer(
                    buffer, range, GridDragDropFlags.Text)
            End If
        Next
    ' For copying the data from one row to many rows
    ElseIf Me.gridControl1.Model.SelectedRanges.ActiveRange.IsRows Then
        For i As Integer = Me.gridControl1.Model.SelectedRanges.ActiveRange.Top To
                           Me.gridControl1.Model.SelectedRanges.ActiveRange.Bottom

            Dim range As GridRangeInfo = GridRangeInfo.Cells(i, 1, i, Me.gridControl1.ColCount)
            Dim iData As IDataObject = Clipboard.GetDataObject()
            Dim buffer As String = TryCast(iData.GetData(DataFormats.UnicodeText), String)

            If buffer IsNot Nothing Then
                Me.gridControl1.Model.TextDataExchange.PasteTextFromBuffer(
                    buffer, range, GridDragDropFlags.Text)
            End If
        Next
    End If
End Sub

Screenshots:

Column based copying

Copy the data based on columns
















Row-based copying

Copy the data based on rows

















Samples:

C#: Copy and Paste_CS

VB: Copy and Paste_VB

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