How to copy the visible columns alone to clipboard in GridDataBound?
By default, while copying cells with hidden rows and columns in GridDataBound control, the values of the hidden rows and columns will be pasted to the clipboard. To avoid this, we need to trace the hidden rows and columns and eliminate it using ClipboardCopy event of the GridDataBound control.
C#
void Model_ClipboardCopy(object sender, Syncfusion.Windows.Forms.Grid.GridCutPasteEventArgs e)
{
GridRangeInfo range = e.RangeList.ActiveRange;
if (!range.IsEmpty)
{
range = range.ExpandRange(1, 1, this.gridDataBoundGrid1.Model.RowCount, this.gridDataBoundGrid1.Model.ColCount);
string s = "";
GridData data = this.gridDataBoundGrid1.Model.Data;
for (int row = range.Top; row <= range.Bottom; ++row)
{
if (!this.gridDataBoundGrid1.Model.Rows.Hidden[row])
{
bool firstCol = true;
for (int col = range.Left; col <= range.Right; ++col)
{
if (!this.gridDataBoundGrid1.Model.Cols.Hidden[col])
{
if (!firstCol) s += "\t";
else
firstCol = false;
s+= this.gridDataBoundGrid1.Model[row, col].CellValue;
}
}
s += Environment.NewLine;
}
}
Clipboard.SetDataObject(s);
e.Handled = true;
Console.WriteLine(s);
}
}
VB
Private Sub Model_ClipboardCopy(ByVal sender As Object, ByVal e As Syncfusion.Windows.Forms.Grid.GridCutPasteEventArgs)
Dim range As GridRangeInfo = e.RangeList.ActiveRange
If (Not range.IsEmpty) Then
range = range.ExpandRange(1, 1, Me.gridDataBoundGrid1.Model.RowCount,
Me.gridDataBoundGrid1.Model.ColCount)
Dim s As String = ""
Dim data As GridData = Me.gridDataBoundGrid1.Model.Data
Dim row As Integer = range.Top
Do While row <= range.Bottom
If (Not Me.gridDataBoundGrid1.Model.Rows.Hidden(row)) Then
Dim firstCol As Boolean = True
Dim col As Integer = range.Left
Do While col <= range.Right
If (Not Me.gridDataBoundGrid1.Model.Cols.Hidden(col)) Then
If (Not firstCol) Then
s &= Constants.vbTab
Else
firstCol = False
End If
s &= Me.gridDataBoundGrid1.Model(row, col).CellValue.ToString()
End If
col += 1
Loop
s &= Environment.NewLine
End If
row += 1
Loop
Clipboard.SetDataObject(s)
e.Handled = True
Console.WriteLine(s)
End If
End Sub
Sample:
http://websamples.syncfusion.com/samples/KB/Grid.Windows/KB_CopyVisibleColumns/main.htm