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:
http://msdn.microsoft.com/en/us/library/ms996467.aspx
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
- Public Class Form1
- 'Implements the manual sorting of items by column
- Class ListViewItemComparer
- Implements IComparer
- Private col As Integer
- Public Sub New()
- col = 0
- End Sub
- Public Sub New(ByVal Column As Integer)
- col = Column
- End Sub
- Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
- Dim returnVal As Integer = -1
- returnVal = [String].Compare(CType(x, ListViewItem).SubItems(col).Text, CType(y, ListViewItem).SubItems(col).Text)
- Return returnVal
- End Function
- End Class
- Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
- ' Dim ListView1 As New ListView()
- ListView1.View = View.Details
- ListView1.Bounds = New Rectangle(New Point(10, 10), New Size(300, 200))
- ListView1.GridLines = True
- ListView1.Font = New Font("Times New Roman", 12)
- 'Create Three New Items and Three Sub items
- Dim item1 As New ListViewItem("Alpha")
- item1.SubItems.Add("1.0")
- item1.SubItems.Add("4\5\1945")
- Dim item2 As New ListViewItem("Charlie")
- item2.SubItems.Add("3.5")
- item2.SubItems.Add("1\9\1920")
- Dim item3 As New ListViewItem("Bravo")
- item3.SubItems.Add("2.4")
- item3.SubItems.Add("12/8/1930")
- 'Create Columns for the items and subitems
- ListView1.Columns.Add("Item")
- ListView1.Columns.Add("SubItem1")
- ListView1.Columns.Add("SubItem2")
- 'Add Items to List View
- ListView1.Items.AddRange(New ListViewItem() {item1, item2, item3})
- 'Add the ListView to the control Collection
- Me.Controls.Add(ListView1)
- End Sub
- End Class