By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,552 Members | 905 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,552 IT Pros & Developers. It's quick & easy.

How can I manually sort columns in List View by clicking on the Column Header?

P: 6

I am trying to write the necessary code so that I can sort a ListView List by clicking on the column header, including header for sub items. I have built a simple application, using the code from the following link:

Even the Column headers, items and subitems are the same as the example. At one point the application did exactly what it was supposed to do but then I apparently, inadvertently made some change and it stopped working. I do not get any errors, either at design time nor at run time. I have gone through the code again and again and cannot find anything wrong but still it does not work. The only thing that is different in the code is that in some places Visual Studio automatically inserts "ByVal" into the code. If I take it out it is automatically added again. Below is the code exactly as I have it in my application. My form is called Form1 and My ListView control is called ListView1. Presently the code that creates the Listview loads automatically when Form1 is opened. Previously, that code was launched by a click event which I think was how I had it when it worked. I have been working on this for days. I would really appreciate if someone can point out where I am going wrong. Thank you

Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  3.     'Implements the manual sorting of items by column
  4.     Class ListViewItemComparer
  5.         Implements IComparer
  6.         Private col As Integer
  7.         Public Sub New()
  8.             col = 0
  9.         End Sub
  10.         Public Sub New(ByVal Column As Integer)
  11.             col = Column
  12.         End Sub
  13.         Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
  14.             Dim returnVal As Integer = -1
  15.             returnVal = [String].Compare(CType(x, ListViewItem).SubItems(col).Text, CType(y, ListViewItem).SubItems(col).Text)
  16.             Return returnVal
  17.         End Function
  18.     End Class
  20.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  22.         ' Dim ListView1 As New ListView()
  23.         ListView1.View = View.Details
  24.         ListView1.Bounds = New Rectangle(New Point(10, 10), New Size(300, 200))
  25.         ListView1.GridLines = True
  26.         ListView1.Font = New Font("Times New Roman", 12)
  28.         'Create Three New Items and Three Sub items
  29.         Dim item1 As New ListViewItem("Alpha")
  30.         item1.SubItems.Add("1.0")
  31.         item1.SubItems.Add("4\5\1945")
  33.         Dim item2 As New ListViewItem("Charlie")
  34.         item2.SubItems.Add("3.5")
  35.         item2.SubItems.Add("1\9\1920")
  38.         Dim item3 As New ListViewItem("Bravo")
  39.         item3.SubItems.Add("2.4")
  40.         item3.SubItems.Add("12/8/1930")
  43.         'Create Columns for the items and subitems
  44.         ListView1.Columns.Add("Item")
  45.         ListView1.Columns.Add("SubItem1")
  46.         ListView1.Columns.Add("SubItem2")
  48.         'Add Items to List View
  49.         ListView1.Items.AddRange(New ListViewItem() {item1, item2, item3})
  51.         'Add the ListView to the control Collection
  52.         Me.Controls.Add(ListView1)
  55.     End Sub
  57. End Class
Dec 19 '12 #1
Share this Question
Share on Google+
1 Reply

Expert Mod 100+
P: 465
Nowhere in your code are you telling your application you want to sort when a header is clicked. To do this you'll need to add it into the ColumnClicked Event of your ListView.

Check that link out, it'll show you how to set up a ListView for sorting the way you're looking for.
Dec 20 '12 #2

Post your reply

Sign in to post your reply or Sign up for a free account.