This documentation is archived and is not being maintained.

DataGridItem.ItemIndex Property

Gets the index of the DataGridItem object from the Items collection of the DataGrid control.

Namespace: System.Web.UI.WebControls
Assembly: System.Web (in system.web.dll)

public virtual int ItemIndex { get; }
/** @property */
public int get_ItemIndex ()

public function get ItemIndex () : int

Property Value

The index of the DataGridItem from the Items collection of the DataGrid control.

Use the ItemIndex property to determine the index of the DataGridItem object from the Items collection of the DataGrid control.


This property applies only to data items in the DataGrid control. The ItemType property of the DataGridItem object must be set to ListItemType.Item, ListItemType.AlternatingItem, ListItemType.SelectedItem, or ListItemType.EditItem.

The following code example demonstrates how to use the ItemIndex property to display the index of the DataGridItem object from the Items collection of the DataGrid control.


The following code example uses the single-file code model and might not work correctly if copied directly into a code-behind file. This code example must be copied into an empty text file that has an .aspx extension. For more information on the Web Forms code model, see Events and Delegates.

<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
<script language="C#" runat="server">
   DataTable Cart;
   DataView CartView;
   ICollection CreateDataSource() 
      DataTable dt = new DataTable();
      DataRow dr;
      dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
      dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
      dt.Columns.Add(new DataColumn("CurrencyValue", typeof(double)));
      for (int i = 0; i < 10; i++) 
         dr = dt.NewRow();
         dr[0] = i;
         dr[1] = "Item " + i.ToString();
         dr[2] = 1.23 * (i+1);
      DataView dv = new DataView(dt);
      return dv;
   void Page_Load(Object sender, EventArgs e) 
      if (!IsPostBack) 
         // Need to load this data only once.
         ItemsGrid.DataSource = CreateDataSource();
   void Button_Click(Object sender, EventArgs e) 

      Label1.Text = "The DataSetIndex of each item in the DataGrid are: <br>"; 
      foreach (DataGridItem item in ItemsGrid.Items)
         Label1.Text += "<br>" + item.ItemIndex.ToString() +
                        " - " + item.Cells[1].Text;
   <form runat=server>

      <h3>DataGridItem ItemIndex Example</h3>
      <asp:DataGrid id="ItemsGrid" runat="server"

         <HeaderStyle BackColor="#00aaaa">

         <FooterStyle BackColor="#00aaaa">

      <asp:Button id="Button1"
           Text="Display ItemIndex of Items in DataGrid"

      <asp:Label id="Label1" 

Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0