Unplanned
Last Updated: 24 Jan 2021 20:59 by Luca
Luca
Created on: 14 Jan 2021 06:04
Category: PropertyGrid
Type: Bug Report
0
RadPropertyGrid: NullReferenceException when activating the editor

The clients needs to use a PropertyGridDropDownListEditor and confirms the new value whenever a new selection is made. The attached gif file illustrates how to replicate the error using the following code snippet:

        public RadForm1()
        {
            InitializeComponent();

            this.radPropertyGrid1.SelectedObject = this; 
            this.radPropertyGrid1.EditorInitialized += radPropertyGrid1_EditorInitialized; 
        } 

        private void radPropertyGrid1_EditorInitialized(object sender, PropertyGridItemEditorInitializedEventArgs e)
        {
            PropertyGridDropDownListEditor ddl = e.Editor as PropertyGridDropDownListEditor;
            if (ddl != null)
            {
                ddl.LoopValuesOnDoubleClick = false;
                BaseDropDownListEditorElement element = ddl.EditorElement as BaseDropDownListEditorElement;

                if (element != null)
                { 
                    element.SelectedValueChanged -= Element_SelectedValueChanged;
                    element.SelectedValueChanged += Element_SelectedValueChanged;
                }
            }
        }
 
        private void Element_SelectedValueChanged(object sender, Telerik.WinControls.UI.Data.ValueChangedEventArgs e)
        {
            this.radPropertyGrid1.EndEdit();
        }

Workaround:

        public RadForm1()
        {
            InitializeComponent();

            this.radPropertyGrid1.SelectedObject = this;  

            this.radPropertyGrid1.ValueChanged += RadPropertyGrid1_ValueChanged;
        }

        private void RadPropertyGrid1_ValueChanged(object sender, EventArgs e)
        {
            if (this.radPropertyGrid1.ActiveEditor is PropertyGridDropDownListEditor)
            {
                this.radPropertyGrid1.EndEdit();
            }
        }

Attached Files:
2 comments
Luca
Posted on: 24 Jan 2021 20:59

Hello Dess,

sorry for being late on replying you. I tested the solution you propose (unsuscribing in the SelectedValueChanged event itself, before calling EndEdit) and it works fine. We will consider this option form now in similar cases... for the issue above we'll keep the implementation with the SelectedValue event.

Thank you so much!

Ciao,
Luca

ADMIN
Dess | Tech Support Engineer, Principal
Posted on: 15 Jan 2021 13:55

Hello, Luca, 

I would like to follow up with some additional information about the editing behavior in RadPropertyGrid and what changes are expected after addressing this issue. When you try to edit a property value, the EditorRequired event gets fired allowing you to specify an editor type if you want to replace the existing one. Then, RadPropertyGrid internally initializes this editor (e.g. PropertyGridDropDownListEditor), setup its DataSource (which sets the SelectedValue to the first record by default), initializes the SelectedValue in correspondence with the property value and fires the EditorInitialized event. This is the appropriate place to customize the editor if you need. For example, in your case you subscribe to the BaseDropDownListEditorElement.SelectedValueChanged event in which event handler you force the EndEdit method. Thus, whenever you change the selection in the editor, this new value is committed to the property and the editor is closed. The next time you try to edit this property, the EditorRequired is again fired, and the internal initialization is performed. Since RadPropertyGrid reuses its editors, you will still have a subscription to the SelectedValueChanged event for the previously used BaseDropDownListEditorElement and setting the SelectedValue during the initial initialization will trigger your event. This will force the EndEdit method and the editor will be closed and set to null in the middle of the initialization process. That is why the NullReferenceException occurs. We will handle this error in order to prevent such a user experience. However, the second time you activate the editor, it will get closed. That is why the appropriate solution in this case is either to unsubscribe from the BaseDropDownListEditorElement.SelectedValueChanged before calling EndEdit or use the RadPropertyGrid.ValueChanged event.

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

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/.