[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
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.
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?
Re: [2005] Sort, recolumn, etc in Listview
To fix your first column selection issue, set the FullRowSelect property to True.
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?
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:
Public Class clsListViewItemComparer
Implements IComparer
Private _Column As Integer
Private _Numeric As Boolean = False
Private _isDate As Boolean = False
Private _sortAscending As Boolean = False
Public Property Column() As Integer
Get
Return _Column
End Get
Set(ByVal Value As Integer)
_Column = Value
End Set
End Property
Public Property Numeric() As Boolean
Get
Return _Numeric
End Get
Set(ByVal Value As Boolean)
_Numeric = Value
End Set
End Property
Public Property isDate() As Boolean
Get
Return _isDate
End Get
Set(ByVal Value As Boolean)
_isDate = Value
End Set
End Property
Public Property sortAscending() As Boolean
Get
Return _sortAscending
End Get
Set(ByVal Value As Boolean)
_sortAscending = Value
End Set
End Property
Public Sub New(ByVal columnIndex As Integer)
Column = columnIndex
End Sub
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
Dim ListX As ListViewItem = CType(x, ListViewItem)
Dim ListY As ListViewItem = CType(y, ListViewItem)
If Numeric Then
' Convert column text to numbers before comparing.
' If the conversion fails, just use the value 0.
Dim ListXVal, ListYVal As Decimal
Try
ListXVal = Decimal.Parse(Replace(ListX.SubItems(Column).Text, "£", ""))
Catch
ListXVal = 0
End Try
Try
ListYVal = Decimal.Parse(Replace(ListY.SubItems(Column).Text, "£", ""))
Catch
ListYVal = 0
End Try
If Not sortAscending Then
Return Decimal.Compare(ListYVal, ListXVal)
Else
Return Decimal.Compare(ListXVal, ListYVal)
End If
ElseIf isDate Then
Dim ListXVal, ListYVal As Date
Try
ListXVal = Date.Parse(ListX.SubItems(Column).Text)
Catch
'ListXVal = 0
End Try
Try
ListYVal = Date.Parse(ListY.SubItems(Column).Text)
Catch
'ListYVal = 0
End Try
If Not sortAscending Then
Return Date.Compare(ListYVal, ListXVal)
Else
Return Date.Compare(ListXVal, ListYVal)
End If
Else
' Keep the column text in its native string format
' and perform an alphabetic comparison.
Dim ListXText As String = ListX.SubItems(Column).Text
Dim ListYText As String = ListY.SubItems(Column).Text
If Not sortAscending Then
Return String.Compare(ListYText, ListXText)
Else
Return String.Compare(ListXText, ListYText)
End If
End If
End Function
End Class
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)?
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?
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.
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!