ListView.SelectedIndex Property

Definition

Gets or sets the index of the selected item in a ListView control.

C#
public virtual int SelectedIndex { get; set; }

Property Value

The zero-based index of the selected item in a ListView control. The default is -1, which indicates that no item is currently selected.

Exceptions

The SelectedIndex property is set to a value less than -1.

Examples

The following example shows how to use the SelectedIndex property to determine whether an item is selected in a ListView control. This code example is part of a larger example provided for the DeleteItem method.

C#
protected void DeleteButton_Click(object sender, EventArgs e)
{
  //Check if an item is selected to delete it.
  if (ContactsListView.SelectedIndex >= 0)
    ContactsListView.DeleteItem(ContactsListView.SelectedIndex);
  else
    Message.Text = "No contact was selected.";
}

Remarks

Use the SelectedIndex property to determine the index of the currently selected item in a ListView control. You can also use this property to programmatically select an item in the control. (However, if you manually data-bind the ListView control, you might have to handle reading or setting this property manually as well.)

Applies to

Product Versions
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

See also