Articles in this section
Category / Section

How to copy and paste selected data into Excel in WinForms GridGroupingControl?

5 mins read

Clipboard operation

By default, WinForms 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/gridgrouping/selections

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


Conclusion

I hope you enjoyed learning about how to copy and paste selected data into Excel in WinForms GridGroupingControl.

You can refer to our  WinForms GridGroupingControl feature tour page to know about its other groundbreaking feature representations. You can also explore our WinForms GridGroupingControl documentation to understand how to create and manipulate data.

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 forumsDirect-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 sign in to leave a comment
Access denied
Access denied