0 out of 1 rated this helpful - Rate this topic

ListView.SelectedIndex Property

Updated: May 2011

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

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web.Extensions (in System.Web.Extensions.dll)
public virtual int SelectedIndex { get; set; }
<asp:ListView SelectedIndex="Int32" />

Property Value

Type: System.Int32
The zero-based index of the selected item in a ListView control. The default is -1, which indicates that no item is currently selected.
Exception Condition
ArgumentOutOfRangeException

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

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.)

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.


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.";
}


.NET Framework

Supported in: 4, 3.5

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Date

History

Reason

May 2011

Added an explanation that the property does not always work as described here when data-binding is performed manually.

Customer feedback.

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
ListView.selectedIndex in Visual Studio 2008 with c#
i was in search of method that would help me in getting the index of selected row of listview but could not find such meterial after playing with code i get to know a way by which i was able to find out the index of selected row of ListView detail view. $0U just have to add the event "MouseClick" to your listview. Also u may have a variable of your form that u may use to store the index value.$0 $0U may declare such variable as a public variable of your form like $0$0 $0 $0 $0$0 $0namespace YOUR_APPLICATION$0 $0{$0 $0    public partial class Form1 : Form$0 $0    {$0 $0 $0public static int HKIndex;$0 $0 $0$0 Now u may add "MouseClick" event for your listview named myListView$0 $0 $0$0 $0private void listViewHK_MouseClick(object sender, MouseEventArgs e)$0 $0        {$0 $0            if ((e.Button == MouseButtons.Right) || (e.Button == MouseButtons.Left))$0 $0            {$0 $0              ListViewHitTestInfo hit = listViewHK.HitTest(e.X, e.Y);$0 $0                if (hit.Item.Index >=0)$0 $0                {$0 $0                    HKIndex = hit.Item.Index;$0 $0                    MessageBox.Show(Convert.ToString(HKIndex));$0 $0                }$0 $0                $0 $0            }$0 $0 $0$0 HKIndex will contain your list view index. You may also check this by clicking at any of the row of your listview and a message box will be shown displaying the index of listview.$0 $0
Doesn't Work as Described
After wasting more than a few hours I finally realized that the documentation (at least for the 3.5 Version) is absolutely incorrect.&nbsp; So that other developers don't have to suffer the grief I did, I published this: http://mwtech.blogspot.com/2010/09/adding-items-to-aspnet-listview.html$0 $0 What would be even better is if the development team at Microsoft actually fixed the control to work properly.$0 $0 [Tom Dykstra - MSFT] The SelectedItem property works with automatic databanding (when you set the data source to a data source control ID). With manual databinding you might have to handle SelectedItem manually. A note about this will be added to the next release of the documentation. For more information see http://stackoverflow.com/questions/570801/programmatically-select-item-in-asp-net-listview and http://forums.asp.net/t/1305147.aspx.$0 $0$0 $0 $0[Rudi Moritz] I also spent several hours with this issue, until I found Robert's comments. After working with .NET for 8.5 years, I am still surprised about Microsoft pushing us into using the latest and the greatest, without making it work properly. These controls (it's not just the ListView) make life easier for the quick and dirty developer, but not so much for the one who has to solve complex problems.$0
In .aspx markup, set SelectedIndex to a string that can be converted to an integer
&lt;asp:ListView ID="myListView" runat="server" SelectedIndex="Int32"&gt;

causes this
Description:
An error occurred during the parsing of a resource required to service this
request. Please review the following specific parse error details and modify
your source file appropriately.

Parser Error Message: Cannot
create an object of type 'System.Int32' from its string representation 'Int32'
for the 'SelectedIndex' property.


Additionally, regardless of what I do, it seems SelectedIndex is always -1.

[Tom Dykstra - MSFT] The error in this case happens because ASP.NET is expecting an actual index number
(e.g., SelectedIndex="2") rather than a string such as "Int32". The number you set here indicates which row
will be selected when the ListView control is initially displayed. For the entire page that this method was taken
from, see http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listview.deleteitem.aspx