This documentation is archived and is not being maintained.

DataGridItem.DataSetIndex Property

Gets the index number the DataGridItem object from the bound data source.

[Visual Basic]
Public Overridable ReadOnly Property DataSetIndex As Integer
[C#]
public virtual int DataSetIndex {get;}
[C++]
public: __property virtual int get_DataSetIndex();
[JScript]
public function get DataSetIndex() : int;

Property Value

The index number of the DataGridItem object from the bound data source.

Remarks

Use the DataSetIndex property to the index number of the DataGridItem object from the bound data source.

Note   This property only applies 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.

Example

[Visual Basic, C#] The following example demonstrates how to use the DataSetIndex property to display the index number of the DataGridItem object from the bound data source.

[Visual Basic, C#] Note   The following code sample uses the single-file code model and may not work correctly if copied directly into a code-behind file. This code sample must be copied into an empty text file that has an .aspx extension. For more information on the Web Forms code model, see Web Forms Code Model.
[Visual Basic] 
<%@ Page Language="VB" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
 
<html>
<script language="VB" runat="server">
 
    Dim Cart As DataTable
    Dim CartView As DataView
 
    Function CreateDataSource() As ICollection
        Dim dt As New DataTable()
        Dim dr As DataRow
        
        dt.Columns.Add(New DataColumn("IntegerValue", GetType(Int32)))
        dt.Columns.Add(New DataColumn("StringValue", GetType(String)))
        dt.Columns.Add(New DataColumn("CurrencyValue", GetType(Double)))
        
        Dim i As Integer
        For i = 0 To 9
            dr = dt.NewRow()
            
            dr(0) = i
            dr(1) = "Item " & i.ToString()
            dr(2) = 1.23 *(i + 1)
            
            dt.Rows.Add(dr)
        Next i
        
        Dim dv As New DataView(dt)
        Return dv
    End Function 'CreateDataSource

    Sub Page_Load(sender As Object, e As EventArgs)
        
        If Not IsPostBack Then
            ' Need to load this data only once.
            ItemsGrid.DataSource = CreateDataSource()
            ItemsGrid.DataBind()
        End If
    End Sub 'Page_Load

    Sub Button_Click(sender As Object, e As EventArgs)
        
        Label1.Text = "The DataSetIndex of each item in the DataGrid are: <br>"
        
        Dim item As DataGridItem
        For Each item In  ItemsGrid.Items
            Label1.Text &= "<br>" & item.DataSetIndex.ToString() & " - " & item.Cells(1).Text
        Next item
    End Sub 'Button_Click
 
</script>
 
<body>
 
   <form runat=server>

      <h3>DataGridItem DataSetIndex Example</h3>
 
      <asp:DataGrid id="ItemsGrid" runat="server"
           BorderColor="black"
           BorderWidth="1"
           CellPadding="3"
           ShowFooter="true"
           AutoGenerateColumns="true">

         <HeaderStyle BackColor="#00aaaa">
         </HeaderStyle>

         <FooterStyle BackColor="#00aaaa">
         </FooterStyle>
   
      </asp:DataGrid>
 
      <br>

      <asp:Button id="Button1"
           Text="Display DataSetIndex of Items in DataGrid"
           OnClick="Button_Click"
           runat="server"/>

      <br><br>
 
      <asp:Label id="Label1" 
           runat="server"/>
 
   </form>
 
</body>
</html>


[C#] 
<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
 
<html>
<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);
 
         dt.Rows.Add(dr);
      }
 
      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();
         ItemsGrid.DataBind();
      }
 
   }
 
   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.DataSetIndex.ToString() +
                        " - " + item.Cells[1].Text;
      }
 
   }
 
</script>
 
<body>
 
   <form runat=server>

      <h3>DataGridItem DataSetIndex Example</h3>
 
      <asp:DataGrid id="ItemsGrid" runat="server"
           BorderColor="black"
           BorderWidth="1"
           CellPadding="3"
           ShowFooter="true"
           AutoGenerateColumns="true">

         <HeaderStyle BackColor="#00aaaa">
         </HeaderStyle>

         <FooterStyle BackColor="#00aaaa">
         </FooterStyle>
   
      </asp:DataGrid>
 
      <br>

      <asp:Button id="Button1"
           Text="Display DataSetIndex of Items in DataGrid"
           OnClick="Button_Click"
           runat="server"/>

      <br><br>
 
      <asp:Label id="Label1" 
           runat="server"/>
 
   </form>
 
</body>
</html>

[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family

See Also

DataGridItem Class | DataGridItem Members | System.Web.UI.WebControls Namespace | DataGrid | DataSource | ItemType

Show: