473,471 Members | 1,713 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

HOWTO: Sorting a list view in detail mode in .NET 2.0 while in virtual mode

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 way... here is my code on how I
did it to help anyone starting out get an idea of how to use virtual mode in
..NET 2.0

Imports CrystalDecisions.CrystalReports.Engine

Imports CrystalDecisions.Shared

Public Class Form1

''' <summary>

''' Retrieve thousands of rows of data from database server to fill dataset
to populate list view

''' </summary>

''' <remarks></remarks>

Dim aItems(0) As ListViewItem

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

' fill data then process it into an array

da.Fill(ds, "entries")

LoadData()

End Sub

''' <summary>

''' Moves data from data set into an array of list view items (speeds things
up when you preprocess the

''' data set into list view items so you dont have to make them on the fly
when retrieveing virtual items

''' </summary>

''' <remarks></remarks>

Private Sub LoadData()

Me.ListView1.VirtualListSize = 0

If Not aItems Is Nothing Then

Array.Clear(aItems, 0, aItems.Length)

End If

Array.Resize(aItems, ds.Tables("entries").Rows.Count)

Dim i As Integer = 0

for each dr as datarow in ds.tables("entries").rows

dim lvitem as listviewitem

' pre process list view items now the add them to the array here

aItems(i) = lvItem

i += 1

Application.DoEvents()

Next

Me.ListView1.VirtualMode = True

Me.ListView1.VirtualListSize = aItems.Length

End Sub

''' <summary>

''' This is the column clicked event of the list view

''' you need to sort the array that contains the list view items

''' when ever this happens based on the column that was clicked on

''' after this, the array will be sorted, but the list view will not

''' reflect this change. Because of this, you must invalidate the

''' list view to force it to reflect the change of the array that was

''' sorted on screen

''' </summary>

''' <param name="sender"></param>

''' <param name="e"></param>

''' <remarks>Must invalidate list view after sorting to force on screen
changes</remarks>

Private Sub ListView1_ColumnClick(ByVal sender As Object, ByVal e As
System.Windows.Forms.ColumnClickEventArgs) Handles ListView1.ColumnClick

Debug.WriteLine("Column header clicked")

If Me.ListView1.Sorting = SortOrder.Ascending Then

Me.ListView1.Sorting = SortOrder.Descending

Else

Me.ListView1.Sorting = SortOrder.Ascending

End If

Dim sorter As New ListViewSortOrder(e.Column, Me.ListView1.Sorting)

Array.Sort(aItems, sorter)

Me.ListView1.Invalidate()

End Sub

Private Sub ListView1_DrawColumnHeader(ByVal sender As Object, ByVal e As
System.Windows.Forms.DrawListViewColumnHeaderEvent Args) Handles
ListView1.DrawColumnHeader

e.DrawDefault = True

End Sub

''' <summary>

''' Sort comparision class which implements IComparable

''' This will be used to sort the array of list view items

''' that the virtual mode list view uses to display

''' </summary>

''' <remarks></remarks>

Private Class ListViewSortOrder

Implements IComparer

Dim column As Integer

Dim order As SortOrder = SortOrder.Ascending

Public Sub New(ByVal aColumn As Integer, ByVal aorder As SortOrder)

column = aColumn

order = aorder

End Sub

Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer
Implements System.Collections.IComparer.Compare

Dim xItem, yItem As System.Windows.Forms.ListViewItem

xItem = CType(x, ListViewItem)

yItem = CType(y, ListViewItem)

If order = SortOrder.Ascending Then

Return (xItem.SubItems(column).Text).CompareTo(yItem.SubI tems(column).Text)

Else

Return (yItem.SubItems(column).Text).CompareTo(xItem.SubI tems(column).Text)

End If

End Function

End Class

''' <summary>

''' Pulls the virtual item out of the list as it is needed to display on the
screen from the

''' array of preprocessed list view items from the initially loaded data set

''' </summary>

''' <param name="sender"></param>

''' <param name="e"></param>

''' <remarks></remarks>

Private Sub ListView1_RetrieveVirtualItem(ByVal sender As Object, ByVal e As
System.Windows.Forms.RetrieveVirtualItemEventArgs) Handles
ListView1.RetrieveVirtualItem

e.Item = aItems(e.ItemIndex)

End Sub

''' <summary>

''' This happens when a selection is reset to no items selected, like when a
user clicks off a selection

''' </summary>

''' <param name="sender"></param>

''' <param name="e"></param>

''' <remarks></remarks>

Private Sub ListView1_VirtualItemsSelectionRangeChanged(ByVal sender As
Object, ByVal e As
System.Windows.Forms.ListViewVirtualItemsSelection RangeChangedEventArgs)
Handles ListView1.VirtualItemsSelectionRangeChanged

Me.ListBox1.BeginUpdate()

Debug.WriteLine(String.Format("{3}: Start {0}, End {1}, IsSelected {2}",
e.StartIndex, e.EndIndex, e.IsSelected, Now.ToLongTimeString))

Me.ListBox1.Items.Clear()

Me.ListBox1.EndUpdate()

End Sub

''' <summary>

''' This happens when a user selects items or multiple selects items or
deselects one of the multi selected items

''' This does not fire though if you have an item selected then click off it
or have multiple items selected then

''' click off them... the virutal selection range change happens at that
point and need to reset your selected

''' items list at that point

''' </summary>

''' <param name="sender"></param>

''' <param name="e"></param>

''' <remarks></remarks>

Private Sub ListView1_ItemSelectionChanged(ByVal sender As Object, ByVal e
As System.Windows.Forms.ListViewItemSelectionChangedE ventArgs) Handles
ListView1.ItemSelectionChanged

Me.ListBox1.BeginUpdate()

Debug.WriteLine(String.Format("Item Changed: {0}, Is selected: {1}",
e.ItemIndex, e.IsSelected))

If e.IsSelected = True Then

Me.ListBox1.Items.Add(CStr(e.ItemIndex))

Else

Me.ListBox1.Items.Remove(CStr(e.ItemIndex))

End If

Me.ListBox1.EndUpdate()

End Sub

End Class

'======================================

Form designer partial class for control setup on form

' =========================================

<Global.Microsoft.VisualBasic.CompilerServices.Des ignerGenerated()> _

Partial Class Form1

Inherits System.Windows.Forms.Form

'Form overrides dispose to clean up the component list.

<System.Diagnostics.DebuggerNonUserCode()> _

Protected Overrides Sub Dispose(ByVal disposing As Boolean)

If disposing AndAlso components IsNot Nothing Then

components.Dispose()

End If

MyBase.Dispose(disposing)

End Sub

'Required by the Windows Form Designer

Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer

'It can be modified using the Windows Form Designer.

'Do not modify it using the code editor.

<System.Diagnostics.DebuggerStepThrough()> _

Private Sub InitializeComponent()

Me.components = New System.ComponentModel.Container

Dim resources As System.ComponentModel.ComponentResourceManager = New
System.ComponentModel.ComponentResourceManager(Get Type(Form1))

Me.ImageList1 = New System.Windows.Forms.ImageList(Me.components)

Me.ListBox1 = New System.Windows.Forms.ListBox

Me.ListView1 = New WindowsApplication9.DoubleBufferedListView

Me.clmTypeIcon = New System.Windows.Forms.ColumnHeader

Me.clmStatusIcon = New System.Windows.Forms.ColumnHeader

Me.clmAttachmentIcon = New System.Windows.Forms.ColumnHeader

Me.clmJournalType = New System.Windows.Forms.ColumnHeader

Me.clmEntryType = New System.Windows.Forms.ColumnHeader

Me.clmCreatedBy = New System.Windows.Forms.ColumnHeader

Me.clmDateCreated = New System.Windows.Forms.ColumnHeader

Me.clmAccount = New System.Windows.Forms.ColumnHeader

Me.clmEntryMessage = New System.Windows.Forms.ColumnHeader

Me.clmStatus = New System.Windows.Forms.ColumnHeader

Me.clmDueDate = New System.Windows.Forms.ColumnHeader

Me.clmEnrollee = New System.Windows.Forms.ColumnHeader

Me.SuspendLayout()

'

'ImageList1

'

Me.ImageList1.ImageStream =
CType(resources.GetObject("ImageList1.ImageStream" ),
System.Windows.Forms.ImageListStreamer)

Me.ImageList1.TransparentColor = System.Drawing.Color.Transparent

Me.ImageList1.Images.SetKeyName(0, "EMAILhighimportance.png")

Me.ImageList1.Images.SetKeyName(1, "EMAILlowimportance.png")

Me.ImageList1.Images.SetKeyName(2, "bookclosedpurple.png")

Me.ImageList1.Images.SetKeyName(3, "bookopengreen.png")

Me.ImageList1.Images.SetKeyName(4, "bookopenred.png")

Me.ImageList1.Images.SetKeyName(5, "bookopenyellow.png")

'

'ListBox1

'

Me.ListBox1.FormattingEnabled = True

Me.ListBox1.Location = New System.Drawing.Point(0, 307)

Me.ListBox1.Name = "ListBox1"

Me.ListBox1.Size = New System.Drawing.Size(265, 225)

Me.ListBox1.TabIndex = 1

'

'ListView1

'

Me.ListView1.Columns.AddRange(New System.Windows.Forms.ColumnHeader()
{Me.clmTypeIcon, Me.clmStatusIcon, Me.clmAttachmentIcon, Me.clmJournalType,
Me.clmEntryType, Me.clmCreatedBy, Me.clmDateCreated, Me.clmAccount,
Me.clmEntryMessage, Me.clmStatus, Me.clmDueDate, Me.clmEnrollee})

Me.ListView1.Dock = System.Windows.Forms.DockStyle.Top

Me.ListView1.FullRowSelect = True

Me.ListView1.HideSelection = False

Me.ListView1.Location = New System.Drawing.Point(0, 0)

Me.ListView1.Name = "ListView1"

Me.ListView1.Size = New System.Drawing.Size(929, 301)

Me.ListView1.SmallImageList = Me.ImageList1

Me.ListView1.Sorting = System.Windows.Forms.SortOrder.Ascending

Me.ListView1.TabIndex = 0

Me.ListView1.UseCompatibleStateImageBehavior = False

Me.ListView1.View = System.Windows.Forms.View.Details

'

'clmTypeIcon

'

Me.clmTypeIcon.Name = "clmTypeIcon"

Me.clmTypeIcon.Width = 21

'

'clmStatusIcon

'

Me.clmStatusIcon.Name = "clmStatusIcon"

Me.clmStatusIcon.Width = 21

'

'clmAttachmentIcon

'

Me.clmAttachmentIcon.Name = "clmAttachmentIcon"

Me.clmAttachmentIcon.Width = 21

'

'clmJournalType

'

Me.clmJournalType.Name = "clmJournalType"

'

'clmEntryType

'

Me.clmEntryType.Name = "clmEntryType"

'

'clmCreatedBy

'

Me.clmCreatedBy.Name = "clmCreatedBy"

'

'clmDateCreated

'

Me.clmDateCreated.Name = "clmDateCreated"

'

'clmAccount

'

Me.clmAccount.Name = "clmAccount"

'

'clmEntryMessage

'

Me.clmEntryMessage.Name = "clmEntryMessage"

Me.clmEntryMessage.Width = 300

'

'clmStatus

'

Me.clmStatus.Name = "clmStatus"

'

'clmDueDate

'

Me.clmDueDate.Name = "clmDueDate"

'

'clmEnrollee

'

Me.clmEnrollee.Name = "clmEnrollee"

'

'Form1

'

Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)

Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font

Me.ClientSize = New System.Drawing.Size(929, 533)

Me.Controls.Add(Me.ListBox1)

Me.Controls.Add(Me.ListView1)

Me.Name = "Form1"

Me.Text = "Form1"

Me.ResumeLayout(False)

End Sub

Friend WithEvents ListView1 As DoubleBufferedListView

Friend WithEvents clmIconA As System.Windows.Forms.ColumnHeader

Friend WithEvents clmIconB As System.Windows.Forms.ColumnHeader

Friend WithEvents clmAttachments As System.Windows.Forms.ColumnHeader

Friend WithEvents clmJournalType As System.Windows.Forms.ColumnHeader

Friend WithEvents clmType As System.Windows.Forms.ColumnHeader

Friend WithEvents clmUser As System.Windows.Forms.ColumnHeader

Friend WithEvents clmDate As System.Windows.Forms.ColumnHeader

Friend WithEvents clmAccount As System.Windows.Forms.ColumnHeader

Friend WithEvents clmMessage As System.Windows.Forms.ColumnHeader

Friend WithEvents clmStatus As System.Windows.Forms.ColumnHeader

Friend WithEvents clmDueDate As System.Windows.Forms.ColumnHeader

Friend WithEvents clmEnrollee As System.Windows.Forms.ColumnHeader

Friend WithEvents clmTypeIcon As System.Windows.Forms.ColumnHeader

Friend WithEvents clmStatusIcon As System.Windows.Forms.ColumnHeader

Friend WithEvents clmAttachmentIcon As System.Windows.Forms.ColumnHeader

Friend WithEvents clmEntryType As System.Windows.Forms.ColumnHeader

Friend WithEvents clmCreatedBy As System.Windows.Forms.ColumnHeader

Friend WithEvents clmDateCreated As System.Windows.Forms.ColumnHeader

Friend WithEvents clmEntryMessage As System.Windows.Forms.ColumnHeader

Friend WithEvents ImageList1 As System.Windows.Forms.ImageList

Friend WithEvents ListBox1 As System.Windows.Forms.ListBox

End Class
Nov 21 '05 #1
0 3207

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Owen T. Soroke | last post by:
Using VB.NET I have a ListView with several columns. Two columns contain integer values, while the remaining contain string values. I am confused as to how I would provide functionality to...
0
by: Brian Henry | last post by:
Since no one else knew how to do this I sat here all morning experimenting with this and this is what I came up with... Its an example of how to get a list of items back from a virtual mode list...
0
by: Brian Henry | last post by:
I am trying to do a owner drawn list view in detail mode, when i inherited the list view into a new custom control then turned on double buffering all the sudden the selection rectangle is the...
3
by: Curtis | last post by:
I am trying to do a simple sort on a list view.With the listview control the first time I sort on the control it sorts the items but anytime after that it fails to sort. I'm sure this is a simple...
0
by: Smokey Grindle | last post by:
Say I do the following in a virtual list box ctrl click one row then ctrl click another row... ok the VirtualItemsSelectionRangeChanged event fires with start index of 2 and end index of 10... ...
6
by: Ian Boyd | last post by:
Every time during development we had to make table changes, we use Control Center. Most of the time, Control Center fails. If you try to "undo all", it doesn't, and you end up losing your identity...
20
by: martin-g | last post by:
Hi. Mostly I program in C++, and I'm not fluent in C# and .NET. In my last project I began to use LinkedList<and suddenly noticed that can't find a way to sort it. Does it mean I must implement...
1
by: Duracel | last post by:
Is there a maximum number for the VirtualListSize property when using a list view in Virtual Mode? When I set it to 1,000, 10,000 or 100,000 I seem to be able to scroll the correct number of...
1
by: ramaswamynanda | last post by:
Hello, I have a simple vb.net 2005 application using an SQLserver database There is a form , employees that has a grid view and a detail view control The grid displays data and has a "Select"...
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,...
1
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...
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,...
1
isladogs
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.