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.
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

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

Article ID: Published Date: Last Revised Date: Platform: Control:
772 04/30/2011 07/10/2014 Windows Forms GridDataBoundGrid(Classic)
Tags:
Did you find this information helpful?
Add Comment
You must log in to leave a comment

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