Re: list view question
From: Dino Buljubasic (dino_at_noplacelikehome.com)
Date: 03/16/04
- Next message: tspratling:: "I have a windows xp problem"
- Previous message: Herfried K. Wagner [MVP]: "Re: list view question"
- In reply to: Sameh Ahmed: "Re: list view question"
- Next in thread: Sameh Ahmed: "Re: list view question"
- Reply: Sameh Ahmed: "Re: list view question"
- Messages sorted by: [ date ] [ thread ]
Date: Tue, 16 Mar 2004 00:10:37 GMT
Hi Ahmed,
sorting columns in .net is not as simple as in VB. .net sorts column
as strings (by default only first colum) so you must provide a way to
sort data other than strings and columns other than first column.
To do that, you need a new class that implements Icomparer that
exposes compare method to compare two objects.
Inside the Compare function of your class, you need to check which
column to perform sorting on and then to do the sorting on that column
by the type of values it contains.
To do this you need to:
1) Inside the form that holds the list view - add inside construcotr
of the form line:
AddHandler lvwMyList.ColumnClick, AddressOf Me.lvwMyList_ColumnClick
, then add event lvwMyList_ColumnClick()
Now you can build your class clsSorter as :
--------- CLASS CODE -------------------------------------------------
' class : classSortER
' purpose : This class is providing functionality to sort list view
items in
' A form depending on which column of the list view has
been
' clicked. This is necessary because vb.net will
otherwise do
' sorting on first column only and the default sort is on
strings.
' sorting : ascending by default
'
' requirements
' - in form's constructor, add following line of code
' AddHandler lvwMyList.ColumnClick, AddressOf
Me.lvwMyList_ColumnClick
' to connect the event handler with the list view
column click
' event.
' - add lvwMyList_ColumnClick event in your form
Public Class classSorter
Implements IComparer ' exposes method (compare) to compare two
objects
Private col As Integer ' column to sort
Private order As SortOrder ' asc or desc
'Dim blnSortingIn As Boolean ' true, sort ascending; false, sort
descending
Public Sub New()
col = 0 ' sort first col by default in ascending order
order = SortOrder.Ascending
End Sub
' c-tor that receives column to sort and in which order
Public Sub New(ByVal aColumn As Integer, ByVal order As SortOrder,
ByVal aForm As Form)
col = aColumn
Me.order = order
End Sub
' method to compare two objects (list view items)
Public Function Compare(ByVal x As Object, ByVal y As Object) _
As Integer Implements
System.Collections.IComparer.Compare
Dim returnVal As Integer = -1
Dim xItem As System.Windows.Forms.ListViewItem = x
Dim yItem As System.Windows.Forms.ListViewItem = y
If TypeOf CallerForm Is Demeter25.frmViewEditInactiveDocuments
Then
' if the caller form is frmViewEditInactiveDocuments then
sort according the
' columns in select case statement below
Select Case col
Case 0 ' in this case 0th column holds numerics
' sort numeric values
Dim blnCompare As Boolean
blnCompare = Val(xItem.SubItems(col).Text) >
Val(yItem.SubItems(col).Text)
If blnCompare Then
returnVal = 1
Else
returnVal = -1
End If
Case 1 ' in this case 1th column holds strings
' sort strings
returnVal = [String].Compare(CType(x,
ListViewItem).SubItems(col).Text, CType(y,
ListViewItem).SubItems(col).Text)
Case 2 '' in this case 2nd column holds datetime vals
' sort date time values
returnVal =
DateTime.Compare(CDate(xItem.SubItems(col).Text),
CDate(yItem.SubItems(col).Text))
Case 3 '' in this case 3rd column holds datetime
vals, too
' sort date time values
returnVal =
DateTime.Compare(CDate(xItem.SubItems(col).Text),
CDate(yItem.SubItems(col).Text))
Case 4 '' in this case 0th column holds numerics
' sort numeric values
Dim blnCompare As Boolean
blnCompare = Val(xItem.SubItems(col).Text) >
Val(yItem.SubItems(col).Text)
If blnCompare Then
returnVal = 1
Else
returnVal = -1
End If
Case Else
End Select
' is sort order descending
If order = SortOrder.Descending Then
' ivert the value returned by string.compare.
returnVal *= -1
End If
Return returnVal
End Function
End Class
---------END OF CLASS CODE----------------------------------------
Now, in variable declaration part of your form add:
dim sortColumn as integer = -1 ' no sorting yet
inside the lvwMyList_ColumnClick event add:
If e.Column <> sortColumn then
sortColumn = e.column
lvwMyList.Sorting = Sorting.Ascending ' asc by default
if lvwMyList.Sorting = sortOrder.Ascending then
lvwMyList.Sorging = SortOrder.Descending
else
lvwMyList.Sorging = SortOrder.Ascending
end if
lvwMyList.Sort()
lvwMyList.ListViewItemSorter = New clsSorter(e.Column,
lvwMyList.Sorting)
end if
hope this helps,
Dino
On Mon, 15 Mar 2004 22:48:34 +0200, "Sameh Ahmed"
<essoplus@hotmail.com> wrote:
>Excellent
>both worked fine:)
>now a another quick question if possible
>I set the tree view Sorting to Ascending
>what if I want it to be sorting depending on the column that the user clicks
>it's header
>Example:
>when he click the header column header of emplid it will be sorted by that
>column and so on?
>Thanks for your time
>Regards
>Sameh
>
>"Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
>news:c352l9$22kfb0$2@ID-208219.news.uni-berlin.de...
>> * "Sameh Ahmed" <essoplus@hotmail.com> scripsit:
>> > Hello there
>> > I have a listview with 3 header columns, I can populate the first and
>second
>> > one with the following code, but cannot figure out how to populate the
>third
>> > and the following header columns?
>> > listview.Items.Add(empname)
>> > listview.Items(i).SubItems.Add(empid)
>>
>> The same as the line above. Add a 2nd 'SubItem' to 'Items(i)'.
>>
>> --
>> Herfried K. Wagner [MVP]
>> <http://dotnet.mvps.org/>
>
- Next message: tspratling:: "I have a windows xp problem"
- Previous message: Herfried K. Wagner [MVP]: "Re: list view question"
- In reply to: Sameh Ahmed: "Re: list view question"
- Next in thread: Sameh Ahmed: "Re: list view question"
- Reply: Sameh Ahmed: "Re: list view question"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|