FlexGrid for WinForms | ComponentOne
In This Topic
    Clipboard
    In This Topic

    FlexGrid provides flexibility to perform various clipboard operations such as cut, copy, and paste on the editable grid data. To enable automatic handling of common clipboard keys, you need to set the AutoClipboard property to true. The property handles following clipboard operations and their corresponding keys:

    Copy

    CTRL+C, Ctrl+INS

    Cut CTRL+X, SHIFT+DEL
    Paste CTRL+V, SHIFT+INS
    Delete DEL

    The above mentioned clipboard operations do not have any affect on the styles and images and only work on grid headers and data. You can choose which part of the selected content to copy out of row header, column header and data by using the ClipboardCopyMode property. However, the copy operation does not support merged cells within the grid. When a datamap or multicolumn combobox exists in the copied range, only the display value is copied. Also, note that hidden cells are also copied when copying a cell range. To exclude hidden cells from copying, see Exclude Hidden Cells.

    The code below demonstrates how to enable clipboard operations in the WinForms FlexGrid.

    // enable clipboard operations through keyboard
       c1FlexGrid1.AutoClipboard = true;
       
    // set copy mode to copy data and all headers
       c1FlexGrid1.ClipboardCopyMode = C1.Win.C1FlexGrid.ClipboardCopyModeEnum.DataAndAllHeaders;
    
    ' enable clipboard operations through keyboard
    c1FlexGrid1.AutoClipboard = True
    
    ' set copy mode to copy data and all headers
    c1FlexGrid1.ClipboardCopyMode = C1.Win.C1FlexGrid.ClipboardCopyModeEnum.DataAndAllHeaders
    

    Clipboard Operations through Code

    FlexGrid provides Cut, Copy, and Paste methods in the C1FlexGrid class which can be used to implement clipboard operations on the entire grid or a selected part of the grid. This enables you to quickly copy or cut the required data and paste the copied content onto the current selection.

    The following example demonstrates how to perform copy and paste operations on button click.

    private void CopyButton_Click(object sender, EventArgs e)
    {
        // allow copying only data of the grid to be copied to the clipboard
        c1FlexGrid1.ClipboardCopyMode = ClipboardCopyModeEnum.DataOnly;
    
        // copies the selected content
        c1FlexGrid1.Copy();
    }
    
    private void PasteButton_Click(object sender, EventArgs e)
    {
        //pastes the copied content to the current selection
        c1FlexGrid1.Paste();
        
    }
    
    Private Sub CopyButton_Click(ByVal sender As Object, ByVal e As EventArgs)
        ' allow copying only data of the grid to be copied to the clipboard
        c1FlexGrid1.ClipboardCopyMode = ClipboardCopyModeEnum.DataOnly
    
        'copies the selected content
        c1FlexGrid1.Copy();
       
    End Sub
    
    Private Sub PasteButton_Click(ByVal sender As Object, ByVal e As EventArgs)
        'pastes the copied content to the current selection
        c1FlexGrid1.Paste();
    
    End Sub
    

    Similarly, you can implement the above mentioned clipboard operations on various types of selected ranges within the grid. These selected ranges could be contiguous, like row range, column range, or non-contiguous, like multi-range. For non-contiguous selection ranges like multi-range, the selected range must lie within a common row or column range.

    Exclude Hidden Cells

    In FlexGrid, by default, hidden cells are also copied when copying a cell range through keyboard operations enabled using the AutoClipboard property. However, you can exclude the hidden rows and columns by using the code below.

    Following code shows how you can exclude the hidden cells from clipboard operations performed on the WinForms Tree Grid.

    // hide a column
    c1FlexGrid1.Cols[2].Visible = false;
    c1FlexGrid1.AutoClipboard = true;
            
    
    // then select some cells containing hidden column before pressing Ctrl+C
    private void c1FlexGrid1_KeyDown_1(object sender, KeyEventArgs e)
    {
     
            // Copy with [Ctrl + C]
            if ((e.Control == true) && (e.KeyCode == Keys.C))
            {
                // Disable key input because automatic processing is not performed
                e.Handled = true;
                // Get the CellRange object for the selected cell range
                C1.Win.C1FlexGrid.CellRange cr;
                cr = c1FlexGrid1.Selection;
    
                string StrCopy = "";
                for (int i = cr.r1; i <= cr.r2; i++)
                {
                    if (c1FlexGrid1.Rows[i].Visible == true)
                    {
                        for (int j = cr.c1; j <= cr.c2; j++)
                        {
                            if (c1FlexGrid1.Cols[j].Visible == true)
                            {
                                StrCopy = StrCopy + c1FlexGrid1[i, j].ToString();
                                if (j != cr.c2)
                                {
                                    StrCopy = StrCopy + "\t";
                                }
                            }
                        }
                        StrCopy = StrCopy + "\n";
                    }
                }
                // set to clipboard
                Clipboard.SetDataObject(StrCopy);
                MessageBox.Show("Copied data: \n" + StrCopy);
            }
      
    }
    
        ' hide a column
        c1FlexGrid1.Cols(2).Visible = False
        c1FlexGrid1.AutoClipboard = True
            
    
    ' then select some cells containing hidden column before pressing Ctrl+C
    Private Sub c1FlexGrid1_KeyDown_1(ByVal sender As Object, ByVal e As KeyEventArgs)
    
        ' Copy with [Ctrl + C]
        If e.Control = True AndAlso e.KeyCode = Keys.C Then
            ' Disable key input because automatic processing is not performed
            e.Handled = True
            ' Get the CellRange object for the selected cell range
            Dim cr As C1.Win.C1FlexGrid.CellRange
            cr = c1FlexGrid1.Selection
            Dim StrCopy = ""
    
            For i = cr.r1 To cr.r2
    
                If c1FlexGrid1.Rows(i).Visible = True Then
                    For j = cr.c1 To cr.c2
    
                        If c1FlexGrid1.Cols(j).Visible = True Then
                            StrCopy = StrCopy & c1FlexGrid1(i, j).ToString()
    
                            If j <> cr.c2 Then
                                StrCopy = StrCopy & Microsoft.VisualBasic.Constants.vbTab
                            End If
                        End If
                    Next
    
                    StrCopy = StrCopy & Microsoft.VisualBasic.Constants.vbLf
                End If
            Next
            ' set to clipboard
            Clipboard.SetDataObject(StrCopy)
            MessageBox.Show("Copied data: " & Microsoft.VisualBasic.Constants.vbLf & StrCopy)
        End If
    End Sub
    
    See Also

    Documentation