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

Exception when grid regains focus

3 Answers 77 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Steve
Top achievements
Rank 1
Steve asked on 08 Mar 2016, 11:53 PM

I have a form with a grid on it, as well as some other controls. I can click between the grid and the other controls without any issues until I edit a cell using a custom editor through Editor Required. It seems that the grid may still be trying to give the control focus even through it isn't valid any more? Unfortunately the error only occurs on my test machine and not my dev machine, so I doubt that giving a sample would help. I am providing through the relevant snippets of code in the hopes that I am just doing something obviously wrong with setting up the custom editor.

First, this is the exception:

System.NullReferenceException: Object reference not set to an instance of an object.

   at Telerik.WinControls.UI.ImeSupport.CreateContext()
   at Telerik.WinControls.UI.ImeSupport.OnGotKeyboardFocus(Object sender, EventArgs e)
   at System.EventHandler.Invoke(Object sender, EventArgs e)
   at System.Windows.Forms.Control.OnGotFocus(EventArgs e)
   at Telerik.WinControls.RadControl.OnGotFocus(EventArgs e)
   at Telerik.WinControls.UI.RadGridView.OnGotFocus(EventArgs e)
   at System.Windows.Forms.Control.WmSetFocus(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at Telerik.WinControls.RadControl.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

This is the custom editor class:

    Private Class CustomTextEditor
        Inherits Telerik.WinControls.UI.RadTextBoxControlEditor
        Public Overrides Sub OnKeyDown(e As Forms.KeyEventArgs)
            Try
                If e.KeyCode = Forms.Keys.Enter Then
                    Dim Cell As Telerik.WinControls.UI.GridDataCellElement = DirectCast(Me.OwnerElement, Telerik.WinControls.UI.GridDataCellElement)
                    Dim View As Telerik.WinControls.UI.MasterGridViewTemplate = DirectCast(Cell.ViewTemplate, Telerik.WinControls.UI.MasterGridViewTemplate)
                    View.Owner.EndEdit()
                Else
                    MyBase.OnKeyDown(e)
                End If
            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try
        End Sub
    End Class

I still get the exception even if I never actually process any of my custom enter key code (it just passes through to MyBase.OnKeyDown

 

This is the code that creates the editor

    Private Sub OnCustomEditor(sender As Object, e As Telerik.WinControls.UI.EditorRequiredEventArgs)

        Try
            e.EditorType = GetType(CustomTextEditor)
        Catch ex As Exception
            Util.Exceptions.Exception.Display(ex)
        End Try
    End Sub

I initially called e.Editor = New CustomTextEditor(), but changed to set EditorType after some examples I found. However it doesn't make any difference.

3 Answers, 1 is accepted

Sort by
0
Dimitar
Telerik team
answered on 09 Mar 2016, 11:40 AM
Hi Steve,

Thank you for writing.

Perhaps you should check the editor type when you are replacing the default editor:
Private Sub radGridView1_EditorRequired(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.EditorRequiredEventArgs)
    If e.EditorType Is GetType(RadTextBoxEditor) Then
        e.EditorType = GetType(CustomTextEditor)
    End If
End Sub

The other code looks ok to me and I cannot see what can cause such exception. Nevertheless here is how you can achieve the same without creating a custom editor:
Private Sub radGridView1_CellEditorInitialized(ByVal sender As Object, ByVal e As GridViewCellEventArgs)
    If TypeOf e.ActiveEditor Is RadTextBoxEditor Then
        Dim element = TryCast(CType(e.ActiveEditor, RadTextBoxEditor).EditorElement, RadTextBoxEditorElement)
        RemoveHandler element.TextBoxItem.KeyDown, AddressOf TextBoxItem_KeyDown
        AddHandler element.TextBoxItem.KeyDown, AddressOf TextBoxItem_KeyDown
    End If
 
End Sub
 
Private Sub TextBoxItem_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
    If e.KeyCode = Keys.Enter Then
        radGridView1.EndEdit()
    End If
End Sub

I hope this helps. Should you have any other questions do not hesitate to ask.

Regards,
Dimitar
Telerik
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
0
Steve
Top achievements
Rank 1
answered on 09 Mar 2016, 06:53 PM
Ultimately the problem was that my custom text editor derived from RadTextBoxControlEditor instead of RadTextBoxEditor. Your documentation here leaves a lot to be desired. What is the difference between these classes and why does it matter? The pages for both classes say only "Represents a textbox editor in RadGridView." with no further detail.
0
Dimitar
Telerik team
answered on 10 Mar 2016, 12:07 PM
Hello Steve,

Thank you for writing back.

The difference is that RadTextBoxEditor uses RadTextBox, and RadTexBoxControlEditor uses RadTextBoxControl. The differences are listed in the following article: RadTextBoxControl vs RadTextBox.

Let me know if you have additional questions.

Regards,
Dimitar
Telerik
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
Tags
GridView
Asked by
Steve
Top achievements
Rank 1
Answers by
Dimitar
Telerik team
Steve
Top achievements
Rank 1
Share this question
or