How To Group ListView Items with ListViewGroup VB2005
This article with source code demonstrates how to group ListView Items with the ListViewGroup class and Visual Basic 2005. In the article example, customers are loaded from a database and three groups are created - customer name, region, and country.
Example Application Screen Shot
About the ListView Grouping
ListView grouping is used to create visual groups of logically related ListView items. Each group consists of a text-based header followed by a horizontal line and the items assigned to that group. See screen shot above for an example. You can align the header text to the left, right, or center of the control. Any groups assigned to a ListView control appear whenever the ListView.View property is set to a value other than View.List.
To use the grouping feature, add one or more ListViewGroup objects to the ListView.Groups collection of a ListView control.
Assign a ListView Item to a group by specifying the group in the ListViewItem constructor, by setting the ListViewItem.Group property, or by directly adding the item to the Items collection of a group. All items should be assigned to groups before they are displayed.
An item can only be in one group at a time. You can change the group to which an item belongs by setting the ListViewItem.Group property at run time or by adding it to the Items collection of another group, which automatically removes it from the previous group.
Source Code Extract
Imports System.Data
Public Class ExampleOneForm
' Declare variable named groupTables of type Hashtable.
Private groupTables() As Hashtable
' Declare variable named columnGroupIndex of type integer.
Private columnGroupIndex As Integer
Private Sub DemoOneForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Make this form a MDI child of MainForm.
Me.MdiParent = My.Forms.MainForm
Me.Text = "ListView Groups Example"
' Set CustomersListView Details and Sorting properties.
CustomersListView.View = View.Details
CustomersListView.Sorting = SortOrder.Ascending
' Create and initialize column headers for CustomersListView.
Dim columnHeader0 As New ColumnHeader()
columnHeader0.Text = "Company Name"
columnHeader0.Width = 150
Dim columnHeader1 As New ColumnHeader()
columnHeader1.Width = 150
columnHeader1.Text = "Region Name"
Dim columnHeader2 As New ColumnHeader()
columnHeader2.Text = "Country"
columnHeader2.Width = 150
' Add the column headers to CustomersListView.
CustomersListView.Columns.AddRange(New ColumnHeader() _
{columnHeader0, columnHeader1, columnHeader2})
' Add a handler for the ColumnClick event.
AddHandler CustomersListView.ColumnClick, AddressOf CustomersListView_ColumnClick
' Load customers.
Dim customerTableAdapter As New DbForTestingDataSetTableAdapters.CustomersTableAdapter
customerTableAdapter.Fill(Me.DbForTestingDataSet.Customers)
' Create CustomersListView Items from customer rows.
For Each customer As DbForTestingDataSet.CustomersRow In Me.DbForTestingDataSet.Customers.Rows
Dim newItem As New ListViewItem(New String() {customer.CompanyName.ToString, customer.City.ToString, customer.Country.ToString})
Me.CustomersListView.Items.Add(newItem)
Next
' Create the groupsTable HashTable array object.
groupTables = New Hashtable(CustomersListView.Columns.Count) {}
' Create a HashTable for each column in CustomersListView.
Dim column As Integer
For column = 0 To CustomersListView.Columns.Count - 1
groupTables(column) = CreateGroupsHashTable(column)
Next column
' Start with the CustomerName group.
SetGroups(0)
End Sub
' Group items according to the column clicked.
Private Sub CustomersListView_ColumnClick( _
ByVal sender As Object, ByVal e As ColumnClickEventArgs)
' If the column group is changing...
If e.Column <> columnGroupIndex And CustomersListView.Sorting = SortOrder.Descending Then
' Reverse the sort order.
CustomersListView.Sorting = SortOrder.Ascending
' Change the columnGroupIndex.
columnGroupIndex = e.Column
ElseIf CustomersListView.Sorting = SortOrder.Ascending Then
CustomersListView.Sorting = SortOrder.Descending
ElseIf CustomersListView.Sorting = SortOrder.Descending Then
CustomersListView.Sorting = SortOrder.Ascending
End If
' Set the grouping according to the column clicked.
SetGroups(e.Column)
End Sub
' Set the grouping according to the column clicked.
Private Sub SetGroups(ByVal column As Integer)
' Clear the current grouping.
CustomersListView.Groups.Clear()
' Retrieve the hash table corresponding to the column clicked.
Dim groupsHashTable As Hashtable = CType(groupTables(column), Hashtable)
' Copy the groups for the column from the groupsHastTable to an array.
Dim groupsArray(groupsHashTable.Count - 1) As ListViewGroup
groupsHashTable.Values.CopyTo(groupsArray, 0)
' Sort the groups and add them to CustomersListView.
Array.Sort(groupsArray, New ListViewGroupSorter(CustomersListView.Sorting))
CustomersListView.Groups.AddRange(groupsArray)
' Iterate through the items in CustomersListView, assigning each
' one to the appropriate group.
Dim item As ListViewItem
For Each item In CustomersListView.Items
' Retrieve the subitem text corresponding to the column.
Dim subItemText As String = item.SubItems(column).Text
' For the Title column, use only the first letter.
If column = 0 Then
subItemText = subItemText.Substring(0, 1)
End If
' Assign the item to the matching group.
item.Group = CType(groupsHashTable(subItemText), ListViewGroup)
Next item
End Sub
Private Function CreateGroupsHashTable(ByVal column As Integer) As Hashtable
' Create a Hashtable object.
Dim groupsHashTable As New Hashtable()
' Iterate through the items in CustomersListView.
Dim item As ListViewItem
For Each item In CustomersListView.Items
' Retrieve the text value for the column.
Dim subItemText As String = item.SubItems(column).Text
' Use the initial letter instead if it is the first column.
If column = 0 Then
subItemText = subItemText.Substring(0, 1)
End If
If Not groupsHashTable.Contains(subItemText) Then
groupsHashTable.Add(subItemText, New ListViewGroup(subItemText, _
HorizontalAlignment.Left))
End If
Next item
Return groupsHashTable
End Function
End Class
For more information:
评论