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. Image for the cookie policy date
close icon

Currentcellkeypress event is not firing

code below is not working correctly. The key_down event only fires on control keys. I thought it would fire on any key pressed. The key press event is not firing after the key down event. I''m trying to stop a user from entering in alpha keys. thanks Private Sub dsEditData_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dsEditData.KeyDown '' Initialize the flag to false. nonNumberEntered = False '' Determine whether the keystroke is a number from the top of the keyboard. If e.KeyCode < Keys.D0 OrElse e.KeyCode > Keys.D9 Then '' Determine whether the keystroke is a number from the keypad. If e.KeyCode < Keys.NumPad0 OrElse e.KeyCode > Keys.NumPad9 Then '' Determine whether the keystroke is a backspace. If e.KeyCode <> Keys.Back Then '' A non-numerical keystroke was pressed. '' Set the flag to true and evaluate in KeyPress event. nonNumberEntered = True End If End If End If End Sub Private Sub dsEditData_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles dsEditData.KeyPress Dim cc As GridCurrentCell = Me.dsEditData.CurrentCell Dim newValue As String = Me.dsEditData(cc.RowIndex, cc.ColIndex).FormattedText Dim dr As DataRow = dsData.Tables("DataPointDetail").Rows(cc.RowIndex - 1) If nonNumberEntered = True Then '' Stop the character from being entered into the control since it is non-numerical. e.Handled = True Else UpdateData(cc.ColIndex, dr, newValue) End If End Sub

1 Reply

AD Administrator Syncfusion Team January 28, 2004 02:11 PM UTC

CurrentCellKeyPress is the event you should use to get every keystroke in a cell. It catches both the first keystroke on an inactive cell as well as subsequent keystrokes into the active cell (when the grid no longer has input focus.) But there is a special event you can handle to validate cell contents on a keystroke by keystroke basis. It is CurrentCellValidateString. It gives you easy access to the potential new string so you can easily validate things. Here is code that will only allow the user to type a valid double. Private Sub GridControl1_CurrentCellValidateString(ByVal sender As Object, ByVal e As GridCurrentCellValidateStringEventArgs) Handles GridControl1.CurrentCellValidateString Dim cc As GridCurrentCell = Me.GridControl1.CurrentCell ''check only column 1 for example.... If cc.ColIndex <> 1 Then Return End If Try If e.Text <> "." Then Dim d As Double = Double.Parse(e.Text) End If Catch e.Cancel = True Return End Try End Sub

Live Chat Icon For mobile
Up arrow icon