Category / Section
How to copy the table content to clipboard without hidden content in WinForms GridControl?
2 mins read
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
Samples:
Reference link: https://help.syncfusion.com/windowsforms/grid-control/copy-and-paste