473,699 Members | 2,333 Online
Bytes | Software Development & Data Engineering Community
+ 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 CrystalDecision s.CrystalReport s.Engine

Imports CrystalDecision s.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(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load

' fill data then process it into an array

da.Fill(ds, "entries")


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.Vi rtualListSize = 0

If Not aItems Is Nothing Then

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

End If

Array.Resize(aI tems, ds.Tables("entr ies").Rows.Coun t)

Dim i As Integer = 0

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

dim lvitem as listviewitem

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

aItems(i) = lvItem

i += 1

Application.DoE vents()


Me.ListView1.Vi rtualMode = True

Me.ListView1.Vi rtualListSize = 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

Private Sub ListView1_Colum nClick(ByVal sender As Object, ByVal e As
System.Windows. Forms.ColumnCli ckEventArgs) Handles ListView1.Colum nClick

Debug.WriteLine ("Column header clicked")

If Me.ListView1.So rting = SortOrder.Ascen ding Then

Me.ListView1.So rting = SortOrder.Desce nding


Me.ListView1.So rting = SortOrder.Ascen ding

End If

Dim sorter As New ListViewSortOrd er(e.Column, Me.ListView1.So rting)

Array.Sort(aIte ms, sorter)

Me.ListView1.In validate()

End Sub

Private Sub ListView1_DrawC olumnHeader(ByV al sender As Object, ByVal e As
System.Windows. Forms.DrawListV iewColumnHeader EventArgs) Handles
ListView1.DrawC olumnHeader

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 ListViewSortOrd er

Implements IComparer

Dim column As Integer

Dim order As SortOrder = SortOrder.Ascen ding

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.Collecti ons.IComparer.C ompare

Dim xItem, yItem As System.Windows. Forms.ListViewI tem

xItem = CType(x, ListViewItem)

yItem = CType(y, ListViewItem)

If order = SortOrder.Ascen ding Then

Return (xItem.SubItems (column).Text). CompareTo(yItem .SubItems(colum n).Text)


Return (yItem.SubItems (column).Text). CompareTo(xItem .SubItems(colum n).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_Retri eveVirtualItem( ByVal sender As Object, ByVal e As
System.Windows. Forms.RetrieveV irtualItemEvent Args) Handles
ListView1.Retri eveVirtualItem

e.Item = aItems(e.ItemIn dex)

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_Virtu alItemsSelectio nRangeChanged(B yVal sender As
Object, ByVal e As
System.Windows. Forms.ListViewV irtualItemsSele ctionRangeChang edEventArgs)
Handles ListView1.Virtu alItemsSelectio nRangeChanged

Me.ListBox1.Beg inUpdate()

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

Me.ListBox1.Ite ms.Clear()

Me.ListBox1.End Update()

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_ItemS electionChanged (ByVal sender As Object, ByVal e
As System.Windows. Forms.ListViewI temSelectionCha ngedEventArgs) Handles
ListView1.ItemS electionChanged

Me.ListBox1.Beg inUpdate()

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

If e.IsSelected = True Then

Me.ListBox1.Ite ms.Add(CStr(e.I temIndex))


Me.ListBox1.Ite ms.Remove(CStr( e.ItemIndex))

End If

Me.ListBox1.End Update()

End Sub

End Class

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

Form designer partial class for control setup on form

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

<Global.Microso ft.VisualBasic. CompilerService s.DesignerGener ated()> _

Partial Class Form1

Inherits System.Windows. Forms.Form

'Form overrides dispose to clean up the component list.

<System.Diagnos tics.DebuggerNo nUserCode()> _

Protected Overrides Sub Dispose(ByVal disposing As Boolean)

If disposing AndAlso components IsNot Nothing Then

components.Disp ose()

End If

MyBase.Dispose( disposing)

End Sub

'Required by the Windows Form Designer

Private components As System.Componen tModel.IContain er

'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.Diagnos tics.DebuggerSt epThrough()> _

Private Sub InitializeCompo nent()

Me.components = New System.Componen tModel.Containe r

Dim resources As System.Componen tModel.Componen tResourceManage r = New
System.Componen tModel.Componen tResourceManage r(GetType(Form1 ))

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

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

Me.ListView1 = New WindowsApplicat ion9.DoubleBuff eredListView

Me.clmTypeIcon = New System.Windows. Forms.ColumnHea der

Me.clmStatusIco n = New System.Windows. Forms.ColumnHea der

Me.clmAttachmen tIcon = New System.Windows. Forms.ColumnHea der

Me.clmJournalTy pe = New System.Windows. Forms.ColumnHea der

Me.clmEntryType = New System.Windows. Forms.ColumnHea der

Me.clmCreatedBy = New System.Windows. Forms.ColumnHea der

Me.clmDateCreat ed = New System.Windows. Forms.ColumnHea der

Me.clmAccount = New System.Windows. Forms.ColumnHea der

Me.clmEntryMess age = New System.Windows. Forms.ColumnHea der

Me.clmStatus = New System.Windows. Forms.ColumnHea der

Me.clmDueDate = New System.Windows. Forms.ColumnHea der

Me.clmEnrollee = New System.Windows. Forms.ColumnHea der

Me.SuspendLayou t()




Me.ImageList1.I mageStream =
CType(resources .GetObject("Ima geList1.ImageSt ream"),
System.Windows. Forms.ImageList Streamer)

Me.ImageList1.T ransparentColor = System.Drawing. Color.Transpare nt

Me.ImageList1.I mages.SetKeyNam e(0, "EMAILhighimpor tance.png")

Me.ImageList1.I mages.SetKeyNam e(1, "EMAILlowimport ance.png")

Me.ImageList1.I mages.SetKeyNam e(2, "bookclosedpurp le.png")

Me.ImageList1.I mages.SetKeyNam e(3, "bookopengreen. png")

Me.ImageList1.I mages.SetKeyNam e(4, "bookopenred.pn g")

Me.ImageList1.I mages.SetKeyNam e(5, "bookopenyellow .png")




Me.ListBox1.For mattingEnabled = True

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

Me.ListBox1.Nam e = "ListBox1"

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

Me.ListBox1.Tab Index = 1




Me.ListView1.Co lumns.AddRange( New System.Windows. Forms.ColumnHea der()
{Me.clmTypeIcon , Me.clmStatusIco n, Me.clmAttachmen tIcon, Me.clmJournalTy pe,
Me.clmEntryType , Me.clmCreatedBy , Me.clmDateCreat ed, Me.clmAccount,
Me.clmEntryMess age, Me.clmStatus, Me.clmDueDate, Me.clmEnrollee} )

Me.ListView1.Do ck = System.Windows. Forms.DockStyle .Top

Me.ListView1.Fu llRowSelect = True

Me.ListView1.Hi deSelection = False

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

Me.ListView1.Na me = "ListView1"

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

Me.ListView1.Sm allImageList = Me.ImageList1

Me.ListView1.So rting = System.Windows. Forms.SortOrder .Ascending

Me.ListView1.Ta bIndex = 0

Me.ListView1.Us eCompatibleStat eImageBehavior = False

Me.ListView1.Vi ew = System.Windows. Forms.View.Deta ils




Me.clmTypeIcon. Name = "clmTypeIco n"

Me.clmTypeIcon. Width = 21




Me.clmStatusIco n.Name = "clmStatusI con"

Me.clmStatusIco n.Width = 21


'clmAttachmentI con


Me.clmAttachmen tIcon.Name = "clmAttachmentI con"

Me.clmAttachmen tIcon.Width = 21




Me.clmJournalTy pe.Name = "clmJournalType "




Me.clmEntryType .Name = "clmEntryTy pe"




Me.clmCreatedBy .Name = "clmCreated By"




Me.clmDateCreat ed.Name = "clmDateCreated "




Me.clmAccount.N ame = "clmAccount "


'clmEntryMessag e


Me.clmEntryMess age.Name = "clmEntryMessag e"

Me.clmEntryMess age.Width = 300




Me.clmStatus.Na me = "clmStatus"




Me.clmDueDate.N ame = "clmDueDate "




Me.clmEnrollee. Name = "clmEnrolle e"




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

Me.AutoScaleMod e = System.Windows. Forms.AutoScale Mode.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 DoubleBufferedL istView

Friend WithEvents clmIconA As System.Windows. Forms.ColumnHea der

Friend WithEvents clmIconB As System.Windows. Forms.ColumnHea der

Friend WithEvents clmAttachments As System.Windows. Forms.ColumnHea der

Friend WithEvents clmJournalType As System.Windows. Forms.ColumnHea der

Friend WithEvents clmType As System.Windows. Forms.ColumnHea der

Friend WithEvents clmUser As System.Windows. Forms.ColumnHea der

Friend WithEvents clmDate As System.Windows. Forms.ColumnHea der

Friend WithEvents clmAccount As System.Windows. Forms.ColumnHea der

Friend WithEvents clmMessage As System.Windows. Forms.ColumnHea der

Friend WithEvents clmStatus As System.Windows. Forms.ColumnHea der

Friend WithEvents clmDueDate As System.Windows. Forms.ColumnHea der

Friend WithEvents clmEnrollee As System.Windows. Forms.ColumnHea der

Friend WithEvents clmTypeIcon As System.Windows. Forms.ColumnHea der

Friend WithEvents clmStatusIcon As System.Windows. Forms.ColumnHea der

Friend WithEvents clmAttachmentIc on As System.Windows. Forms.ColumnHea der

Friend WithEvents clmEntryType As System.Windows. Forms.ColumnHea der

Friend WithEvents clmCreatedBy As System.Windows. Forms.ColumnHea der

Friend WithEvents clmDateCreated As System.Windows. Forms.ColumnHea der

Friend WithEvents clmEntryMessage As System.Windows. Forms.ColumnHea der

Friend WithEvents ImageList1 As System.Windows. Forms.ImageList

Friend WithEvents ListBox1 As System.Windows. Forms.ListBox

End Class
Nov 21 '05 #1
0 3237

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

Similar topics

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 sort columns based on the column header the user has clicked in both Ascending and Descending formats.
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 view in .NET 2.0 with multiple selection turned on... since the .NET documentation is EXTREAML vague on how to do this and offers no clue on what the virtual mode events do or how to use them... just thought this might help someone else out...
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 alpha channel transparent one that windows xp's shell uses... is there any way to just keep the standard dotted rectangle one? because performance is slow when the transparent rectangle region is enabled with double buffering... but i cant find a way...
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 answer but anyways here is my code any help would be greatly apperciated. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load lvTest.Items.Add("1001") lvTest.Items.Add("5000")...
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... now I control click 3 items of that list off... how in the world do I know whats selected and how do I tell? better yet... whats the best way to determin what is selected in a virtual mode list view?
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 seed, or your constraints, or your triggers, or your table. Talking to developers at other companies who have had the misfortune of using DB2, they are adamant that you cannot use the tools; they are buggy and you just have to resign yourself to...
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 sorting for LinkedList<myself? Thanks in advance Martin
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 items, but when I set it to 1,000,000 the scroll bars behave like there are 1,000,000 items, but rendering doesn't reflect that number. The "index" property in the owner draw render override seems to state the correct number however. For example,...
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" link as the leftmost column. Clicking on this displays employee details in the detail view. The detail view further has Edit, Delete and New links, that help to maintain data. My problem is this -
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
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 we have to send another system

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.