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