I was looking at the ListView control in VS 2005 today and noticed the new addition of the ListViewGroup object. I didn't immediately know what this was but
intellisense's tooltip states that it "Represents a group of items displayed within a ListView control" and this was enough to peak my interest (I'm easily
pleased). For those like myself that are unaware of what a ListView group is, the following screen shot from Windows Explorer shows ListViewGroup's in
action:
Example of the ListViewGroup in Windows ExplorerSo I thought I would have a quick go at adding these ListViewGroup's to a
ListView control to see how easy it was. Well, it certainly wasn't quick and I must admit, not very intuitive. Being one of those people that expect
something to work in a pre-defined way, I didn't bother looking at the documentation and went straight to coding. I created a ListView control, set it's view
mode to details and commenced to add some columns to it. I then declared a new ListViewGroup and then started adding ListViewItem objects to the
ListViewGroups Items collection (seemed reasonable enough to me). Once I had added all of the items I wanted to the group, I then added the group to the
ListView control using the ListView.Groups.Add method. Rubbing my hands eagerly, I pressed F5 and hey presto, empty ListView :-(
Returning to the code,
I checked to ensure I hadn't done anything stupid, like create the ListViewItem's but forget to add them to the Group (never done that before ;)) but the
code looked fine. So I gave in and pressed F1 on the ListViewGroup and was surprised at how complex the example in the help documentation seemed. After all,
I only wanted to add a couple of groups to a ListView and add some items to each group. So then I decided to create a new project and add the groups and
items to the ListView control through the properties window and then looked at the .designer file to see how it had been done and to my surprise, it is
relatively simple. The problem I had was in thinking of the ListViewGroup as a collection of ListViewItems and that each ListViewGroup would then be added to
the ListView Control when in fact, you simply add the ListViewItem's like you normally would to the ListView control but if you want them to be in a group,
you then set the Group property of the ListViewItem to the group that you want the item to be associated to.
So after all of that, I finally had a
working example of using ListViewGroup's. Below is the code that I created to give me a similar output to that in the above screen shot. Just copy it into
the Form_Load event. Also, beware that the code only works for Windows XP and Windows 2003 (this is what I ascertained from the help
documentation).
| |
'Create the ListView control and add it to the Form's controls collection.
Dim lv As New ListView
lv.Dock = DockStyle.Fill
Me.Controls.Add(lv)
'Setup the ListView control.
With lv
'Add column headers to the ListView.
.Columns.Add("Name", 200, HorizontalAlignment.Left)
.Columns.Add("Type", 150, HorizontalAlignment.Left)
.Columns.Add("Total Size", 100, HorizontalAlignment.Right)
.Columns.Add("Free Space", 100, HorizontalAlignment.Right)
'Allow the columns to be re-ordered at run time
.AllowColumnReorder = True
'Set the initial display mode to Details
.View = View.Details
End With
'Create the "Hard Disk Drives" group
Dim hddGroup As New ListViewGroup("Hard Disk
Drives", HorizontalAlignment.Left)
hddGroup.Name = "HDDGroup"
'Create the "Devices with Removable Storage" group
Dim removableGroup As New ListViewGroup("Devices with
Removable Storage", HorizontalAlignment.Left)
removableGroup.Name = "RemovableStorage"
'Add the hddGroup and removableGroup ListViewGroups to the ListView control
lv.Groups.Add(hddGroup)
lv.Groups.Add(removableGroup)
'Create a ListViewItem for each drive and add to the ListView control
Dim li As ListViewItem
Dim volumeLabel As String = String.Empty
'Enumerate through the drives collection and add each drive name, type, total size and free space
'to the ListView control. A check is made to determine the type of drive which then determines
'which group to assign to the ListViewItem.
For Each driveName As System.IO.DriveInfo In
My.Computer.FileSystem.Drives
li = New ListViewItem
If driveName.IsReady = False OrElse
driveName.VolumeLabel.Length = 0 _
Then volumeLabel = driveName.DriveType.ToString Else
volumeLabel = driveName.VolumeLabel
li.Text = volumeLabel & " (" & driveName.Name.Substring(0, 2) & ")"
li.SubItems.Add(driveName.DriveType.ToString)
If driveName.IsReady = False Then li.SubItems.Add("0") Else li.SubItems.Add(driveName.TotalSize.ToString)
If driveName.IsReady = False Then li.SubItems.Add("0") Else li.SubItems.Add(driveName.TotalFreeSpace.ToString)
'Determine the drive type and assign the relevant group to the current ListViewItem Group
property
Select Case driveName.DriveType
Case IO.DriveType.Fixed
li.Group = hddGroup
Case IO.DriveType.Removable, IO.DriveType.CDRom
li.Group = removableGroup
End Select
lv.Items.Add(li)
Next
|