DataKeyArray.GetEnumerator Méthode

Définition

Retourne un énumérateur qui contient tous les objets DataKey de la collection.

public:
 virtual System::Collections::IEnumerator ^ GetEnumerator();
public System.Collections.IEnumerator GetEnumerator ();
abstract member GetEnumerator : unit -> System.Collections.IEnumerator
override this.GetEnumerator : unit -> System.Collections.IEnumerator
Public Function GetEnumerator () As IEnumerator

Retours

Objet implémentant IEnumerator qui contient tous les objets DataKey de la collection.

Implémente

Exemples

L’exemple de code suivant montre comment effectuer une itération au sein de l’énumérateur créé par la GetEnumerator méthode .


<%@ Page language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  void CustomerGridView_DataBound(Object sender, EventArgs e)
  {

    // Use the Count property to determine whether the
    // DataKeys collection contains any items.
    if (CustomerGridView.DataKeys.Count > 0)
    {

      MessageLabel.Text = "The primary key of each record displayed are: <br/><br/>";

      // Use the GetEnumerator method to create an enumerator that 
      // contains the DataKey objects for the GridView control.
      IEnumerator keyEnumerator = CustomerGridView.DataKeys.GetEnumerator();

      // Iterate though the enumerator and display the primary key
      // value of each record displayed.
      while (keyEnumerator.MoveNext())
      {
        DataKey key = (DataKey)keyEnumerator.Current;
        MessageLabel.Text += key.Value.ToString() + "<br/>";
      }

    }
    else
    {
      MessageLabel.Text = "No DataKey objects.";
    }

  }
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >

  <head runat="server">
    <title>DataKeyArray Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>DataKeyArray Example</h3>
                       
        <asp:gridview id="CustomerGridView"
          datasourceid="CustomerDataSource"
          autogeneratecolumns="true"
          datakeynames="CustomerID"  
          allowpaging="true"
          ondatabound="CustomerGridView_DataBound" 
          runat="server">
            
        </asp:gridview>
        
        <br/>
        
        <asp:label id="MessageLabel"
          forecolor="Red"
          runat="server"/>
            
        <!-- This example uses Microsoft SQL Server and connects  -->
        <!-- to the Northwind sample database. Use an ASP.NET     -->
        <!-- expression to retrieve the connection string value   -->
        <!-- from the Web.config file.                            -->
        <asp:sqldatasource id="CustomerDataSource"
          selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
          connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" 
          runat="server"/>
            
      </form>
  </body>
</html>

<%@ Page language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  Sub CustomerGridView_DataBound(ByVal sender As Object, ByVal e As EventArgs) Handles CustomerGridView.DataBound

    ' Use the Count property to determine whether the
    ' DataKeys collection contains any items.
    If CustomerGridView.DataKeys.Count > 0 Then

      MessageLabel.Text = "The primary key of each record displayed are: <br/><br/>"

      ' Use the GetEnumerator method to create an enumerator that 
      ' contains the DataKey objects for the GridView control.
      Dim keyEnumerator As IEnumerator = CustomerGridView.DataKeys.GetEnumerator()

      ' Iterate though the enumerator and display the primary key
      ' value of each record displayed.
      While keyEnumerator.MoveNext()
      
        Dim key As DataKey = CType(keyEnumerator.Current, DataKey)
        MessageLabel.Text &= key.Value.ToString() & "<br/>"
      
      End While

    Else
    
      MessageLabel.Text = "No DataKey objects."
    
    End If

  End Sub
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >

  <head runat="server">
    <title>DataKeyArray Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>DataKeyArray Example</h3>
                       
        <asp:gridview id="CustomerGridView"
          datasourceid="CustomerDataSource"
          autogeneratecolumns="true"
          datakeynames="CustomerID"  
          allowpaging="true"
          runat="server">
            
        </asp:gridview>
        
        <br/>
        
        <asp:label id="MessageLabel"
          forecolor="Red"
          runat="server"/>
            
        <!-- This example uses Microsoft SQL Server and connects  -->
        <!-- to the Northwind sample database. Use an ASP.NET     -->
        <!-- expression to retrieve the connection string value   -->
        <!-- from the Web.config file.                            -->
        <asp:sqldatasource id="CustomerDataSource"
          selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
          connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" 
          runat="server"/>
            
      </form>
  </body>
</html>

Remarques

Utilisez la GetEnumerator méthode pour obtenir un énumérateur qui peut être itéré de manière linéaire pour accéder à chaque élément de la collection. Pour accéder à l’élément à la position actuelle dans l’énumérateur, utilisez la IEnumerator.Current propriété . Utilisez la IEnumerator.MoveNext méthode pour passer à l’élément suivant de la collection. Pour déplacer l’énumérateur vers sa position initiale, utilisez la IEnumerator.Reset méthode .

Remarque

Lorsque vous obtenez initialement l’énumérateur ou utilisez la IEnumerator.Reset méthode pour déplacer l’énumérateur vers le premier élément de la collection, vous devez appeler la IEnumerator.MoveNext méthode . Sinon, l’élément représenté par la IEnumerator.Current propriété n’est pas défini.

S’applique à

Voir aussi