Click here to Skip to main content
Click here to Skip to main content

Using reflection to fill ListViews with arbritary objects

, 21 Jan 2004
Rate this:
Please Sign up or sign in to vote.
How to make the ListView to display objects regardless of type.

Sample Image - listviewadapter.gif

Introduction

An often recurring task is to visualize collections of objects. This article explains how to use an adapter class to fill a ListView control with completely arbitrary objects.

Using the ListViewAdapter

The ListViewAdapter must get some basic information about which properties it should display:

adapter.AddBinding("Name","The name",100);
adapter.AddBinding("Comment","Comments?",100);
adapter.Fill(myListView,myCollection);

This is enough to get the adapter to display your objects. You could throw an object at it that doesn't have any of the properties Name and Comment. It would work, but a blank row will just look silly, so please don't.

The myCollection object must implement the interface IEnumerable. Most collections do, so this shouldn't be a problem.

How it works

What makes this piece of magic work is - as most of you probably have figured out - reflection. Reflection is used to discover type information runtime to handle objects that are unknown when you wrote the code.

The first step when the adapter fills a ListView with objects is to remove existing columns and ListViewItems. Secondly, it iterates through the bindings and creates the columns. Then it starts filling the ListView with items:

  Type t=obj.GetType();
  ListViewItem item=new ListViewItem();
  bool first=true;

  foreach(ColumnBinding b in bindings)
  {
    PropertyInfo p=t.GetProperty(b.Property);

    string text=string.Empty;
    if(p!=null)
    {
      //Fetch the value of the property of the object
      object val=p.GetValue(obj,null);

    //Parse the object
    if(val!=null)
    {
      text=b.Parser.Parse(val);
    }
  }

  //The first column gets its text from the Text property.
  //Thats why we must store the result of the first binding
  //in item.Text and the subsequent in subitems.
  if(first)
    item.Text=text;
  else
    item.SubItems.Add(text);
  first=false;
}

This is the part of the code that builds the actual ListViewItem. It is pretty straightforward. It loops through the column bindings and tries to get the value, as an object, of the property that is bound to the column. Then it sends the object to the parser, which I will explain later, and adds the string returned from the parser to the ListViewItem.

There is some additional code which handles images. But if you get the principles of this part of the code, you'll understand how it works when you look at it.

The Parser

The Parser arose from the need to display information that was interpreted in some way. It is not always desirable to display the result of a .ToString() call. A Parser object doesn't do anything else than return the result of .ToString(). But it can be extended to allow more creative interpretations.

An example of a class that extends Parser is the CustomParser class. The custom parser overrides the Parse method of its base class and replaces it with a method that uses a delegate to parse the object. So if you don't feel like writing a new class just to parse an object, you can just use the custom parser and provide it with a suitable delegate to do the job.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Hugo Wetterberg
Web Developer
Sweden Sweden
I'm 23 years old and I study informatics at the university of Lund. I'm currently working on my bachelor thesis on predicate logic in .Net.
 
When it comes to development I'm totally hooked on .Net, it is probably the most exiting platform to work on today, and I'm looking forward to its second incarnation in Longhorn.

Comments and Discussions

 
Generalbravo! i do not know why no one else did this Pinmembersermit10-Apr-10 2:25 
GeneralUse TypeConverters for convenience and abstraction PinmemberNick Hounsome30-Jun-05 23:10 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140902.1 | Last Updated 22 Jan 2004
Article Copyright 2004 by Hugo Wetterberg
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid