473,558 Members | 2,949 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What entity get ListViewItems sorted by?

1 New Member
Hello :-)

I have built a ListView and filled it like this (simplified pseudo code):

Expand|Select|Wrap|Line Numbers
  1. Dim clm(3) As String
  3. For i = 0 To rows
  5.                 clm(0) = something(i)
  6.                 clm(1) = anything(i)
  7.                 clm(2) = stuff(i)
  9.                 Dim itm As ListViewItem = New ListViewItem(clm)
  11.                 myListView.Items.Add(itm)
  13. Next

Now, when the myListView.Sort ing property is set to anything else but "None" the listview will be sorted, ascending or descending. But what determines by which entity it will be sorted?

The MS docs only say:

The Sorting property allows you to specify whether or not items are sorted in the ListView control. By default, no sorting is performed. When the Sorting property is set to Ascending or Descending, the items in the ListView are sorted automatically in ascending alphabetical order (when the property is set to Ascending) or descending alphabetical order (when the property is set to Descending).
  1. The "items" are an array each. So by which member of the array will the ListView be sorted?
  2. Can I set a key, and would it make sorting quicker (currently taking a painful 5+ minutes for a 23000 items ListView)?
  3. The ListViewItem.Na me property is often referred to as a 'key', but with the above fill method this property will remain an emtpy string. Does it make any sense to set it to some value?

Grateful for any insight and help
- Ralph
Apr 6 '21 #1
2 5553
272 Contributor
To have ListView items sorted automatically, set the ListView.Sortin g property to SortOrder.Ascen ding or SortOrder.Desce nding.
Expand|Select|Wrap|Line Numbers
  1. myListView.Sorting = System.Windows.Forms.SortOrder.Ascending
However, with this method, sub-items cannot be compared and sorted, and numbers and times cannot be sorted correctly.
(Only the first item is sorted.)
To solve these problems, set the ListView.ListVi ewItemSorter property to an instance of a class that implements the IComparer interface that defines the arrangement.

Expand|Select|Wrap|Line Numbers
  1. Public Class ListViewItemComparer
  2.     Implements IComparer
  3.     Private _column As Integer
  4.     Private _Ascend As Boolean
  5.     Public Sub New(ByVal col As Integer, ByVal Ascend As Boolean)
  6.         _column = col
  7.         _Ascend = Ascend
  8.     End Sub
  10.     Public Function CompareTo(ByVal x As Object, ByVal y As Object) _
  11.         As Integer Implements System.Collections.IComparer.Compare
  12.         'Get the ListViewItem
  13.         Dim itemx As ListViewItem = CType(x, ListViewItem)
  14.         Dim itemy As ListViewItem = CType(y, ListViewItem)
  16.         If IsNumeric(itemx.SubItems(_column).Text) = True Then
  17.             Dim xx As Double
  18.             Dim yy As Double
  19.             Dim resulta As Boolean = Double.TryParse(itemx.SubItems(_column).Text, xx)
  20.             Dim resultb As Boolean = Double.TryParse(itemy.SubItems(_column).Text, yy)
  21.             If _Ascend = True then
  22.                 Return xx.CompareTo(yy)
  23.             Else
  24.                 Return yy.CompareTo(xx)
  25.             End If
  26.         Else
  27.             If _Ascend = True then
  28.                 Return itemx.SubItems(_column).Text.CompareTo(itemy.SubItems(_column).Text)
  29.             Else
  30.                 Return itemy.SubItems(_column).Text.CompareTo(itemx.SubItems(_column).Text)
  31.             End If
  32.         End If
  33.     End Function
  34. End Class
  36. Public Partial Class MainForm
  37.     Dim AD As Boolean = True
  38.     Public Sub New()
  39.         Me.InitializeComponent()
  40.         myListView.View = View.Details
  41.         'Set ColumnClick event handler
  42.         AddHandler myListView.ColumnClick, _
  43.             AddressOf ListView1_ColumnClick
  45.         Dim i As Long
  46.         Dim clm(3) As String
  47.         Dim rn As New System.Random()
  48.         Dim rc As New System.Random()
  49.         For i = 0 To 22999
  50.             Clm(0) = ChrW(rc.Next(AscW("A"), AscW("Z") + 1)) & ChrW(rc.Next(AscW("A"), AscW("Z") + 1)) & ChrW(rc.Next(AscW("A"), AscW("Z") + 1))
  51.             clm(1) = rn.Next().ToString
  52.             clm(2) = i.ToString
  53.             Dim itm As ListViewItem = New ListViewItem(clm)
  54.             myListView.Items.Add(itm)
  55.         Next i
  57.         'Add columns
  58.         myListView.Columns.Add("String", 100, HorizontalAlignment.Left)
  59.         myListView.Columns.Add("Numeric1", 100, HorizontalAlignment.Right)
  60.         myListView.Columns.Add("Numeric2", 100, HorizontalAlignment.Right)
  61.         'Sort Ascending
  62.         myListView.Sorting = System.Windows.Forms.SortOrder.Ascending
  63.     End Sub
  65.     Private Sub ListView1_ColumnClick(ByVal sender As Object, ByVal e As ColumnClickEventArgs)
  66.         myListView.ListViewItemSorter = New ListViewItemComparer(e.Column, AD)
  67.         If AD = True Then
  68.             AD = False
  69.         Else
  70.             AD = True
  71.         End If
  72.     End Sub
  73. End Class
The code above sorts the items in ascending / descending order by clicking on the column header.
Apr 8 '21 #2
308 Contributor
Please restrict the sort order to some value.It also constrained to datatype.

Let me know the datatype please.
Apr 10 '21 #3

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

Similar topics

by: Douglas Reith | last post by:
Hi There, Can someone please tell me why the XML spec states that an attribute value with an external entity is forbidden? Or point me to the appropriate document? Or better still, perhaps you know of a work around? It is a little frustrating that the normally powerful external entities are limited in this fashion. Example (myextent.txt...
by: Razvan | last post by:
Hi What is the difference between an internal and an external entity ? The first one is defined in the internal subset (not in a separate DTD file, but in the XML file itself - in DOCTYPE) while the second one is defined in the external subset (in a separate DTD file) ? Or an
by: lkrubner | last post by:
Can't get this RSS feed clean: http://www.whatisliberalism.com/pdsFiles/page2533.xml Why is it dying? Some users write posts in Microsoft Word, then copy and paste their post to the web browser and paste it in and hit submit and create a weblog entry. This is what I just did myself.
by: Arsen Vladimirskiy | last post by:
Hello, If I have a few simple classes to represent Entities such as Customers and Orders. What is the proper way to pass information to the Data Access Layer? 1) Pass the actual ENTITY to the Data Access Layer method -or- 2) Pass some kind of a unique id to the Data Access Layer method
by: Bob Rock | last post by:
Hello, this is something I've been asking myself for sometime ... and now I'd like to clarify this point. When should the try block start in a method??? What I mean is, does having all the code instead of a smaller set of it inside a try clause add any overhead??? 1What I'd like to understand is if, to be completely sure that no unhandles...
by: peorro77 | last post by:
How can i use IComparer to sort a grid of rows and lines? This is what i need: Shuffle Arraylist Field ROW Field LINE 5 2 5 3 4 2 6 2
by: Chad Miller | last post by:
lvProjects.Clear() lvProjects.View = View.Details lvProjects.FullRowSelect = True lvProjects.Columns.Add("Id", 50, HorizontalAlignment.Left) lvProjects.Columns.Add("Name", -1, HorizontalAlignment.Left) lvProjects.Columns.Add("Last Used", 100, HorizontalAlignment.Center) lvProjects.Columns.Add("Status", 100, HorizontalAlignment.Center) Dim...
by: polocar | last post by:
Hello, I'm developing a C# program using Visual Studio 2005 Professional Edition. In the main form I have inserted a ListView control that is populated in a different way depending on the item selected in a ComboBox. I have written a procedure that reads the ComboBox selected item and populates the ListView according to that. The procedure...
by: =?Utf-8?B?VmljdG9y?= | last post by:
Hi, How to get a list of ListViewItems that are currently shown in a Windows Form ListView control when that that control is in a virtual mode?
by: markla | last post by:
Hi, I have an Entity data model built in Entity Framework, which sources data primarily from an MS SQL 2008 database, and sources some static (data dictionary) values from code-based objects. I know I *could* store the data dict values in SQL: for various reasons that's not the path I want to take. I have some lookups, which are based on...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.