This is a migrated thread and some comments may be shown as answers.

CustomEditor

4 Answers 161 Views
GridView
This is a migrated thread and some comments may be shown as answers.
pierre-jean
Top achievements
Rank 1
Veteran
Iron
pierre-jean asked on 19 Feb 2021, 05:26 PM

Hello

I am trying to write a customEditor for a gridview to handle "rapid" data entry for grade values.

The grade values can be from 0 to 10 in steps of 0.5 (valid values are, for instance, 4 or 4.5, but not 4.1)
I wish to be able to type in the grades on the numerical keypad and on the main keyboard
where:typing a single character numeric value sets the value typed
and typing CTRL-num value sets the "halfed grade" (ex: type CTRL-4 for obtaining 4.5)
furthermoreI need to have the cursor positioned to the cell below imediately after the entry validation

The columns are currently text columns but could be numerical columns

I have tried using the followind code and Custom Editor:

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    Private Sub gvMark_EditorRequired(sender As Object, e As EditorRequiredEventArgs) Handles gvMark.EditorRequired
        If e.EditorType Is GetType(RadTextBoxEditor) Then
            e.EditorType = GetType(MarkEditor)
        End If
    End Sub

Public Class MarkEditor
    Inherits RadTextBoxEditor
    Public Property Value As Object
        Get
            Dim Editor As RadTextBoxEditorElement = CType(EditorElement, RadTextBoxEditorElement)
            Return Editor.Text
        End Get
        Set(value As Object)
            Dim Editor As RadTextBoxEditorElement = CType(EditorElement, RadTextBoxEditorElement)
            If value IsNot Nothing Then
                Editor.Text = Convert.ToString(value)
            Else
                Editor.Text = ""
            End If
        End Set
    End Property
    Public Overrides Sub BeginEdit()
        MyBase.BeginEdit()
        Me.EditorElement.Focus()
        Dim EditorElement As RadTextBoxEditorElement = CType(Me.EditorElement, RadTextBoxEditorElement)
        AddHandler EditorElement.KeyDown, AddressOf Element_KeyDown
    End Sub
    Public Overrides Function EndEdit() As Boolean
        MyBase.EndEdit()
        Me.EditorElement.Focus()
        Dim EditorElement As RadTextBoxEditorElement = CType(Me.EditorElement, RadTextBoxEditorElement)
        RemoveHandler EditorElement.KeyDown, AddressOf Element_KeyDown
        Return MyBase.EndEdit
    End Function
    Private Sub Element_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
        Try
            Dim KeyChar As String = fromKeyCode(e.KeyCode)
            If Not e.Control Then
                Value = KeyChar
            Else
                Value = KeyChar & ".5"
            End If
        Catch ex As Exception

        End Try
    End Sub

        Public Function fromKeyCode(KeyCode As Integer) As String
            'returns the caracters 0..9 for Keyboard Keys 0..9 and NumPadKeys 0..9 and "*" from NumPad "Multiply"
            Try
                If KeyCode >= 48 And KeyCode <= 57 Then '0..9 Keyboard
                    Return (KeyCode - 48)
                ElseIf KeyCode >= 96 And KeyCode <= 105 Then '0..9 NumPad
                    Return (KeyCode - 96)
                ElseIf KeyCode = 106 Then '* NumPad
                    Return "*"
                Else
                    Return ""
                End If
            Catch ex As Exception
                Return ""
            End Try
        End Function

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

I have a strange result (same result when using the keyboard or the numeric pad)

1. if I type a single numeric value -> it is displayed twice in the cell, i.e.if I type 4 I see 44 and the cursor is in the middle of the two characters

2. if I type CTRL-4 I get the desired 4.5 string in the cell

3. I have tried to add a sendkeys.send(vbcr) at different places (valueChanged, CellvalueChanged events unsuccessfully) to move the focus to the cell below

(Ultimately I'll have to do more advanced data validation based on other criterias)

Thanks in advance for any suggestion

Best Regards

4 Answers, 1 is accepted

Sort by
0
Nadya | Tech Support Engineer
Telerik team
answered on 24 Feb 2021, 02:21 PM

Hello, Pierre-Jean,

Thank you for the provided code snippet. 

According to the provided information, it seems that you want to enter grades as numeric values as in the cells in the grid. GridViewDecimalColumn allows decimal data to be displayed and edited and I would recommend you to use it instead of a GridViewTextBoxColumn with a custom editor. When you enter edit mode GridSpinEditor will be activated. It offers the possibility to define a step value that can be added/subtracted from the editor's value which I believe would suit your scenario where the steps would be 0.5. You can also use the MinValue and MaxValue properties to define the range from 0 to 10. Thus, the user would be restricted to enter values bigger or smaller than the specified minimum and maximum. 

ValueChanging event fires when the active editor is about to change its value. This event allows being canceled. Here is the right place where you can perform your custom logic for validating. For example, you can define the valid values for your case, and check if the new value is valid. If not, you can cancel it and this will not allow non-valid value to be preserved. More information about editing events in RadGridView you can find here: https://docs.telerik.com/devtools/winforms/controls/gridview/editors/events 

I prepared a sample demonstration for your reference. Feel free to modify and extend it in a way to fully cover your scenario.

Sub New()
    ' This call is required by the designer.
    InitializeComponent()

    AddHandler RadGridView1.CellEditorInitialized, AddressOf RadGridView1_CellEditorInitialized
    AddHandler RadGridView1.ValueChanging, AddressOf RadGridView1_ValueChanging
End Sub

Private Sub RadGridView1_ValueChanging(sender As Object, e As ValueChangingEventArgs)
    ' if the new value is not valid => cancel it
    If e.NewValue IsNot ValidValues Then
        e.Cancel = True
    End If
End Sub

' indicates which values are valid - TO DO
Private ValidValues()

Private Sub RadGridView1_CellEditorInitialized(sender As Object, e As GridViewCellEventArgs)
    Dim editor As GridSpinEditor = TryCast(Me.RadGridView1.ActiveEditor, GridSpinEditor)
    If editor IsNot Nothing Then
        editor.Step = 0.5
        editor.MinValue = 0
        editor.MaxValue = 10
        editor.DecimalPlaces = 1
    End If
End Sub

I believe this approach would be more suitable for you. Can you give it a try and see how it works.

I hope this information helps. Let me know if I can assist you further.

Regards,
Nadya
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
pierre-jean
Top achievements
Rank 1
Veteran
Iron
answered on 25 Feb 2021, 07:25 PM

Hello Nadya

Thanks for having spent time on my question.

I have looked at your proposed solution, which adresses the question of the allowed values max, step) for the grades, which is the first step of my requirement.

It however does not adress the question of using shortcuts (single keystroke) in the data entry, as per the following 

  • Entering a single numerical digit (on the main keyboard or on the numeric keypad) sets the grade to the value
  • Entering CTRL- with a single digit on the main keyboard or on the numeric keypad sets the "halfed" value, ex: CTRL-8 -> 8.5
  • Entering "*" or "X" sets a value = 10
  • Entering a space clears the current cell

And, immediately after the above single keystroke, the following cell (row below, or first cell of next column if last cell of the row)
must be selected, ready to be receive a new entry (unless the current cell has been cleared)

The purpose of this is to provide a rapid and secured data entry system

I would welcome any suggestion on how to proceed to achieve these two requirements.

Best Regards

Pierre-Jean

0
Nadya | Tech Support Engineer
Telerik team
answered on 26 Feb 2021, 01:58 PM

Hello, Pierre-Jean,

It is possible to customize the behavior of GridSpinEditor in a way that is suitable for you. Note, we can provide you general guidance and approaches that are suitable for achieving a specific custom requirement, however, the whole implementation is up to you. 

You can adapt the code that you provided earlier to be used in GridSpinEditorGridSpinEditor has RadSpinEditorElement. You can handle its KeyDown event and execute your custom logic there. I used the code snippet for your custom MarkEditor provided earlier in my sample project. I observed similar behavior when entering a single numeric value, for example - '4', it appears twice in the cell as '44'.  After debugging the code in the KeyDown it seems that you assign a value: element.Value = KeyChar. Thus the operation sequence is as follows: main logic when entering a value is executed by default, then your custom logic is executed and this is why you see the same value twice. I commented on this line of code and the problem disappears.

I am providing my updated sample with your custom logic. You can see the result in the gif file. Feel free to extend it or modify it in a way to completely fit your requirements.

Public Class RadForm1
    Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        AddHandler RadGridView1.CellEditorInitialized, AddressOf RadGridView1_CellEditorInitialized
      '  AddHandler RadGridView1.ValueChanging, AddressOf RadGridView1_ValueChanging
    End Sub

    Private Sub RadGridView1_ValueChanging(sender As Object, e As ValueChangingEventArgs)
        ' if the new value is not valid => cancel it
        If e.NewValue IsNot ValidValues Then
            e.Cancel = True
        End If
    End Sub

    ' indicates which values are valid
    Private ValidValues()

    Dim element As RadSpinEditorElement
    Private Sub RadGridView1_CellEditorInitialized(sender As Object, e As GridViewCellEventArgs)
        Dim editor As GridSpinEditor = TryCast(Me.RadGridView1.ActiveEditor, GridSpinEditor)
        If editor IsNot Nothing Then
            editor.Step = 0.5
            editor.MinValue = 0
            editor.MaxValue = 10
            editor.DecimalPlaces = 1
            element = TryCast(editor.EditorElement, RadSpinEditorElement)
            AddHandler element.KeyDown, AddressOf element_KeyDown

        End If
    End Sub

    Private Sub element_KeyDown(sender As Object, e As KeyEventArgs)
        Try
            Dim KeyChar As String = fromKeyCode(e.KeyCode)
            If Not e.Control Then
                ' element.Value = KeyChar
            Else
                element.Value = KeyChar & ".5"
            End If
        Catch ex As Exception

        End Try
    End Sub

    Public Function fromKeyCode(KeyCode As Integer) As String
        'returns the caracters 0..9 for Keyboard Keys 0..9 and NumPadKeys 0..9 and "*" from NumPad "Multiply"
        Try
            If KeyCode >= 48 And KeyCode <= 57 Then '0..9 Keyboard
                Return (KeyCode - 48)
            ElseIf KeyCode >= 96 And KeyCode <= 105 Then '0..9 NumPad
                Return (KeyCode - 96)
            ElseIf KeyCode = 106 Then '* NumPad
                Return "*"
            Else
                Return ""
            End If
        Catch ex As Exception
            Return ""
        End Try
    End Function
End Class

In addition, RadGridView offers the EnterKeyMode property. Thus, you can control the Enter key behavior when RadGridView is in edit mode. It can move the focus to the next cell or the next row. If you need to achieve some custom row behavior, please refer to the following article where is demonstrated how you can register a custom row behavior: https://docs.telerik.com/devtools/winforms/controls/gridview/rows/row-behaviors 

I hope this information helps.

Regards,
Nadya
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
pierre-jean
Top achievements
Rank 1
Veteran
Iron
answered on 26 Feb 2021, 03:46 PM

Hello Nadya

Thanks a lot for looking into my problem

I do realize that this gos beyond your nomal support activity

The information you have provided me will greatly help me finalzing the implementation of my Data Entry system

Best regards

Pierre-Jean

Tags
GridView
Asked by
pierre-jean
Top achievements
Rank 1
Veteran
Iron
Answers by
Nadya | Tech Support Engineer
Telerik team
pierre-jean
Top achievements
Rank 1
Veteran
Iron
Share this question
or