Results 1 to 5 of 5

Thread: List View Help

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2007
    Posts
    167

    List View Help

    I need help working with List View
    How can I add Column Headers and then Specific Items to each Column?

    I want to do this:

    Name Age <-- Headers
    John 21
    Mike 12
    Lara 23


    ....

    thx

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: List View Help

    See if this works for you:
    Code:
    Public Class Form1
    
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
        With ListView1
            .View = View.Details
    
            .Columns.Add("Name")
            .Columns.Add("Age")
    
            .Items.Add("John")
            .Items(.Items.Count - 1).SubItems.Add(21)
    
            .Items.Add("Mike")
            .Items(.Items.Count - 1).SubItems.Add(12)
    
            .Items.Add("Lara")
            .Items(.Items.Count - 1).SubItems.Add(23)
        End With
    
    End Sub
    
    End Class

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Dec 2007
    Posts
    167

    Re: List View Help

    thx
    I have another question
    how can I sort the item in ascending order of the AGE column (when the user clicks on the Age header)?

    thanks alot

  4. #4
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: List View Help

    Tried a search? Search the forums for "listview column sort", without the quotationmarks.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: List View Help

    that should be a new thread really.

    to sort a listview, you use an iComparer.
    heres an example

    vb Code:
    1. Public Class clsListViewItemComparer
    2.     Implements IComparer
    3.  
    4.     Private _Column As Integer
    5.     Private _Numeric As Boolean = False
    6.     Private _isDate As Boolean = False
    7.     Private _sortAscending As Boolean = False
    8.  
    9.     Public Property Column() As Integer
    10.         Get
    11.             Return _Column
    12.         End Get
    13.         Set(ByVal Value As Integer)
    14.             _Column = Value
    15.         End Set
    16.     End Property
    17.  
    18.     Public Property Numeric() As Boolean
    19.         Get
    20.             Return _Numeric
    21.         End Get
    22.         Set(ByVal Value As Boolean)
    23.             _Numeric = Value
    24.         End Set
    25.     End Property
    26.  
    27.     Public Property isDate() As Boolean
    28.         Get
    29.             Return _isDate
    30.         End Get
    31.         Set(ByVal Value As Boolean)
    32.             _isDate = Value
    33.         End Set
    34.     End Property
    35.  
    36.     Public Property sortAscending() As Boolean
    37.         Get
    38.             Return _sortAscending
    39.         End Get
    40.         Set(ByVal Value As Boolean)
    41.             _sortAscending = Value
    42.         End Set
    43.     End Property
    44.  
    45.     Public Sub New(ByVal columnIndex As Integer)
    46.         Column = columnIndex
    47.     End Sub
    48.  
    49.     Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
    50.         Dim ListX As ListViewItem = CType(x, ListViewItem)
    51.         Dim ListY As ListViewItem = CType(y, ListViewItem)
    52.  
    53.         If Numeric Then
    54.             ' Convert column text to numbers before comparing.
    55.             ' If the conversion fails, just use the value 0.
    56.             Dim ListXVal, ListYVal As Decimal
    57.             Try
    58.                 ListXVal = Decimal.Parse(Replace(ListX.SubItems(Column).Text, "£", ""))
    59.             Catch
    60.                 ListXVal = 0
    61.             End Try
    62.  
    63.             Try
    64.                 ListYVal = Decimal.Parse(Replace(ListY.SubItems(Column).Text, "£", ""))
    65.             Catch
    66.                 ListYVal = 0
    67.             End Try
    68.  
    69.             If Not sortAscending Then
    70.                 Return Decimal.Compare(ListYVal, ListXVal)
    71.             Else
    72.                 Return Decimal.Compare(ListXVal, ListYVal)
    73.             End If
    74.  
    75.         ElseIf isDate Then
    76.             Dim ListXVal, ListYVal As Date
    77.             Try
    78.                 ListXVal = Date.Parse(ListX.SubItems(Column).Text)
    79.             Catch
    80.                 'ListXVal = 0
    81.             End Try
    82.  
    83.             Try
    84.                 ListYVal = Date.Parse(ListY.SubItems(Column).Text)
    85.             Catch
    86.                 'ListYVal = 0
    87.             End Try
    88.  
    89.             If Not sortAscending Then
    90.                 Return Date.Compare(ListYVal, ListXVal)
    91.             Else
    92.                 Return Date.Compare(ListXVal, ListYVal)
    93.             End If
    94.  
    95.         Else
    96.             ' Keep the column text in its native string format
    97.             ' and perform an alphabetic comparison.
    98.             Dim ListXText As String = ListX.SubItems(Column).Text
    99.             Dim ListYText As String = ListY.SubItems(Column).Text
    100.  
    101.             If Not sortAscending Then
    102.                 Return String.Compare(ListYText, ListXText)
    103.             Else
    104.                 Return String.Compare(ListXText, ListYText)
    105.             End If
    106.  
    107.         End If
    108.  
    109.     End Function
    110. End Class
    then you assign the comparer class to the listview like this

    vb Code:
    1. Private Sub listview1_ColumnClick(ByVal sender As Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles listview1.ColumnClick
    2.  
    3.         If e.Column = 0 Then Exit Sub
    4.  
    5.         Dim Sorter As New clsListViewItemComparer(e.Column)
    6.         Sorter.Numeric = True
    7.         Sorter.sortAscending = True
    8.         listview1.ListViewItemSorter = Sorter
    9.         listview1.Sort()
    10.         listview1.ListViewItemSorter = nothing
    11.  
    12. End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width