Tuesday, November 25, 2008

ObjectListView - “How I Learned to Stop Worrying and Love .NET's ListView”

CodeProject - A Much Easier to Use ListView

ObjectListView

  ReportModernExample

All projects suffer creeping featurism. Things that start simple and elegant end up as the "before" image in a weight-loss ad. This control has grown considerably since it was first written. For those in a hurry, this control has the following major features:

  • It can easily display a list of objects in a ListView, including automatically sorting and grouping.
  • It has a version (FastObjectListView) that can build a list of 10,000 objects in less than 0.1 seconds. [v1.9.1]
  • It can easily edit the values shown in the ListView [v1.8].
  • It can trivially produce nice reports from the ListView [v1.7].
  • It supports data binding.
  • It supports millions of rows through ListView's virtual mode.
  • It supports all ListView views (report, tile, large and small icons).
  • It supports owner drawing, including rendering animated GIFs.
  • Its columns can be fixed-width or limited to a minimum/maximum.
  • It shows a "list is empty" message when the list is empty (obviously).
  • Its row height can be explicitly set.
  • It supports user selection of visible columns by right clicking on the header [v1.9].
  • It supports columns that automatically resize to fill any unoccupied width [v1.10].

This control has many features. If you want to do something with a ListView, this code probably has some code to help you do it.

This control now has its own website, hosted by SourceForge: ObjectListView - How I Learned To Stop Worrying and Love .NET's ListView. This is not an empty shell site. It actually has lots of useful information.

Those who aren't in a hurry can now read the rest of the article. :-)

The ListView "Problem"

I often find that I have a collection of objects which I want to present to the user in some sort of tabular format. It could be the list of clients for a business, a list of known FTP servers or even something as mundane as a list of files in a directory. User interface-wise, the ListView is the perfect control for these situations. However, I find myself groaning at the thought of using the ListView and secretly hoping that I can use a ListBox instead.

The reason for wanting to avoid the ListView is all the boilerplate code it needs to work: make the ListViewItems, add all the SubItems, catch the header click events and sort the items depending on the data type. Each of these tasks is slightly different for each instance of a ListView. If you want to support grouping, there's an even bigger chunk of boilerplate code to copy and then modify slightly.

For a basically lazy person, this is far too much work. ObjectListView was born to relieve this workload.

Unlearn You Must

For those of you who have struggled with a ListView before, you must unlearn. An ObjectListView is not a drop in replacement for a ListView. If you have an existing project, you cannot simply create an ObjectListView instead of creating a ListView. An ObjectListView operates in a more declarative manner than a ListView -- you tell an ObjectListView what you want it to do, and it does it for you.

Beware of ListViewItems. You never need to add ListViewItems to an ObjectListView. If you find yourself adding things to the Items collection, creating ListViewItems, or adding sub-items to anything, then you need to stop — you are being seduced to the dark side. An ObjectListView does all that work for you. You tell it the aspects you want to show on each object (via the OLVColumn objects) and then you give it the list of objects to show.

…”

ObjectListView Project Home

“ObjectListView is a C# wrapper around a .NET ListView. It makes the ListView much easier to use and provides some neat extra functionality.

Larry Wall, the author of Perl, once wrote that the three essential character flaws of any good programmer were sloth, impatience and hubris. Good programmers want to do the minimum amount of work (sloth). They want their programs to run quickly (impatience). They take inordinate pride in what they have written (hubris).

ObjectListView encourages the vices of sloth and hubris, by allowing programmers to do far less work but still produce great looking results.

You can download a demonstration of the ObjectListView in action. This demo includes ObjectListView.cs which is the file you need to include in your project.

Once you have the demo, you might want to look at the Getting Started and the Cookbook sections. …”

It’s a good day when I find a new (well, okay it’s been around for a couple years… so it’s new to me… ;) WinForm ListView. Like many .Net developers I have a love/hate relationship with the ListView, so am almost always willing to enhance/replace it with a better implementation. From what I’ve read, ObjectListView looks to be easily worth a look. It’s been active for a couple years, has a nice level of doc’s, it’s free and best of all the source is available. And any project that tries to help you from “being seduced to the dark side” can’t be all that bad? 

 

(via WindowsClient.NetArticles [GD:There’s no permalink to the WindowsClient.Net entry… :( ] )

No comments: