i have a listview and a combobox. i need to display all my column headers from the listview as items on my combobox. also i need to "sort" the data of my listview according to the selected item on my combobox.

ex: my listview contains the column headers, "name", "address", "age", "date"
i need name, address and age as my options on my drop down menu which is my combobox
next, i need to sort it. if i select "name" on the combobox, the listview should show the name columnheader sorted either ascending or descending

how do i do it? need serious help. i cant find answers anywhere. :(

you can add your listview columns into your combobox in this way :

   For i = 0 To ListView1.Columns.Count - 1
            ComboBox1.Items.Add(ListView1.Columns(i).Text)
        Next

In order to sort on a column when you click on it you have to create a sort class for that column. The sort class must be defined as implementing the IComparer interface. This is required because the actual sorting code (which you do not have to write) does not know how to sort for all given types of data. The sort class that you will write will contain only one method. That method must be named "Compare". This method will take two parameters of type Object. The Compare method will cast the parameters to ListViewItem, then return a value of -1, 0 or 1 depending on a comparison which you will code. A couple of things to note:

x and y (the generated parameters in the Compare method) are the listview rows. Your custom class must do the comparison on specific SubItems.

return -1 if item1 is < item2
return 0 if item1 is = item2
return +1 if item1 is > item2

A particular sort need not be restricted to one column. For example, you could code the custom sort for the first column to sort, first on column one, then on column two.

The following code requires one listview with three columns in details view mode. When run, it populates the listview with some random numbers. Click on the first column header to sort the rows based on the numeric values in the first column. Click on the second column header to sort the rows based on the string values in the second column. Click on the third column header to sort the rows based on the sum of the digits in the third column.

Public Class Form1

    Dim rnd As New System.Random

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        'populate a three column listview with some random numbers

        For i As Integer = 1 To 5

            Dim item As New ListViewItem
            item.Text = rnd.Next(1000)

            For j As Integer = 1 To 2
                item.SubItems.Add(rnd.Next(1000))
            Next

            ListView1.Items.Add(item)

        Next


    End Sub

    Private Sub ListView1_ColumnClick(sender As System.Object, e As System.Windows.Forms.ColumnClickEventArgs) Handles ListView1.ColumnClick

        'sort ascending on the clicked column

        Select Case e.Column
            Case 0
                ListView1.ListViewItemSorter = New FirstSorter
            Case 1
                ListView1.ListViewItemSorter = New SecondSorter
            Case 2
                ListView1.ListViewItemSorter = New ThirdSorter
        End Select

        ListView1.Sorting = SortOrder.Ascending

    End Sub

    Class FirstSorter

        'sorts on the first column based on the numeric value

        Implements IComparer

        Public Function Compare(x As Object, y As Object) As Integer Implements System.Collections.IComparer.Compare

            Dim item1 As ListViewItem = x
            Dim item2 As ListViewItem = y

            Return Math.Sign(CInt(item1.SubItems(0).Text) - CInt(item2.SubItems(0).Text))

        End Function

    End Class

    Class SecondSorter

        'sorts on the second column based on the string value

        Implements IComparer

        Public Function Compare(x As Object, y As Object) As Integer Implements System.Collections.IComparer.Compare

            Dim item1 As ListViewItem = x
            Dim item2 As ListViewItem = y

            Return StrComp(item1.SubItems(1).Text, item2.SubItems(1).Text)

        End Function

    End Class

    Class ThirdSorter

        'sorts on the third column based on the sum of all digits

        Implements IComparer

        Public Function Compare(x As Object, y As Object) As Integer Implements System.Collections.IComparer.Compare

            Dim item1 As ListViewItem = x
            Dim item2 As ListViewItem = y

            Dim sum1 As Integer = 0
            Dim sum2 As Integer = 0

            For Each ch As Char In item1.SubItems(2).Text.ToCharArray
                sum1 += Val(ch)
            Next

            For Each ch As Char In item2.SubItems(2).Text.ToCharArray
                sum2 += Val(ch)
            Next

            Return Math.Sign(sum1 - sum2)

        End Function

    End Class

End Class

To copy the column headers to the combobox you can do

For Each hdr As ColumnHeader In ListView1.Columns
    ComboBox1.Items.Add(hdr.Text)
Next    
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.