)
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 restrict the operations of delete and backspace key of DateTimePicker in WinForms GridControl?

Platform: WinForms |
Control: GridControl |
Published Date: March 21, 2016 |
Last Revised Date: December 17, 2019

Restrict operation of delete and backspace key

To restrict specific keys in Date time picker cells, you need to use CurrentCellKeyDown event and validate using key code.

C#

//MonthCalendar celltype
this.gridControl1[1, 2].Text = "MonthCalendar";
this.gridControl1.ColWidths[2] = 100;
this.gridControl1[2, 2].CellType = GridCellTypeName.MonthCalendar;
//Event Handler
this.gridControl1.CurrentCellKeyDown += gridControl1_CurrentCellKeyDown;
 
 void gridControl1_CurrentCellKeyDown(object sender, KeyEventArgs e)
        {
            //To restrict the operations of delete key.
            int rowIndex = this.gridControl1.CurrentCell.RowIndex;
            int columnIndex = this.gridControl1.CurrentCell.ColIndex;
            if (this.gridControl1[rowIndex,columnIndex].CellType  == GridCellTypeName.MonthCalendar 
                && (e.KeyCode == Keys.Delete))
            {                
                e.Handled = true;
            }
        }
 

 

VB

'MonthCalendar celltype
 Me.gridControl1(1, 2).Text = "MonthCalendar"
 Me.gridControl1.ColWidths(2) = 100
 Me.gridControl1(2, 2).CellType = GridCellTypeName.MonthCalendar
'Event Handler
 Me.gridControl1.CurrentCellKeyDown += gridControl1_CurrentCellKeyDown
 
Private Sub gridControl1_CurrentCellKeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
  'To restrict the operations of delete key.
   Dim rowIndex As Integer = Me.gridControl1.CurrentCell.RowIndex
   Dim columnIndex As Integer = Me.gridControl1.CurrentCell.ColIndex
   If Me.gridControl1(rowIndex,columnIndex).CellType Is GridCellTypeName.MonthCalendar AndAlso (e.KeyCode = Keys.Delete) Then
 e.Handled = True
   End If
End Sub

 

Screenshot

Restrict operation of delete and backspacekey of datetimepicker

 

Samples:

C#: RestrictKeys_CS

VB: RestrictKeys_VB

 

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