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

How to use standard Editors with runtime added properties

5 Answers 283 Views
PropertyGrid
This is a migrated thread and some comments may be shown as answers.
Dennis
Top achievements
Rank 1
Dennis asked on 20 Oct 2014, 10:53 AM
I am building the property grid at runtime and I want to use the standard property editors/Attributes  such as Password, browser, dropdown list, etc.    I've found examples of how to write custom editors, but in this case I want to leverage the build in editors.    How can I assign editor attributes at runtime for programmatically added items in a property grid ?

This is the WinForm version of the control , not WPF

5 Answers, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 23 Oct 2014, 08:08 AM
Hello Dennis,

Thank you for writing.

It is possible to specify the used editor for a certain property by using the EditorRequired event and setting the EditorType property:
private void radPropertyGrid1_EditorRequired(object sender, PropertyGridEditorRequiredEventArgs e)
{
    if (e.Item.Name == "String")
    {
        e.EditorType = typeof(PropertyGridDropDownListEditor);
    }
}

Additionally if you populate the RadPropertyGrid via RadPropertyStore with PropertyStoreItems, you are allowed to add attributes, including EditorAttribute as well:
PropertyStoreItem intItem = new PropertyStoreItem(typeof(int), "Integer", 1);
intItem.Attributes.Add(new EditorAttribute(typeof(PropertyGridBrowseEditor), typeof(BaseInputEditor)));
 
//some other store items
 
RadPropertyStore store = new RadPropertyStore();
store.Add(intItem);
 
this.radPropertyGrid1.SelectedObject = store;

I hope this information helps. Should you have further questions, I would be glad to help.

Regards,
Desislava
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Dennis
Top achievements
Rank 1
answered on 24 Oct 2014, 01:40 AM
Thanks, this is exactly what I was looking for.  
On this approach if I want to use a PropertyGridDropDownListEditor  how do I set the properties of the editors when I'm adding an item to the store ?  For example, provide a list of values for the dropdownlist,   I can add Passwordtext   psItem.Attributes.Add(new PasswordPropertyTextAttribute(true));
and a numeric range to a spin edit, but can't seem to find an attribute to supply the drop down list its set of values
   psItem.Attributes.Add(new EditorAttribute(typeof(Telerik.WinControls.UI.PropertyGridSpinEditor), typeof(BaseInputEditor)));
psItem.Attributes.Add(new RadRangeAttribute(1, 100));

PropertyStoreItem intItem = new PropertyStoreItem(typeof(int), "Integer", 1);intItem.Attributes.Add(new EditorAttribute(typeof(PropertyGridBrowseEditor), typeof(BaseInputEditor)));
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 28 Oct 2014, 01:07 PM
Hello Dennis,

Thank you for writing back.

If I understand your requirement correctly, you are trying to populate the PropertyGridDropDownListEditor with items. For this purpose, you should use the EditorInitialized event and set the BaseDropDownListEditorElement.DataSource property to the desired items collection:
private void radPropertyGrid1_EditorInitialized(object sender, PropertyGridItemEditorInitializedEventArgs e)
{
    PropertyGridDropDownListEditor ddl = e.Editor as PropertyGridDropDownListEditor;
    if (ddl != null && e.Item.Name == "ItemName")
    {
        BaseDropDownListEditorElement element = ddl.EditorElement as BaseDropDownListEditorElement;
        if (element.DataSource == null)
        {
            element.DataSource = GetDataTable();
            element.DisplayMember = "Name";
            element.ValueMember = "Id";
        }
 
        PropertyGridItem pgi = e.Item as PropertyGridItem;
        element.SelectedValue = int.Parse(pgi.Value + "");
    }
}
 
private object GetDataTable()
{
    DataTable dt = new DataTable();
    dt.Columns.Add("Id", typeof(int));
    dt.Columns.Add("Name", typeof(string));
 
    for (int i = 0; i < 5; i++)
    {
        dt.Rows.Add(i, "Item" + i);
    }
    return dt;
}

If it is not the exact requirement, please specify in details the expected behavior. Thus, we would be able to investigate the precise case and assist you further.

I hope this information helps. If you have any additional questions, please let me know.

Regards,
Desislava
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Juan Gustavo
Top achievements
Rank 1
answered on 10 Nov 2015, 11:35 PM

Hi 

I am  using this editor   but I need to  allow to add new items 

I changed the DropDownStyle  but does not take  effect​

It's necessary to set another property?

 Regards

 

PropertyGridDropDownListEditor editor = new PropertyGridDropDownListEditor();
BaseDropDownListEditorElement element = editor.EditorElement as BaseDropDownListEditorElement;
 
element.DataSource = new Dictionary<string, string>();
element.DataSource = ParameterGroup;
element.DisplayMember = "Value";
element.ValueMember = "Key";
element.DropDownStyle = RadDropDownStyle.DropDown;
element.SelectedValue = item.Value ?? "";
 
e.Editor = editor;

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 13 Nov 2015, 11:46 AM
Hello Juan,

Thank you for writing.

PropertyGridDropDownListEditor allows only valid values which are available in the DataSource collection. If your requirement is to allow the user to enter custom values, these new values should be added in the DataSource. I have prepared a sample code snippet which result is illustrated on the attached gif file: 
DataTable dt = new DataTable();
 
public Form1()
{
    InitializeComponent();
    dt.Columns.Add("Id", typeof(int));
    dt.Columns.Add("Name", typeof(string));
 
    for (int i = 0; i < 5; i++)
    {
        dt.Rows.Add(i, "Item" + i);
    }
     
    PropertyStoreItem intItem = new PropertyStoreItem(typeof(int), "Integer", 1);
    intItem.Attributes.Add(new EditorAttribute(typeof(PropertyGridBrowseEditor),
        typeof(BaseInputEditor)));
 
    PropertyStoreItem floatItem = new PropertyStoreItem(typeof(string), "ItemName", 1,
        "Property storing a value.");
    floatItem.Attributes.Add(new EditorAttribute(typeof(PropertyGridDropDownListEditor),
        typeof(BaseInputEditor)));
 
    PropertyStoreItem stringItem = new PropertyStoreItem(typeof(string), "String", "telerik",
        "Property storing a string value", "Telerik");
    PropertyStoreItem dockItem = new PropertyStoreItem(typeof(DockStyle), "Dock", DockStyle.Top,
        "Property containing DockStyle value", "Layout", false);
 
    RadPropertyStore store = new RadPropertyStore();
    store.Add(intItem);
    store.Add(floatItem);
    store.Add(stringItem);
    store.Add(dockItem);
 
    this.radPropertyGrid1.SelectedObject = store;
 
    this.radPropertyGrid1.EditorRequired += radPropertyGrid1_EditorRequired;
    this.radPropertyGrid1.EditorInitialized += radPropertyGrid1_EditorInitialized;
}
 
public class CustomPropertyGridDropDownListEditor : PropertyGridDropDownListEditor
{
    DataTable dt;
 
    public override void BeginEdit()
    {
        base.BeginEdit();
        BaseDropDownListEditorElement element = this.EditorElement as BaseDropDownListEditorElement;
        element.DropDownStyle = Telerik.WinControls.RadDropDownStyle.DropDown;
        if (element.DataSource != null)
        {
            dt = element.DataSource as DataTable;
        }
    }
 
    public override bool EndEdit()
    {
        BaseDropDownListEditorElement element = this.EditorElement as BaseDropDownListEditorElement;
         
        if (dt != null)
        {
            foreach (DataRow row in dt.Rows)
            {
                if (row[element.DisplayMember].ToString() == element.Text)
                {
                    return base.EndEdit();
                }
            }
            dt.Rows.Add(dt.Rows.Count, element.Text);
        }
        PropertyGridItemElement itemElement = this.OwnerElement as PropertyGridItemElement;
        PropertyGridItem item = itemElement.Data as PropertyGridItem;
        if (item != null)
        {
            item.Value = element.SelectedValue;
        }
        return base.EndEdit();
    }
}
 
private void radPropertyGrid1_EditorInitialized(object sender, PropertyGridItemEditorInitializedEventArgs e)
{
    PropertyGridDropDownListEditor ddl = e.Editor as PropertyGridDropDownListEditor;
    if (ddl != null && e.Item.Name == "ItemName")
    {
        CustomPropertyGridDropDownListEditor editor = new CustomPropertyGridDropDownListEditor();
        BaseDropDownListEditorElement element = editor.EditorElement as BaseDropDownListEditorElement;
        PropertyGridItem pgi = e.Item as PropertyGridItem;
        element.SelectedValue = int.Parse(pgi.Value + "");
    }
}
 
 
private void radPropertyGrid1_EditorRequired(object sender, PropertyGridEditorRequiredEventArgs e)
{
    if (e.Item.Name == "ItemName")
    {
        CustomPropertyGridDropDownListEditor editor = new CustomPropertyGridDropDownListEditor();
        BaseDropDownListEditorElement element = editor.EditorElement as BaseDropDownListEditorElement;
        if (element.DataSource == null)
        {
            element.DataSource = dt;
            element.DisplayMember = "Name";
            element.ValueMember = "Id";
        }
        e.Editor = editor;
    }
}

Note that this is just a sample approach and it may not cover all possible cases. Feel free to modify it in a way which suits your requirement best.

I hope this information helps. Should you have further questions I would be glad to help.
 
Regards,
Dess
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
PropertyGrid
Asked by
Dennis
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Dennis
Top achievements
Rank 1
Juan Gustavo
Top achievements
Rank 1
Share this question
or