ListView.ListViewItemSorter Property

 

Gets or sets the sorting comparer for the control.

Namespace:   System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

[<BrowsableAttribute(false)>]
member ListViewItemSorter : IComparer with get, set

Property Value

Type: System.Collections.IComparer

An IComparer that represents the sorting comparer for the control.

The ListViewItemSorter property allows you to specify the object that performs the sorting of items in a ListView control when the Sort method is called or when items are added to the list. Note that items are not sorted automatically when their label text changes.

The specified object must be an instance of a class that implements the IComparer interface, which has a single method called Comparer.Compare.

You can use this property to provide custom sorting, for example, when a column header is clicked in the details view. To do this, create a class that implements the IComparer interface and provides a constructor that accepts the index of a column to sort by. You can then implement a handler for the ColumnClick event that creates an instance of this class using the index of the clicked column. When you set the ListViewItemSorter property to the new instance, the ListView control is automatically sorted using the specified object. Subsequent calls to the Sort method use the same object.

System_CAPS_noteNote

When you set the value of the ListViewItemSorter property, the Sort method is called automatically.

The following code example creates a form that contains a ListView control that manually sorts items when a column in the ListView control is clicked. The example defines a class called ListViewItemComparer that implements the System.Collections.IComparer interface that performs the ListViewItem comparison. The example creates an instance of ListViewItemComparer and uses it to set the ListViewItemSorter property of the ListView control. The Sort method call in the ColumnClick event handler uses the methods defined in ListViewItemComparer to perform the sort of items, based on the column that is clicked.

No code example is currently available or this language may not be supported.

.NET Framework
Available since 1.1
Return to top
Show: