Click here to Skip to main content
15,358,528 members
Articles / Desktop Programming / Windows Forms
Technical Blog
Posted 11 Feb 2010

Tagged as


8 bookmarked

PropertyGrid Collection Events

Rate me:
Please Sign up or sign in to vote.
3.00/5 (9 votes)
2 Apr 2010CPOL2 min read
When changing collections in an object through PropertyGrid, the PropertyChanged event doesn't get called.

You are using the PropertyGrid control to allow the user to edit the properties of an object at run-time from the UI. This works great and is in fact very reliable since this is the same control used by Visual Studio to display the properties of controls. Now that you are letting the user update the object directly, you'd like to get a PropertyValueChanged event whenever the user changes a property, so you can respond to the user’s action. You'll be able to do that by adding a PropertyValueChanged event to the PropertyGrid, which will get triggered every time a property is changed, unless the property is a collection. So if you have a collection inside the property grid selected object and you'd like to be notified when a property changes in that collection or when an item is added\removed, there is no way to do so with the OOB Microsoft PropertyGrid control. Clearly, the reason why the PropertyValueChanged event is not triggered when a collection is changed is because the reference to the collection object is still the same.

In order to be notified when a property changes inside a collection, follow instructions on this blog. You'll find this very helpful. However, this solution can't help you if you'd like to be notified when an item is removed from the collection or when it is added without any of its properties changed. A more generic solution I found was to get a notification when the collection form is closed. This way, I wouldn't have to worry too much about the steps the user is taking in the collection editor. When the collection form is closed, I get notified and act on the collection object based on its current state.


First, add a reference to System.Design DLL in your Visual Studio project. Create the following class, which represents a collection editor that triggers a MyFormClosed event when the collection editor form is closed.

using System;
using System.ComponentModel.Design;
using System.Windows.Forms;

public class MyCollectionEditor : CollectionEditor
    public delegate void MyFormClosedEventHandler(object sender,
                                        FormClosedEventArgs e);

    public static event MyFormClosedEventHandler MyFormClosed;

    public MyCollectionEditor(Type type) : base(type) { }
    protected override CollectionForm CreateCollectionForm()
        CollectionForm collectionForm = base.CreateCollectionForm();
        collectionForm.FormClosed += new FormClosedEventHandler(collection_FormClosed);
        return collectionForm;

    void collection_FormClosed(object sender, FormClosedEventArgs e)
        if (MyFormClosed != null)
            MyFormClosed(this, e);

To use the above collection editor for the property grid, you can simply add the following attribute above the collection Property of the object which you set to propertGrid.SelectedObject.

[Editor(typeof(MyCollectionEditor), typeof(UITypeEditor))]

The final part is to create our custom FormClosed event handler and bind it to our custom collection editor as follows:

public MyForm()

    //  Make the property grid listen to collection properties changes
    MyCollectionEditor.MyFormClosed += new MyCollectionEditor.MyFormClosedEventHandler

private void propertyGrid_CollectionFormClosed(object s, FormClosedEventArgs e)
    //  Code to run when collection form is closed

Posted in .NET, C# Tagged: collection, collection editor, CollectionEditor, event, FormClosed, FormClosedEventHandler, propertygrid, propertyvaluechanged, PropertyValueChangedEventHandler


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Ali BaderEddin
Software Developer Qualtrics
United States United States

Comments and Discussions

SuggestionWorking VB code Pin
atverweij11-May-16 0:20
Memberatverweij11-May-16 0:20 
GeneralMy vote of 1 Pin
BillWoodruff7-Jan-15 23:15
mveBillWoodruff7-Jan-15 23:15 
GeneralRe: My vote of 1 Pin
Ali BaderEddin8-Jan-15 11:27
MemberAli BaderEddin8-Jan-15 11:27 

does this count as a response to use questions?
-- Ali B

GeneralMy vote of 2 Pin
mnuaimi21-Sep-14 9:28
Membermnuaimi21-Sep-14 9:28 
QuestionNice idea Pin
Bernhard Hiller26-Oct-11 3:53
MemberBernhard Hiller26-Oct-11 3:53 
Questioncant translate to Pin
colin2205587-Sep-11 4:24
Membercolin2205587-Sep-11 4:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.