Hello Group!

I've looked around to see if there is specific code in VB.net to do this, but I haven't seen any.

I'm using ListView/Detail and want to ensure that it is sorted ascending by one specific column. That column is called "hdrLineNo". I know that I can choose "Sorting - Ascending" in the properties box. However this field IS NOT the first column of the ListView. So my question is:

Can I dynamically program the ListView to sort by the column called "hdrLineNo"? If so, how is this done?

As always, thanks for your help.

Don

Thanks, Jim. I'll head there now and read through it.

Don

Here's a simple way to sort by any column. Just set the global variable SortIndex to the column index to sort by:

Public Class Form2
    Dim SortIndex As Integer = 0
    Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim Rows As New List(Of ListViewItem)
        SortIndex = 2
        For Each item As ListViewItem In ListView1.Items
            Rows.Add(item)
        Next
        Rows.Sort(AddressOf SortByColumn)
        ListView1.Items.Clear()
        ListView1.Items.AddRange(Rows.ToArray)

    End Sub
    Private Function SortByColumn(x As ListViewItem, y As ListViewItem) As Integer
        Return x.SubItems(SortIndex).Text.CompareTo(y.SubItems(SortIndex).Text)
    End Function
End Class

Or you can do this way :

Add Class to your project, named ListViewColumnSorter (You can modify it).
Replace with this following code :

Imports System.Collections
Imports System.Windows.Forms

Public Class ListViewColumnSorter
    Implements System.Collections.IComparer

    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
End Class

In your form :

Public Class 
    ' Create an instance of class 
    Private lvwColumnSorter As New ListViewColumnSorter()

    Private Sub ListView1_ColumnClick(ByVal sender As Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles ListView1.ColumnClick
        If (e.Column = lvwColumnSorter.SortColumn) Then
            If (lvwColumnSorter.Order = SortOrder.Ascending) Then
                lvwColumnSorter.Order = SortOrder.Descending
            Else
                lvwColumnSorter.Order = SortOrder.Ascending
            End If
        Else
            lvwColumnSorter.SortColumn = e.Column
            lvwColumnSorter.Order = SortOrder.Ascending
        End If

        Me.ListView1.Sort()
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ListView1.ListViewItemSorter = lvwColumnSorter
        ListView1.View = View.Details

        With ListView1
            ' Add collumn header
            With .Columns
                .Add("No", 30)
                .Add("First Name", 150)
                .Add("Last Name", 150)
                .Add("Email", 100)
            End With

            ' Add a few data in listview
            With .Items
                Dim lv As New ListViewItem

                lv = .Add("1")
                lv.SubItems.Add("Andre")
                lv.SubItems.Add("White")
                lv.SubItems.Add("Andre@White.com")

                lv = .Add("2")
                lv.SubItems.Add("Danny")
                lv.SubItems.Add("Burnett")
                lv.SubItems.Add("Danny@Burnett.com")

                lv = .Add("3")
                lv.SubItems.Add("Edward")
                lv.SubItems.Add("Carter")
                lv.SubItems.Add("Edward@Carter.com")

                lv = .Add("4")
                lv.SubItems.Add("Anne")
                lv.SubItems.Add("Witter")
                lv.SubItems.Add("Anne@Witter.com")

                lv = .Add("5")
                lv.SubItems.Add("Vicky")
                lv.SubItems.Add("Myron")
                lv.SubItems.Add("Vicky@Myron.com")
            End With
        End With
    End Sub
End Class
commented: Great example +4
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.