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

PropertyGridDropDownListEditor select the first item automatically

1 Answer 117 Views
PropertyGrid
This is a migrated thread and some comments may be shown as answers.
Steve
Top achievements
Rank 1
Veteran
Steve asked on 04 Aug 2020, 03:24 AM

Why does the PropertyGridDropDownListEditor automatically select the first option for me, is it possible to change this behavior?I want to keep things as they are when I have not chosen to.

public partial class RadForm1 : Telerik.WinControls.UI.RadForm
        {
        private class Info
            {
            [Editor(typeof(PropertyGridDropDownListEditor), typeof(BaseInputEditor))]
            public string Address { get; set; }
            }
 
        public RadForm1()
            {
            InitializeComponent();
            }
 
        private void RadForm1_Load(object sender, EventArgs e)
            {
            radPropertyGrid1.SelectedObject = new Info { Address = "B" };
            }
 
        private void radPropertyGrid1_EditorInitialized(object sender, PropertyGridItemEditorInitializedEventArgs e)
            {
            PropertyGridDropDownListEditor ddl = e.Editor as PropertyGridDropDownListEditor;
            if (ddl != null)
                {
                BaseDropDownListEditorElement el = ddl.EditorElement as BaseDropDownListEditorElement;
                el.DataSource = new List<string> { "A", "B", "C" };
                el.Text = ((PropertyGridItem)e.Item).Value + "";
                }
            }
        }

1 Answer, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 06 Aug 2020, 05:55 AM

Hello, Steve,

By default, setting the DataSource property for RadDropDownList or BaseDropDownListEditorElement in particular makes the first item selected as it gets current in the collection. Please also have in mind that the EditorInitialized event is fired after the editor in RadPropertyGrid has been initialized and you are free to do any customizations to item. That is why it would be necessary to specify the selected item in the editor according to the value of the item being edited.

An alternative solution is to handle the EditorRequired event which is fired much earlier and bind the editor there. Thus, the editor's value will be synchronized with the value in the PropertyGridItem

        private class Info
        {
            [Editor(typeof(PropertyGridDropDownListEditor), typeof(BaseInputEditor))]
            public string Address { get; set; }
        }

        public RadForm1()
        {
            InitializeComponent();

            radPropertyGrid1.SelectedObject = new Info { Address = "B" };  

            radPropertyGrid1.EditorRequired+=radPropertyGrid1_EditorRequired;
        }

        private void radPropertyGrid1_EditorRequired(object sender, PropertyGridEditorRequiredEventArgs e)
        {
            if (e.EditorType==typeof(PropertyGridDropDownListEditor))
            {
                PropertyGridDropDownListEditor ddl = new PropertyGridDropDownListEditor();
                BaseDropDownListEditorElement el = ddl.EditorElement as BaseDropDownListEditorElement;
                el.DataSource = new List<string> { "A", "B", "C" };
                e.Editor = ddl;
            }
        } 

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

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