)
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 prevent the grid cell from allowing paste in WinForms GridControl?

Platform: WinForms |
Control: GridControl |
Published Date: April 30, 2011 |
Last Revised Date: December 18, 2019
Tags: copypaste

Handle the PasteCellText event

You have to handle the PasteCellText event of the GridControl and cancel the event that prevents the cells from allowing paste. This event is not triggered when the current cell is in Edit mode.

C#

//In Form_Load
this.gridControl1[1, 2].Text = "HI";
this.gridControl1.ActivateCurrentCellBehavior = GridCellActivateAction.DblClickOnCell;
//using the PasteCellText Event
void gridControl1_PasteCellText(object sender, Syncfusion.Windows.Forms.Grid.GridPasteCellTextEventArgs e)
{
    //Row 3 as prevent from paste
    if (e.RowIndex == 3)
        e.Cancel = true;
}

VB

' In Form_Load
Me.gridControl1(2, 2).Text = "HI"
Me.gridControl1.ActivateCurrentCellBehavior = GridCellActivateAction.DblClickOnCell
Private Sub gridControl1_PasteCellText(ByVal sender As Object, ByVal e As Syncfusion.Windows.Forms.Grid.GridPasteCellTextEventArgs)
' Row 3 as prevent from paste
    If e.RowIndex = 3 Then
        e.Cancel = True
End If
End Sub

When you want to avoid pasting into a cell that is in Edit mode, you can make use of the CurrentCellControlKeyMessage event. In the following code, the paste operation is specified by the shortcut key, Ctrl+V.

C#

void gridControl1_CurrentCellControlKeyMessage(object sender, GridCurrentCellControlKeyMessageEventArgs e)
{
    Keys keyCode = (Keys)((int)e.Msg.WParam) & Keys.KeyCode;
    //Condition for Ctr+V keys
    if ((Control.ModifierKeys & Keys.Control) != 0 && keyCode == Keys.V )
    {
        GridCurrentCell cc = this.gridControl1.CurrentCell;
        //set the row 3 as prevent from  paste
        if(cc.RowIndex == 3)
            e.Handled = true;
        e.Result = true;
    }
}

VB

Private Sub gridControl1_CurrentCellControlKeyMessage(ByVal sender As Object, ByVal e As GridCurrentCellControlKeyMessageEventArgs)
    Dim keyCode As Keys = CType(CInt(Fix(e.Msg.WParam)), Keys) And Keys.KeyCode
    If (Control.ModifierKeys And Keys.Control) <> 0 AndAlso keyCode = Keys.V Then
        ' set the row 3 as prevent from allow paste
        Dim cc As GridCurrentCell = Me.gridControl1.CurrentCell
        If cc.RowIndex = 3 Then
            e.Handled = True
        End If
    e.Result = True
    End If
End Sub

Samples:

C#: Preventing_from_Pate

VB: Preventing_from_Paste

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

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