Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to use listview in VB.NET?

64 views
Skip to first unread message

Alexei Smirnov

unread,
Jan 9, 2002, 10:02:54 AM1/9/02
to
anybody knows how to get itemid when user clicks to listview (e.g.
listview1_doubleclick event)
and how to order columns (sorting property) when user clicks to column
header?

Thx


Dushan Bilbija

unread,
Jan 9, 2002, 1:56:11 PM1/9/02
to
hi alexei

i populate the key property of each listitem with the unique id... then use
listview.selecteditem.key to get it

can't remember how to sort off the top of my head... the sdk has it... i
believe it's straightforward...

hope this helps

dushan bilbija


"Alexei Smirnov" <sma...@hotmail.com> wrote in message
news:#aZr46RmBHA.1644@tkmsftngp05...

Alexei Smirnov

unread,
Jan 10, 2002, 3:22:45 AM1/10/02
to
> i populate the key property of each listitem with the unique id... then
use
> listview.selecteditem.key to get it

Are you talking about .NET, or VB6? I know how to get it in VB6, but I need
it in new version.

Dushan Bilbija

unread,
Jan 10, 2002, 9:12:53 PM1/10/02
to
oops...

reflex reply... that's vb6 talk

in vb.net i use the tag property of the item.

btw... the sorting question? not straightforward, but here's the code:

Private Sub ListView1_ColumnClick(ByVal sender As Object, ByVal e As
System.WinForms.ColumnClickEventArgs) Handles ListView1.ColumnClick
ListView1.Sorting = SortOrder.Ascending
Dim Sorter = New ListViewSorter(e.column)

ListView1.ListItemSorter = Sorter

End Sub

Public Class ListViewSorter

Implements System.Collections.IComparer

Public SortIndex As Integer

Public Sub New(ByVal SortIndex As Integer)

Me.SortIndex = SortIndex

End Sub

Public Function Compare_(ByVal x As Object, ByVal y As Object) As
Integer Implements System.Collections.IComparer.Compare

If SortIndex = 0 Then

Return String.Compare(x.text, y.text)

Else

Return String.Compare(x.GetSubItem(SortIndex - 1),
y.GetSubItem(SortIndex - 1))

End If

End Function

End Class

hope this helps

dushan bilbija

"Alexei Smirnov" <sma...@hotmail.com> wrote in message

news:ec4J9$amBHA.2012@tkmsftngp04...

Alexei Smirnov

unread,
Jan 14, 2002, 5:18:12 AM1/14/02
to
hi Dushan,

thanks again for the answer, but it not helps me :-)

I saw somewhere already code you sent me, but when I copy it to my form, I
receive an error "ListView1.ListItemSorter private and can't be public" (or
something like this...). Maybe I made something wrong, but can you again
explain me, how to use this code? Should I copy "Class ListViewSorter" to
form body, or I need to create a new class?

Maybe it would be nice if you could sent me zip with your form, and I can
check where was my error.

thx,
Alexei

"Dushan Bilbija" <dbil...@email.msn.com> wrote in message
news:OEfmLWkmBHA.2212@tkmsftngp05...

Gary

unread,
Jan 14, 2002, 7:48:00 AM1/14/02
to
Use the SortKey property:

Private Sub ListView1_ColumnClick(ByVal ColumnHeader As ColumnHeader)
' When a ColumnHeader object is clicked, the ListView control is
' sorted by the subitems of that column.
' Set the SortKey to the Index of the ColumnHeader - 1
ListView1.SortKey = ColumnHeader.Index - 1
' Set Sorted to True to sort the list.
ListView1.Sorted = True
End Sub

For more information, please refer to
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cmctl198/ht
ml/vbprosortorderx.asp

Regards,

This posting is provided “AS IS”, with no warranties, and confers no
rights.

Alexei Smirnov

unread,
Jan 15, 2002, 3:22:58 AM1/15/02
to
.NET please!

"Gary" <garyo...@microsoft.com> wrote in message
news:x7pA6mPnBHA.2216@cpmsftngxa07...

Gary

unread,
Jan 17, 2002, 9:25:40 PM1/17/02
to
1. Use the ListView1.SelectedIndices.Item(0) to get the index of the
currently selected Item

2. By default, we can only sort the the ListView control based on first
column as follows:

Private Sub ListView1_ColumnClick(ByVal sender As Object, ByVal e As

System.Windows.Forms.ColumnClickEventArgs) Handles ListView1.ColumnClick
'MessageBox.Show(e.Column.ToString())
'listview1.ListViewItemSorter=
If ListView1.Sorting = SortOrder.Descending Then
ListView1.Sorting = SortOrder.Ascending
ElseIf ListView1.Sorting = SortOrder.Ascending Then
ListView1.Sorting = SortOrder.Descending
End If
End Sub

To sort on the clicked column, you could create a class (that implements
the IComparer interface) to support column sorting in the ListView control.
If an instance of the class is assigned to the ListViewItemSorter property
and the Sort method is called, you can then create code in the event
handler for the ColumnClick event to perform sorting based on the column
that was clicked.

0 new messages