Note that there are some explanatory texts on larger screens.

plurals
  1. POSort the column by value if it is an integer/long and also by date if the column is a date column in ListView
    primarykey
    data
    text
    <p>I am using the following code to sort a listview when clicking on the column:</p> <pre><code>Imports System.Collections </code></pre> <p>Imports System.Windows.Forms</p> <p>Public Class ListViewColumnSorter Implements System.Collections.IComparer</p> <pre><code>Private ColumnToSort As Integer Private OrderOfSort As SortOrder Private ObjectCompare As CaseInsensitiveComparer Public Sub New() ' Initialize the column to '0'. ColumnToSort = 0 ' Initialize the sort order to 'none'. OrderOfSort = SortOrder.None ' Initialize the CaseInsensitiveComparer object. ObjectCompare = New CaseInsensitiveComparer() End Sub Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare Dim compareResult As Integer Dim listviewX As ListViewItem Dim listviewY As ListViewItem ' Cast the objects to be compared to ListViewItem objects. listviewX = CType(x, ListViewItem) listviewY = CType(y, ListViewItem) ' Compare the two items. compareResult = ObjectCompare.Compare(listviewX.SubItems(ColumnToSort).Text, listviewY.SubItems(ColumnToSort).Text) ' Calculate the correct return value based on the object ' comparison. If (OrderOfSort = SortOrder.Ascending) Then ' Ascending sort is selected, return typical result of ' compare operation. Return compareResult ElseIf (OrderOfSort = SortOrder.Descending) Then ' Descending sort is selected, return negative result of ' compare operation. Return (-compareResult) Else ' Return '0' to indicate that they are equal. Return 0 End If End Function Public Property SortColumn() As Integer Set(ByVal Value As Integer) ColumnToSort = Value End Set Get Return ColumnToSort End Get End Property Public Property Order() As SortOrder Set(ByVal Value As SortOrder) OrderOfSort = Value End Set Get Return OrderOfSort End Get End Property </code></pre> <p>End Class</p> <p>The msdn site for this is at: <a href="http://support.microsoft.com/kb/319399" rel="nofollow">http://support.microsoft.com/kb/319399</a></p> <p>This is only sorting the columns via the text. I am wanting some help to modify this to sort the column by value if it is an integer/long and also by date if the column is a date column.</p> <p>I can modify this line: compareResult = ObjectCompare.Compare(listviewX.SubItems(ColumnToSort).Text, listviewY.SubItems(ColumnToSort).Text) to sort via dates/integers etc, but I am having to manually code it. Is there a way to detect if the column I have clicked on is a date/long/integer column... maybe via tryparse?</p> <p>thanks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload