DataGridView FTW

I recently ran across this situation:

The user wants to have a bunch of data displayed with some checkboxes to select the data. The user also wants to be able to sort data and show only entries that are already checked. The user also wants to be able to filter the displayed data and only see records that match a piece of text.

I’ve worked on dialogs in the past that have had similar behaviour, and in the past have used both DataGridViews and ListViews (with a visible checkbox column) to do a job like this. This time proved to be a little more difficult than previous attempts.

At the start, I tried out both controls – the ListView and the DataGridView. Both were capable of displaying data with a checkbox column. After working with DataGridViews on several other dialogs, it seemed like the logical choice. I was able to get the data displaying properly, along with a checkbox column in hardly any time. That’s when I hit a small snag: how DataGridViews handle selections.

When some contents of a DataGridView are selected, it ‘hightlights’ them (in a blue color, just like Word, or Notepad). As soon as you click on a different cell, it selects that cell and de-selects whatever was previously selected. This means that if you have a checkbox column, and you want to check a bunch of things at once, you can’t just highlight the rows and click a single checkbox.* Perhaps is there some way around this, but I didn’t easily see one, so I went ahead and tried a ListView.

The ListView was slightly more annoying to set up (in my opinion, after working with a DataGridView where you have more control over columns). Getting things to display wasn’t a problem, but getting them to display nicely was a problem. It took a bit of messing around to get things to display in a nice list properly, as opposed to the old-school ‘Microsoft Windows Explorer Icon and sub-items for everything’ type view. Setting up the checkbox column was easier, and out of the box it did allow users to select a bunch of items at a time and click a single checkbox to select a bunch of items at once. The downside? Less control over managing the data within the control. Perhaps again I’m just used to working with DataGridViews, but whipping through a row and grabbing data is a piece of cake – you just grab whatever data out of each column you need. The ListView has a collection of ListViewSubItems. That, and instead of storing Ids in the Tag property of a ListViewItem, you can easily put them in an invisible column on a DataGridView. This can come in handy for debugging purposes. ListView? yeah, not quite as easy. After getting fairly far into using the ListView control I sort of wish I would have stuck with the DataGridView.

My personal conclusion:
When you need to display data and allow a user to check a bunch of items in one click, use a ListView

For everything else, use a DataGridView.

*There are ways around this, such as using a context menu with a “Check selected” type menu item, but this wasn’t really the ideal solution in this situation. There’s a school of thought that anything that is available in a context menu should also be available elsewhere (through buttons or other menu items). There is some truth to this sort of design, but that is perhaps a topif for a another discussion.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s