473,388 Members | 1,427 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

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

Hi,

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
  1. Public Class Form1
  2.  
  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
  19.  
  20.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  21.  
  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)
  27.  
  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")
  32.  
  33.         Dim item2 As New ListViewItem("Charlie")
  34.         item2.SubItems.Add("3.5")
  35.         item2.SubItems.Add("1\9\1920")
  36.  
  37.  
  38.         Dim item3 As New ListViewItem("Bravo")
  39.         item3.SubItems.Add("2.4")
  40.         item3.SubItems.Add("12/8/1930")
  41.  
  42.  
  43.         'Create Columns for the items and subitems
  44.         ListView1.Columns.Add("Item")
  45.         ListView1.Columns.Add("SubItem1")
  46.         ListView1.Columns.Add("SubItem2")
  47.  
  48.         'Add Items to List View
  49.         ListView1.Items.AddRange(New ListViewItem() {item1, item2, item3})
  50.  
  51.         'Add the ListView to the control Collection
  52.         Me.Controls.Add(ListView1)
  53.  
  54.  
  55.     End Sub
  56.  
  57. End Class
Dec 19 '12 #1
1 4006
PsychoCoder
465 Expert Mod 256MB
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

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

Similar topics

0
by: Sandy | last post by:
Within my tag: <asp:DataGrid I'm using: AllowSorting="true" and OnSortCommand="Sort_Grid" which referrs to a method in my Codebehind file. (see Sort_Grid below) Since I have...
0
by: Amalorpavanathan Yagulasamy(AMAL) | last post by:
Hi All How can change color of the Sort icon in Data Grid column Header Regards AMAL
1
by: VM | last post by:
How can I replicate the process of clicking on a column header in order to sort by that column? I tried getting the grid datasource, converting it to a view, sorting, and then attaching it to the...
2
by: Dan | last post by:
Hi, I would like my list view controls column headers to display a "down arrow / triangle" to indicate by which column they are sorted. I don't want the ListView to actually sort any data as...
2
by: Gas | last post by:
Hi, I am just wondering is there a way to add an up or down arrow in the column header of a list view control to indicate that the row are sort by that column? Gas
3
by: Steve | last post by:
I have windows form with ListView control. The ListView control has few columns which user can sort by clicking the column header. I want the column header have the small triangle indicator of...
0
by: Brian Henry | last post by:
Here is another virtual mode example for the .NET 2.0 framework while working with the list view. Since you can not access the items collection of the list view you need to do sorting another...
3
by: ±è¿ë°Ç | last post by:
i used a list view component to a form and i would like to do some thing when i click right mouse on the list view's column header but the mouse down event was not occured while i click list...
0
by: Gary | last post by:
Hi how do i instruct the listview to autoresize columns according to the width of the data they contain? (similar to what happens when you double click the column line when the form is displayed)....
3
by: > Adrian | last post by:
In VS 2003 I could sort a column in a list view using the code below, hover, in V2005 I get this error: Error 1 Using the generic type 'System.Collections.Generic.IComparer<T>' requires '1' type...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.