Category / Section
How to restrict the operations of delete and backspace key of DateTimePicker in WinForms GridControl?
1 min read
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