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
Printable View
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
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
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
Tried a search?;) Search the forums for "listview column sort", without the quotationmarks.
that should be a new thread really.
to sort a listview, you use an iComparer.
heres an example
then you assign the comparer class to the listview like thisvb 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
vb Code:
Private Sub listview1_ColumnClick(ByVal sender As Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles listview1.ColumnClick If e.Column = 0 Then Exit Sub Dim Sorter As New clsListViewItemComparer(e.Column) Sorter.Numeric = True Sorter.sortAscending = True listview1.ListViewItemSorter = Sorter listview1.Sort() listview1.ListViewItemSorter = nothing End Sub