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

Need tab key event on Cell edit mode

5 Answers 373 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Dev
Top achievements
Rank 1
Veteran
Dev asked on 06 Apr 2020, 03:24 PM

Hi Team,
       On the cell end edit mode the enter key event is working fine and it is firing the keydown event from the CellEditorInitialized and gets updated with the given value on the other grid. - This is the actual process.

Is it possible to have the same functionality while pressing the tab key?

 

Video Link

5 Answers, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 09 Apr 2020, 10:20 AM

 

Hello, Rick,  

The provided sample video is greatly appreciated for understanding better the scenario you have. However, it is not very clear what is the exact code implementation that you have on your end.

I suppose that in the CellEditorInitialized event you subscribe to some event of the activated editor. Once this handled event is fired, e.g. when pressing Enter, you execute certain logic, e.g. updating the respective cell's value for the top grid.

In this case, I would recommend you to follow another approach. Don't handle any events on the editor itself. It is suitable to use the RadGridView.CellValueChanged event. Since this event fires when the value of a cell has been changed, you will handle cell's value changing as a result of both keys pressing, TAB and ENTER, along with focusing another cell as well. Then you can perform the logic you have.

However, if it is not the case, it would be greatly appreciated if you can provide more details about the precise case. Thus, we would be able to think about a suitable solution.

I hope this information helps. If you need any further assistance please don't hesitate to contact me.

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
0
Dev
Top achievements
Rank 1
Veteran
answered on 09 Apr 2020, 02:21 PM

Hi Dess,

Thanks for your reply. With the provided solution we are not sure from where the current key event is taken. Can you please provide us a sample code snippet for it?

We have written code on key down event on cell editor initialized.Can you please let us know if we can use the same code on cellvalueChanged ?

     Private Sub rdgvHGrid_CellEditorInitialized(sender As Object, e As GridViewCellEventArgs) Handles rdgvHGrid.CellEditorInitialized
        If Not processing Then
            Dim tbEditor As RadTextBoxEditor = TryCast(Me.rdgvHGrid.ActiveEditor, RadTextBoxEditor)
            If Not tbEditor Is Nothing Then
                processing = True
                Dim tbElement As RadTextBoxEditorElement = CType(tbEditor.EditorElement, RadTextBoxEditorElement)
                AddHandler tbElement.KeyDown, AddressOf tbElement_KeyDown
            End If
        End If
    End Sub

  Private Sub tbElement_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
        Dim T As Integer

        T = rdgvHGrid.CurrentColumn.Index

        ' If hit delete key
        If rdgvHGrid.CurrentColumn.Index > 0 Or rdgvHGrid.CurrentColumn.Index < 6 Then
            If e.KeyCode = Keys.Delete Then
                '.Columns(.Col).Text = 0
                '.RefetchRow
                ArrayItemH(rdgvHGrid.CurrentRow.Index)(rdgvHGrid.CurrentColumn.Index) = 0
                rdgvHGrid.DataSource = ArrayItemH
            End If
        End If

        ' If the user hits the arrow down key
        'If e.KeyCode = Keys.Down Then
        If e.KeyCode = 13 Then
            rdgvHGrid.EndEdit()
            rdgvHGrid.BeginEdit()
            ' Update array
            ArrayItemH(rdgvHGrid.CurrentRow.Index)(1) = rdgvHGrid.CurrentRow.Cells(1).Value
            ArrayItemH(rdgvHGrid.CurrentRow.Index)(2) = rdgvHGrid.CurrentRow.Cells(2).Value
            ArrayItemH(rdgvHGrid.CurrentRow.Index)(3) = rdgvHGrid.CurrentRow.Cells(3).Value
            ArrayItemH(rdgvHGrid.CurrentRow.Index)(4) = rdgvHGrid.CurrentRow.Cells(4).Value
            ArrayItemH(rdgvHGrid.CurrentRow.Index)(5) = rdgvHGrid.CurrentRow.Cells(5).Value

            ' Update Main grid
            If rdgvHGrid.CurrentColumn.Index > 0 And rdgvHGrid.CurrentColumn.Index < 6 Then UpdateMain()

            ' .Col = T
        End If

        ' Update array
        If e.KeyCode = Keys.Tab Or e.KeyCode = Keys.Enter Or e.KeyCode = Keys.Up Then

            ArrayItemH(rdgvHGrid.CurrentRow.Index)(rdgvHGrid.CurrentColumn.Index) = rdgvHGrid.CurrentRow.Cells(rdgvHGrid.CurrentColumn.Index).Value

            ' Update Main grid
            If T > 0 And T < 6 Then UpdateMain()

            ' rdgvHGrid.CurrentColumn.Index = T
            ArrayItemH(rdgvHGrid.CurrentRow.Index)(1) = rdgvHGrid.CurrentRow.Cells(1).Value
            ArrayItemH(rdgvHGrid.CurrentRow.Index)(2) = rdgvHGrid.CurrentRow.Cells(2).Value
            ArrayItemH(rdgvHGrid.CurrentRow.Index)(3) = rdgvHGrid.CurrentRow.Cells(3).Value
            ArrayItemH(rdgvHGrid.CurrentRow.Index)(4) = rdgvHGrid.CurrentRow.Cells(4).Value
            ArrayItemH(rdgvHGrid.CurrentRow.Index)(5) = rdgvHGrid.CurrentRow.Cells(5).Value

            ' Update Main grid
            If rdgvHGrid.CurrentColumn.Index > 0 And rdgvHGrid.CurrentColumn.Index < 6 Then UpdateMain()

            '.Col = T
        End If
    End Sub

0
Dev
Top achievements
Rank 1
Veteran
answered on 09 Apr 2020, 02:35 PM

Hi Dess,

Thanks for your reply. With the provided solution we are not sure from where the current key event is taken. Can you please provide us a sample code snippet for it?

We have written code on key down event on cell editor initialized.Can you please let us know if we can use the same code on cellvalueChanged ?

     Private Sub rdgvHGrid_CellEditorInitialized(sender As Object, e As GridViewCellEventArgs) Handles rdgvHGrid.CellEditorInitialized
        If Not processing Then
            Dim tbEditor As RadTextBoxEditor = TryCast(Me.rdgvHGrid.ActiveEditor, RadTextBoxEditor)
            If Not tbEditor Is Nothing Then
                processing = True
                Dim tbElement As RadTextBoxEditorElement = CType(tbEditor.EditorElement, RadTextBoxEditorElement)
                AddHandler tbElement.KeyDown, AddressOf tbElement_KeyDown
            End If
        End If
    End Sub

  Private Sub tbElement_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
        Dim T As Integer

        T = rdgvHGrid.CurrentColumn.Index

        ' If hit delete key
        If rdgvHGrid.CurrentColumn.Index > 0 Or rdgvHGrid.CurrentColumn.Index < 6 Then
            If e.KeyCode = Keys.Delete Then
                '.Columns(.Col).Text = 0
                '.RefetchRow
                ArrayItemH(rdgvHGrid.CurrentRow.Index)(rdgvHGrid.CurrentColumn.Index) = 0
                rdgvHGrid.DataSource = ArrayItemH
            End If
        End If

        ' If the user hits the arrow down key
        'If e.KeyCode = Keys.Down Then
        If e.KeyCode = 13 Then
            rdgvHGrid.EndEdit()
            rdgvHGrid.BeginEdit()
            ' Update array
            ArrayItemH(rdgvHGrid.CurrentRow.Index)(1) = rdgvHGrid.CurrentRow.Cells(1).Value
            ArrayItemH(rdgvHGrid.CurrentRow.Index)(2) = rdgvHGrid.CurrentRow.Cells(2).Value
            ArrayItemH(rdgvHGrid.CurrentRow.Index)(3) = rdgvHGrid.CurrentRow.Cells(3).Value
            ArrayItemH(rdgvHGrid.CurrentRow.Index)(4) = rdgvHGrid.CurrentRow.Cells(4).Value
            ArrayItemH(rdgvHGrid.CurrentRow.Index)(5) = rdgvHGrid.CurrentRow.Cells(5).Value

            ' Update Main grid
            If rdgvHGrid.CurrentColumn.Index > 0 And rdgvHGrid.CurrentColumn.Index < 6 Then UpdateMain()

            ' .Col = T
        End If

        ' Update array
        If e.KeyCode = Keys.Tab Or e.KeyCode = Keys.Enter Or e.KeyCode = Keys.Up Then

            ArrayItemH(rdgvHGrid.CurrentRow.Index)(rdgvHGrid.CurrentColumn.Index) = rdgvHGrid.CurrentRow.Cells(rdgvHGrid.CurrentColumn.Index).Value

            ' Update Main grid
            If T > 0 And T < 6 Then UpdateMain()

            ' rdgvHGrid.CurrentColumn.Index = T
            ArrayItemH(rdgvHGrid.CurrentRow.Index)(1) = rdgvHGrid.CurrentRow.Cells(1).Value
            ArrayItemH(rdgvHGrid.CurrentRow.Index)(2) = rdgvHGrid.CurrentRow.Cells(2).Value
            ArrayItemH(rdgvHGrid.CurrentRow.Index)(3) = rdgvHGrid.CurrentRow.Cells(3).Value
            ArrayItemH(rdgvHGrid.CurrentRow.Index)(4) = rdgvHGrid.CurrentRow.Cells(4).Value
            ArrayItemH(rdgvHGrid.CurrentRow.Index)(5) = rdgvHGrid.CurrentRow.Cells(5).Value

            ' Update Main grid
            If rdgvHGrid.CurrentColumn.Index > 0 And rdgvHGrid.CurrentColumn.Index < 6 Then UpdateMain()

            '.Col = T
        End If
    End Sub

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 13 Apr 2020, 11:35 AM
Hello, Rick,

Thank you for sharing the code snippet.

According to the provided piece of code, it seems that after you press Keys.Enter, an item gets updated by the values of the current row's cells. I believe that this part of the code can be successfully moved to the CellValueChanged event. Thus, no matter whether Tab or Enter key is pressed, once the value is committed to the cell, the CellValueChanged event will be fired and you will be able to store the changes to the item you need. 

However, if you still prefer the way of handling the editor's key, note that it is necessary to subscribe to the PreviewKeyDown event of the hosted TextBox inside the editor:
    Sub New()
         
        InitializeComponent()

        AddHandler Me.RadGridView1.CellEditorInitialized, AddressOf RadGridView1_CellEditorInitialized

    End Sub

    Private Sub RadGridView1_CellEditorInitialized(sender As Object, e As Telerik.WinControls.UI.GridViewCellEventArgs)

        Dim tbEditor As RadTextBoxEditor = TryCast(e.ActiveEditor, RadTextBoxEditor)
        If Not tbEditor Is Nothing Then

            Dim tbElement As RadTextBoxEditorElement = CType(tbEditor.EditorElement, RadTextBoxEditorElement)
            RemoveHandler tbElement.TextBoxItem.TextBoxControl.PreviewKeyDown, AddressOf TextBoxControl_PreviewKeyDown
            AddHandler tbElement.TextBoxItem.TextBoxControl.PreviewKeyDown, AddressOf TextBoxControl_PreviewKeyDown
            
        End If

    End Sub 
 
    Private Sub TextBoxControl_PreviewKeyDown(sender As Object, e As PreviewKeyDownEventArgs)
        If e.KeyData = Keys.Enter Then
            Console.WriteLine("Enter")
        ElseIf e.KeyData = Keys.Tab Then
            Console.WriteLine("Tab") 
        End If 
    End Sub
Feel free to use this approach which suits your requirements best. 

Should you have further questions please let me know.

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
0
Dev
Top achievements
Rank 1
Veteran
answered on 17 Apr 2020, 05:37 AM

Hi Dess,

    Thank you so much for your reply. This is working good.

Tags
GridView
Asked by
Dev
Top achievements
Rank 1
Veteran
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Dev
Top achievements
Rank 1
Veteran
Share this question
or