Results 1 to 10 of 10

Thread: [2005] Sort, recolumn, etc in Listview

  1. #1

    Thread Starter
    Hyperactive Member mulhearn22's Avatar
    Join Date
    Jun 2007
    Location
    Cherry Hill, NJ
    Posts
    347

    [2005] Sort, recolumn, etc in Listview

    Hi,

    I'm making an audio player and want to implement the common listview that applications like iTunes, SongBird, etc implement. They all have the column titles, which you can drag and rearrange, sort by column, add/remove columns, etc. Anyone have a tutorial I can look at or somewhere I can start? I'm thinking I have to make my own control with the ListView as my base but I'm not sure. All help will be welcome.

    TIA,
    Matt
    VS 2010 / .NET 4.0 / ASP.NET 4.0

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

    Re: [2005] Sort, recolumn, etc in Listview

    what information will you have in your columns?
    do you want to be able to sort on any column?

    you can set .allowcolumnreorder so you can reorder your columns, but if you are planning on having multiple sorting columns, remember you'll need to keep track of the columns by title instead of by index number.

  3. #3

    Thread Starter
    Hyperactive Member mulhearn22's Avatar
    Join Date
    Jun 2007
    Location
    Cherry Hill, NJ
    Posts
    347

    Re: [2005] Sort, recolumn, etc in Listview

    Yes, I would want to allow sorting by any column, which in of itself would be dynamic. It's a music player, so like iTunes, I want to be able to do allow the user to right click the column headers, select what columns to display, and sort by them. I did find that property that allowed for reordering which is great, but I'm still stuck on the others.

    Also, my listview is only allowing me to select the first column and now the whole row when I select a row. To clarify, when you select a row, the row's bgcolor changes. The bgcolor only changes for the first column. Though the program understands that the whole row is selected, the visual effect isn't what I'm looking for. Any ideas on that?
    VS 2010 / .NET 4.0 / ASP.NET 4.0

  4. #4
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: [2005] Sort, recolumn, etc in Listview

    To fix your first column selection issue, set the FullRowSelect property to True.

  5. #5

    Thread Starter
    Hyperactive Member mulhearn22's Avatar
    Join Date
    Jun 2007
    Location
    Cherry Hill, NJ
    Posts
    347

    Re: [2005] Sort, recolumn, etc in Listview

    Excellent Negative0. I was looking for that property but I thought it would be something like "SelectEntireRow" or something. Of course they would start it with 'F'....

    Anyone have ideas about my other issue?
    VS 2010 / .NET 4.0 / ASP.NET 4.0

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

    Re: [2005] Sort, recolumn, etc in Listview

    if the information in your columns is all strings, its easy.
    if you have different datatypes in your columns (strings, numbers, dates) you need to know which column has been clicked + sort it by the appropriate datatype. heres a sorter i use for a listview withe strings, currency, + dates:

    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

  7. #7

    Thread Starter
    Hyperactive Member mulhearn22's Avatar
    Join Date
    Jun 2007
    Location
    Cherry Hill, NJ
    Posts
    347

    Re: [2005] Sort, recolumn, etc in Listview

    I'm sorry to make you walk me through this, but how would I go about implementing that (assuming I'm sorting strings)?
    VS 2010 / .NET 4.0 / ASP.NET 4.0

  8. #8

    Thread Starter
    Hyperactive Member mulhearn22's Avatar
    Join Date
    Jun 2007
    Location
    Cherry Hill, NJ
    Posts
    347

    Re: [2005] Sort, recolumn, etc in Listview

    hey, actually i got it. however, now i think i need to sort based on multiple columns (such as by artist, then by album, then by track number so that tracks stick together and are in order). any ideas on that one?
    VS 2010 / .NET 4.0 / ASP.NET 4.0

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

    Re: [2005] Sort, recolumn, etc in Listview

    can you post some example information? i'll put it in a listview + try to write a custom sorter. its not difficult but its always easier when you've got something to test it with.

  10. #10

    Thread Starter
    Hyperactive Member mulhearn22's Avatar
    Join Date
    Jun 2007
    Location
    Cherry Hill, NJ
    Posts
    347

    Re: [2005] Sort, recolumn, etc in Listview

    im not exactly sure what to post. basically, i have a listview with artist, album, title, track num, year, genre. if you sort by say track num and then by artist, the tracks get out of order because i'm only sorting by artist, not artist, then album, then track. iTunes does this really well.

    if this is unclear, let me know what code you're looking for and i'll post it. thanks for the help!
    VS 2010 / .NET 4.0 / ASP.NET 4.0

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