)
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 the table content to clipboard without hidden content in WinForms GridControl?

Platform: WinForms |
Control: GridControl |
Published Date: May 10, 2016 |
Last Revised Date: December 16, 2019
Tags: copypaste

Copy the table content to clipboard

By default, while copying cells with hidden rows and columns in GridControl, the values of the hidden rows and columns will be copied to the clipboard. In order to copy the table content to clipboard without hidden contents, the ClipboardCopy event can be used to copy the cell content which are visible only.

C#

void Model_ClipboardCopy(object sender, GridCutPasteEventArgs e)
{
   GridRangeInfo range = e.RangeList.ActiveRange;
   if (!range.IsEmpty)
   {
      range = range.ExpandRange(1, 1, this.gridControl1.Model.RowCount, this.gridControl1.Model.ColCount);
      string clipboardText = "";
      GridData data = this.gridControl1.Model.Data;
      for (int row = range.Top; row <= range.Bottom; ++row)
      {
          if (!this.gridControl1.Model.Rows.Hidden[row])
          {
             bool firstCol = true;
             for (int col = range.Left; col <= range.Right; ++col)
             {
                if (!this.gridControl1.Model.Cols.Hidden[col])
                {
                   if (!firstCol) 
                      clipboardText += "\t";
                   else
                      firstCol = false;
                   clipboardText += this.gridControl1.Model[row, col].CellValue;
                 }
              }
            clipboardText += Environment.NewLine;
        }
     }
     Clipboard.SetDataObject(clipboardText);
     e.Handled = true;
     this.textBox1.Text = clipboardText;
   }
}
 

 

VB

Private Sub Model_ClipboardCopy(ByVal sender As Object, ByVal e As GridCutPasteEventArgs)
   Dim range As GridRangeInfo = e.RangeList.ActiveRange
   If Not range.IsEmpty Then
      range = range.ExpandRange(1, 1, Me.gridControl1.Model.RowCount, Me.gridControl1.Model.ColCount)
      Dim clipboardText As String = ""
      Dim data As GridData = Me.gridControl1.Model.Data
      For row As Integer = range.Top To range.Bottom
         If Not Me.gridControl1.Model.Rows.Hidden(row) Then
            Dim firstCol As Boolean = True
 For col As Integer = range.Left To range.Right
               If Not Me.gridControl1.Model.Cols.Hidden(col) Then
                  If Not firstCol Then
          clipboardText &= Constants.vbTab
       Else
          firstCol = False
       End If
       clipboardText &= Me.gridControl1.Model(row, col).CellValue
    End If
 Next col
 clipboardText &= Environment.NewLine
        End If
     Next row
     Clipboard.SetDataObject(clipboardText)
     e.Handled = True
     Me.textBox1.Text = clipboardText
  End If
End Sub

      Copy and Paste without hidden contents

 

 

Samples:

C#: Copy and paste_CS

VB: Copy and paste_VB

Reference link: https://help.syncfusion.com/windowsforms/grid-control/copy-and-paste

 

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