Updates from January, 2010 Toggle Comment Threads | Keyboard Shortcuts

  • Ali BaderEddin 11:41 pm on January 16, 2010 Permalink | Reply  

    Sortable ListView 

    The Windows Forms ListView control doesn’t provide column sorting functionality. So if you click on a column in a ListView Details view, don’t expect the items to be sorted by the clicked column. To get this functionality, we’ll need to sort the items by the clicked column in the ListView ColumnClick event. I searched online for “Sortable ListView” and I found three MSDN articles talking about this: Sort ListView Column in Visual C#, Sorting ListView Items by Column Using Windows Forms, and How to: Sort ListView Items. None of those implementations takes into consideration the type of the column being sorted. That is, they all do string sorting. If you have dates and numbers in your list, then they’ll not be sorted properly. For example, number 2 will be considered greater than 11. Date time 9/9/1400 will be considered greater than 11/11/2020. Below is an implementation that takes into consideration string, DateTime, int and double types. It can be easily extended to handle more types.

    • Add the SortableListView control to your Windows Form
    • When adding columns to the SortableListView, set the Tag attribute to the type of the column.
    sortableListView.Columns.Add("String Field").Tag = typeof(string);
    sortableListView.Columns.Add("DateTime Field").Tag = typeof(DateTime);
    sortableListView.Columns.Add("Int Field").Tag = typeof(int);
    sortableListView.Columns.Add("Double Field").Tag = typeof(double);
    • Now, you can add the items as usual.

    For example, the below list is sorted by the DateTime field.

    Sortable ListView

    Sortable ListView

     Click on the above image to download the SortableListView control.

    • Mayank Raichura 9:21 am on October 6, 2011 Permalink | Reply

      Great article and a really nice control. Just one question…. why didn’t you add a new Property of, say, ColumnType? I mean this could let the tag property to be used for other purposes….

      Anyways….thank you very much for the control….its really really awesome…

  • Ali BaderEddin 9:53 pm on January 12, 2010 Permalink | Reply
    Tags: collection, collection editor, CollectionEditor, event, FormClosed, FormClosedEventHandler, propertygrid, propertyvaluechanged, PropertyValueChangedEventHandler   

    PropertGrid Collection Events 

    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 PropertValueChanged 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
    • Eugen 7:24 am on January 13, 2010 Permalink | Reply

      Thanks for adding a link to my blog article on your post. I am glad you consider it useful.

    • Muzammil Rehman 10:58 am on January 1, 2012 Permalink | Reply

      Thanks, great job, panic resolving article.

    • jhdhfdf 4:11 am on November 8, 2012 Permalink | Reply

      I GOT ERROR LIKE CONSTRUCTOR OF TYPE sYSTEM.String not found if i follw this code.

Compose new post
Next post/Next comment
Previous post/Previous comment
Show/Hide comments
Go to top
Go to login
Show/Hide help
shift + esc

Get every new post delivered to your Inbox.

%d bloggers like this: