Hello :-)
I have built a ListView and filled it like this (simplified pseudo code): -
Dim clm(3) As String
-
-
For i = 0 To rows
-
-
clm(0) = something(i)
-
clm(1) = anything(i)
-
clm(2) = stuff(i)
-
-
Dim itm As ListViewItem = New ListViewItem(clm)
-
-
myListView.Items.Add(itm)
-
-
Next
-
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). - The "items" are an array each. So by which member of the array will the ListView be sorted?
- Can I set a key, and would it make sorting quicker (currently taking a painful 5+ minutes for a 23000 items ListView)?
- 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
2 5470
To have ListView items sorted automatically, set the ListView.Sorting property to SortOrder.Ascending or SortOrder.Descending. - 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. - Public Class ListViewItemComparer
-
Implements IComparer
-
Private _column As Integer
-
Private _Ascend As Boolean
-
Public Sub New(ByVal col As Integer, ByVal Ascend As Boolean)
-
_column = col
-
_Ascend = Ascend
-
End Sub
-
-
Public Function CompareTo(ByVal x As Object, ByVal y As Object) _
-
As Integer Implements System.Collections.IComparer.Compare
-
'Get the ListViewItem
-
Dim itemx As ListViewItem = CType(x, ListViewItem)
-
Dim itemy As ListViewItem = CType(y, ListViewItem)
-
-
If IsNumeric(itemx.SubItems(_column).Text) = True Then
-
Dim xx As Double
-
Dim yy As Double
-
Dim resulta As Boolean = Double.TryParse(itemx.SubItems(_column).Text, xx)
-
Dim resultb As Boolean = Double.TryParse(itemy.SubItems(_column).Text, yy)
-
If _Ascend = True then
-
Return xx.CompareTo(yy)
-
Else
-
Return yy.CompareTo(xx)
-
End If
-
Else
-
If _Ascend = True then
-
Return itemx.SubItems(_column).Text.CompareTo(itemy.SubItems(_column).Text)
-
Else
-
Return itemy.SubItems(_column).Text.CompareTo(itemx.SubItems(_column).Text)
-
End If
-
End If
-
End Function
-
End Class
-
-
Public Partial Class MainForm
-
Dim AD As Boolean = True
-
Public Sub New()
-
Me.InitializeComponent()
-
myListView.View = View.Details
-
'Set ColumnClick event handler
-
AddHandler myListView.ColumnClick, _
-
AddressOf ListView1_ColumnClick
-
-
Dim i As Long
-
Dim clm(3) As String
-
Dim rn As New System.Random()
-
Dim rc As New System.Random()
-
For i = 0 To 22999
-
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))
-
clm(1) = rn.Next().ToString
-
clm(2) = i.ToString
-
Dim itm As ListViewItem = New ListViewItem(clm)
-
myListView.Items.Add(itm)
-
Next i
-
-
'Add columns
-
myListView.Columns.Add("String", 100, HorizontalAlignment.Left)
-
myListView.Columns.Add("Numeric1", 100, HorizontalAlignment.Right)
-
myListView.Columns.Add("Numeric2", 100, HorizontalAlignment.Right)
-
'Sort Ascending
-
myListView.Sorting = System.Windows.Forms.SortOrder.Ascending
-
End Sub
-
-
Private Sub ListView1_ColumnClick(ByVal sender As Object, ByVal e As ColumnClickEventArgs)
-
myListView.ListViewItemSorter = New ListViewItemComparer(e.Column, AD)
-
If AD = True Then
-
AD = False
-
Else
-
AD = True
-
End If
-
End Sub
-
End Class
-
The code above sorts the items in ascending / descending order by clicking on the column header.
Please restrict the sort order to some value.It also constrained to datatype.
Let me know the datatype please.
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...
|
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...
|
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...
|
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...
|
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...
|
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 ...
|
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,...
|
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...
|
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...
|
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"....
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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...
|
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)...
| |