refresh a collection in propertygrid
Asked Answered
D

2

0

hay all. i am using property grid to add or remove an object to a collection. but when the collectioneditor is closed only once the grid refreshes. after adding another object the grid wont get refresh. the collection in a list. i have seen many people with the same problem but no solutions. thansk

Densmore answered 16/9, 2010 at 13:25 Comment(0)
H
0

Implement INotifyCollectionChanged interface or use ObservableCollection class. see the link

Huelva answered 16/9, 2010 at 13:28 Comment(1)
can you give me a nice example please?Densmore
T
2

I realise i am very late to the party, but here it goes. I use this base class

public class CollectionEditorBase : CollectionEditor
{
    protected PropertyGrid ownerGrid;

    public CollectionEditorBase(Type type) : base(type) { }

    public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
    {
        PropertyInfo ownerGridProperty = provider.GetType().GetProperty("OwnerGrid", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
        ownerGrid = (PropertyGrid)ownerGridProperty.GetValue(provider);

        return base.EditValue(context, provider, value);
    }

    protected override CollectionForm CreateCollectionForm()
    {
        CollectionForm cf = base.CreateCollectionForm();
        cf.FormClosing += delegate(object sender, FormClosingEventArgs e)
        {
            ownerGrid.Refresh();
        };

        return cf;
    }
}

Then you just create a new Collectioneditor based on that. It will automatically refresh the property grid when the collection form is closed.

Be aware though, this solution is reflecting into the internals of the property grid and can be broken at any time, but i have done this for a while now with no problem

Tableland answered 5/11, 2015 at 22:21 Comment(0)
H
0

Implement INotifyCollectionChanged interface or use ObservableCollection class. see the link

Huelva answered 16/9, 2010 at 13:28 Comment(1)
can you give me a nice example please?Densmore

© 2022 - 2024 — McMap. All rights reserved.