Re: list view question

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Dino Buljubasic (dino_at_noplacelikehome.com)
Date: 03/16/04


Date: Tue, 16 Mar 2004 16:56:04 GMT


Hi Ahmed,

just forgot to tell you, from the class code I sent to you, remove the
line :

 If TypeOf CallerForm Is Demeter25.frmViewEditInactiveDocuments Then

and its end statement (which I think removed already).

Dino

On Tue, 16 Mar 2004 15:30:27 +0200, "Sameh Ahmed"
<essoplus@hotmail.com> wrote:

>Thanks a lot
>will test and update you
>thanks for your time
>Sameh
>"Dino Buljubasic" <dino@noplacelikehome.com> wrote in message
>news:04gc50t2j5ok4vq2uoosuf41h190g04i40@4ax.com...
>>
>> 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/>
>> >
>>
>



Relevant Pages

  • Re: a question for sorting keys in Map
    ... I have a Map, actually a TreeMap, which will automatically sort the keys ... You can write your own Comparator object which implements the Compare ... strings so that X10 comes after X2 instead of before it. ... Unix ls command sort file names the way you want your strings to sort. ...
    (comp.lang.java.programmer)
  • Re: list view question
    ... > sort data other than strings and columns other than first column. ... > column to perform sorting on and then to do the sorting on that column ... > Public Sub New(ByVal aColumn As Integer, ByVal order As SortOrder, ...
    (microsoft.public.dotnet.languages.vb.controls)
  • Re: how to do a "REAL" string compare?
    ... > list control, but thats not important) displayed to the user. ... > list based on the list controls sort function (again, ... > its Windows) which ends up calling a compare function in my code: ... I think the problem here is treating the data as C strings. ...
    (comp.lang.cpp)
  • Re: Function that compares 2 alpha-numeric values?
    ... >Does anyone have a code snippet to compare those values so I can sort ... >The strings can include character values and strings. ...
    (comp.lang.javascript)
  • Re: BWT, tuning for speed
    ... then sort by the next-rightmost character; ... )> one for the center where you compare on the next set of N bytes. ... )> Suppose you have sorted all strings starting with 'a'. ...
    (comp.compression)