473,289 Members | 2,108 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,289 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 5470
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...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.