Selecting a row in ListView

ravimeetsu

Newcomer
Joined
Dec 19, 2002
Messages
3
Hi

A specific question is that whether I can select a particular row in List View, show the modified row as highlighted and know the row number ??

Regards and Thanks
Ravi Hasija
 
Do you mean you want to select a row with a specific index? If so:

Visual Basic:
MyListView.Items(itemIndex).Selected = True
 
ravimeetsu sent me this PM, so I figured I'd answer it here.

Hi

Thanks for your concern. I will be more specific this time.

I want to modify certain row.

My first concern is how to let a user select a row and then how to know which row he selected.

For example is it possible for a user to click on a certain row, then I highlight it and then I delete / modify according to my requirement.

Please help me with this and feel free to suggest even outside the scope of the question.

Regards and Thanks,
Ravi Hasija

When a row is selected by the user, the SelectedIndexChanged
event of the ListView control will fire. In this event, you can get
the selected item from the ListView and work with it (where lvw
is the name of the ListView control):

Visual Basic:
Private Sub lvw_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lvw.SelectedIndexChanged
  Dim selectedItem As ListViewItem

  selectedItem = lvw.SelectedItems(0)

  ' Do something with selectedItem
End Sub

If your ListView allows multiple selections by the user, then the
other selected items will also be in the SelectedItems collection of
the ListView.
 
Index number of selected Item?

Good answers in this thread, Bucky. One further question:

Is there a way to get the actual Index number of the selected item? (the sequential number in the ListView) ?
 
You might try something like this:
Visual Basic:
Dim col As ListViewItem.ListViewSubItem
Dim ret As String

If ListView1.SelectedItems.Count > 0 Then
    For Each col In ListView1.SelectedItems(0).SubItems
        ret &= col.Text & "|"
    Next

    MessageBox.Show(ret)
End If
 
Back
Top