locked
ListView with a custom item collection RRS feed

  • Question

  • I have a Visual Studio 2008 C# .NET 3.5 project with a UserControl based on a ListView. But, the data I want the ListView to display isn't just strings. How can I get the ListView to be associated with a custom item such as the one in the code block below instead of a ListViewItem stored in a ListViewItemCollection?

    public class MyItem
    {
        private string info1_;
        private string note_;
        private byte[] info2_;
        private int info3_;
    
        // ...
    }
    
    public class MyItemCollection : IList<MyItem>,
                                    ICollection<MyItem>,
                                    IEnumerable<MyItem>
    {
        // Implements IList<>, ICollection<>, & IEnumerable<>...
    }


    Thanks,
    PaulH
    Tuesday, September 1, 2009 8:21 PM

Answers

  • The ListView is a difficult choice to implement this kind of functionality.  It requires you to set OwnerDraw to true, and use GDI to manually draw each and every item you want to display by either handling the DrawItem event of the ListView, or overriding OnDrawItem, if the ListView is the base class for a custom display class.

    A good workaround would be to use the FlowLayoutPanel instead.  You would do it like this:

    1. Create a UserControl that displays the data in the format you want.  Make sure it binds to just one of your MyItem instances.
    2. Add a FlowLayoutPanel to your form.
    3. Adjust the properties of the FlowLayoutPanel to fit your specific needs.  The AutoScroll, FlowDirection and WrapContents properties are a few properties of FlowLayoutPanel worth experimenting with.
    4. Add a bunch of these user control instances that were created in step 1 to your FlowLayoutPanel. 

    If all goes well, you should have a nicely laid out set of items that look exactly like you want, and in a quarter of the time it'll take you to manually draw the item using the ListView.


    Coding Light - Illuminated Ideas and Algorithms in Software
    Coding Light WikiLinkedInForumsBrowser
    • Proposed as answer by Geert van Horrik Wednesday, September 2, 2009 5:46 AM
    • Marked as answer by PaulH79 Wednesday, September 2, 2009 5:28 PM
    Tuesday, September 1, 2009 8:34 PM

All replies

  • The ListView is a difficult choice to implement this kind of functionality.  It requires you to set OwnerDraw to true, and use GDI to manually draw each and every item you want to display by either handling the DrawItem event of the ListView, or overriding OnDrawItem, if the ListView is the base class for a custom display class.

    A good workaround would be to use the FlowLayoutPanel instead.  You would do it like this:

    1. Create a UserControl that displays the data in the format you want.  Make sure it binds to just one of your MyItem instances.
    2. Add a FlowLayoutPanel to your form.
    3. Adjust the properties of the FlowLayoutPanel to fit your specific needs.  The AutoScroll, FlowDirection and WrapContents properties are a few properties of FlowLayoutPanel worth experimenting with.
    4. Add a bunch of these user control instances that were created in step 1 to your FlowLayoutPanel. 

    If all goes well, you should have a nicely laid out set of items that look exactly like you want, and in a quarter of the time it'll take you to manually draw the item using the ListView.


    Coding Light - Illuminated Ideas and Algorithms in Software
    Coding Light WikiLinkedInForumsBrowser
    • Proposed as answer by Geert van Horrik Wednesday, September 2, 2009 5:46 AM
    • Marked as answer by PaulH79 Wednesday, September 2, 2009 5:28 PM
    Tuesday, September 1, 2009 8:34 PM
  • I think that's a darn fine solution. I'll go with that.

    -PaulH

    Wednesday, September 2, 2009 5:28 PM