Does anybody know how to sort a Listview by checkbox state, so all checked items are at the top of the list.
I'm targeting Framework 2.0.
Printable View
Does anybody know how to sort a Listview by checkbox state, so all checked items are at the top of the list.
I'm targeting Framework 2.0.
Sorting a ListView is the same regardless. In all the examples you will have already found by searching the web, you will have seen that you need to create an object that implements IComparer to compare two ListViewItems. You can compare them any way you want, including by their Checked properties.
Thanks. I didn't know I could use it to compare the Checked property as well.
vb.net Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click ListView.ListViewItemSorter = New ListViewCompareChecked(SortOrder.Descending) ListView.Sort() End Sub Public Class ListViewCompareChecked Implements IComparer Private m_SortOrder As SortOrder Public Sub New(ByVal sort_order As SortOrder) m_SortOrder = sort_order End Sub Public Function Compare(ByVal x As Object, ByVal y As Object) _ As Integer Implements System.Collections.IComparer.Compare Dim item_x As ListViewItem = DirectCast(x, ListViewItem) Dim item_y As ListViewItem = DirectCast(y, ListViewItem) If m_SortOrder = SortOrder.Ascending Then Return item_x.Checked.CompareTo(item_y.Checked) Else Return -item_x.Checked.CompareTo(item_y.Checked) End If End Function End Class