)
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 copy and paste the selected records data into excel sheet in WinForms GridGroupingControl?

Platform: WinForms |
Control: GridGroupingControl |
Published Date: January 3, 2016 |
Last Revised Date: December 31, 2019
Tags: excel, copypaste

Clipboard operation

By default, GridGroupingControl cannot be copied the whole cell values for NestedGrid, it can be achieved by manually setting the clipboard text based on our need using ClipboardCopy event.

C#

private void TableModel_ClipboardCopy(object sender, GridCutPasteEventArgs e)
{
    this.CopySelectedRecords(false);
    e.Handled = true;
}
private void CopySelectedRecords(bool cut)
{
    string s = "";
    //Copying visible column names to the string buffer.
    foreach (GridVisibleColumnDescriptor cd in this.gridGroupingControl1.TableDescriptor.VisibleColumns)
    {
        int index = this.gridGroupingControl1.TableDescriptor.VisibleColumns.IndexOf(cd);
        if (index != 0)
        {
            s += "\t";
        }
        s += cd.Name;
    }
    s += Environment.NewLine;
    //Copying the selected records.
    if (this.gridGroupingControl1.Table.SelectedRecords.Count > 0)
    {
        //Selection is made in the parent table
        foreach (SelectedRecord selRec in this.gridGroupingControl1.Table.SelectedRecords)
        {
            s = CopySelectedRecordsToBuffer(s, selRec.Record, gridGroupingControl1.TableDescriptor, cut);
        }
    }
    else
    {
        //Selection is made in the child table.
        s = "";
        GridTable child = gridGroupingControl1.GetTable("MarkSheet");
        if (child.SelectedRecords.Count > 0)
        {
            //Copying visible column names of the child table into the string buffer.
            foreach (GridVisibleColumnDescriptor cd in child.TableDescriptor.VisibleColumns)
            {
                int index = child.TableDescriptor.VisibleColumns.IndexOf(cd);
                if (index != 0)
                {
                    s += "\t";
                }
                s += cd.Name;
            }
            s += Environment.NewLine;
            foreach (SelectedRecord r in child.SelectedRecords)
            {
                s = CopySelectedRecordsToBuffer(s, r.Record, child.TableDescriptor, cut);
            }
        }
    }
    Clipboard.SetDataObject(new DataObject(s), true);
}

 

VB

Private Sub TableModel_ClipboardCopy(ByVal sender As Object, ByVal e As GridCutPasteEventArgs)
        Me.CopySelectedRecords(False)
        e.Handled = True
End Sub
Private Sub CopySelectedRecords(ByVal cut As Boolean)
        Dim s As String = ""
        'Copying visible column names to the string buffer.
        For Each cd As GridVisibleColumnDescriptor In Me.gridGroupingControl1.TableDescriptor.VisibleColumns
            Dim index As Integer = Me.gridGroupingControl1.TableDescriptor.VisibleColumns.IndexOf(cd)
                If index <> 0 Then
                    s &= Constants.vbTab
                End If
                    s &= cd.Name
                    Next cd
                    s &= Environment.NewLine
        'Copying the selected records.
        If Me.gridGroupingControl1.Table.SelectedRecords.Count > 0 Then
            'Selection is made in the parent table
            For Each selRec As SelectedRecord In Me.gridGroupingControl1.Table.SelectedRecords
                s = CopySelectedRecordsToBuffer(s, selRec.Record, gridGroupingControl1.TableDescriptor, cut)
                Next selRec
        Else
            'Selection is made in the child table.
                s = ""
                Dim child As GridTable = gridGroupingControl1.GetTable("MarkSheet")
                If child.SelectedRecords.Count > 0 Then
                    'Copying visible column names of the child table into the string buffer.
                    For Each cd As GridVisibleColumnDescriptor In child.TableDescriptor.VisibleColumns
                        Dim index As Integer = child.TableDescriptor.VisibleColumns.IndexOf(cd)
                            If index <> 0 Then
                                s &= Constants.vbTab
                            End If
                            s &= cd.Name
                            Next cd
                            s &= Environment.NewLine
                            For Each r As SelectedRecord In child.SelectedRecords
                                    s = CopySelectedRecordsToBuffer(s, r.Record, child.TableDescriptor, cut)
                                    Next r
                End If
        End If
        Clipboard.SetDataObject(New DataObject(s), True)
End Sub

 

Samples:

C# : Pastetocell_CS

VB : Pastetocell_VB

Reference links:

1.https://help.syncfusion.com/windowsforms/classic/gridgroupingcontrol/selections

2. https://help.syncfusion.com/windowsforms/classic/gridgroupingcontrol/clipboard-operations

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