Articles in this section
Category / Section

How to prevent the grid cell from allowing paste in WinForms GridControl?

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.

//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;
}
' 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.

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;
    }
}
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: Clipboard Support

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Access denied
Access denied