473,421 Members | 1,638 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,421 software developers and data experts.

What entity get ListViewItems sorted by?

1
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
  2.  
  3. For i = 0 To rows
  4.  
  5.                 clm(0) = something(i)
  6.                 clm(1) = anything(i)
  7.                 clm(2) = stuff(i)
  8.  
  9.                 Dim itm As ListViewItem = New ListViewItem(clm)
  10.  
  11.                 myListView.Items.Add(itm)
  12.  
  13. Next
  14.  

Now, when the myListView.Sorting 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.Name 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 5542
SioSio
272 256MB
To have ListView items sorted automatically, set the ListView.Sorting property to SortOrder.Ascending or SortOrder.Descending.
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.ListViewItemSorter 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
  9.  
  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)
  15.  
  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
  35.  
  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
  44.  
  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
  56.  
  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
  64.  
  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
  74.  
The code above sorts the items in ascending / descending order by clicking on the column header.
Apr 8 '21 #2
madankarmukta
308 256MB
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

11
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...
1
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...
9
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...
11
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...
3
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...
3
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 ...
8
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,...
2
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...
1
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?
1
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...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
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...
0
tracyyun
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.