GridViewRowCollection.GetEnumerator Metoda

Definicja

Zwraca moduł wyliczający zawierający wszystkie GridViewRow obiekty w obiekcie GridViewRowCollection.

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

Zwraca

IEnumerator

IEnumerator Zaimplementowany obiekt, który zawiera wszystkie GridViewRow obiekty w obiekcie GridViewRowCollection.

Implementuje

Przykłady

W poniższym przykładzie pokazano, jak za pomocą GetEnumerator metody pobrać moduł wyliczający zawierający wartości z kolekcji. Moduł wyliczający jest iteratora, a wartości pierwszej komórki są wyświetlane na stronie.


<%@ 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 AuthorsGridView_RowCreated(Object sender, GridViewRowEventArgs e)
  {
    if (e.Row.RowType == DataControlRowType.Footer)
    {
      Message.Text = "The authors are:<br />";
      
      // Get the enumerator that contains the data rows in the 
      // GridView control.
      IEnumerator rowEnumerator = AuthorsGridView.Rows.GetEnumerator();

      // Iterate though the enumerator and display the value in the
      // first cell of the row.
      while(rowEnumerator.MoveNext())
      {
        GridViewRow row = (GridViewRow)rowEnumerator.Current;
        Message.Text += row.Cells[0].Text + "<br />";
      }
    }
  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>GridViewRowCollection GetEnumerator Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>GridViewRowCollection GetEnumerator Example</h3>

      <table>
        <tr>
          <td>
            <asp:gridview id="AuthorsGridView" 
              datasourceid="AuthorsSqlDataSource" 
              autogeneratecolumns="false"
              onrowcreated="AuthorsGridView_RowCreated"  
              runat="server"> 
                     
              <columns>
                <asp:boundfield datafield="au_lname"
                  headertext="Last Name"/>
                <asp:boundfield datafield="au_fname"
                  headertext="First Name"/>
              </columns>
                                    
            </asp:gridview>
          </td>
          <td>
            <asp:label id="Message" 
              forecolor="Red"
              runat="server"/>
          </td>
        </tr>
      </table>
            
      <!-- This example uses Microsoft SQL Server and connects -->
      <!-- to the Pubs sample database.                        -->
      <asp:sqldatasource id="AuthorsSqlDataSource"  
        selectcommand="SELECT [au_lname], [au_fname] FROM [authors] WHERE [state]='CA'"
        connectionstring="server=localhost;database=pubs;integrated security=SSPI"
        runat="server">
      </asp:sqldatasource>
          
    </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 AuthorsGridView_RowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs)

    If e.Row.RowType = DataControlRowType.Footer Then
    
      Message.Text = "The authors are:<br />"
      
      ' Get the enumerator that contains the data rows in the 
      ' GridView control.
      Dim rowEnumerator As IEnumerator = AuthorsGridView.Rows.GetEnumerator()

      ' Iterate though the enumerator and display the value in the
      ' first cell of the row.
      While rowEnumerator.MoveNext()

        Dim row As GridViewRow = CType(rowEnumerator.Current, GridViewRow)
        Message.Text &= row.Cells(0).Text & "<br />"
      
      End While
      
    End If
  
  End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>GridViewRowCollection GetEnumerator Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>GridViewRowCollection GetEnumerator Example</h3>

      <table>
        <tr>
          <td>
            <asp:gridview id="AuthorsGridView" 
              datasourceid="AuthorsSqlDataSource" 
              autogeneratecolumns="false"
              onrowcreated="AuthorsGridView_RowCreated"  
              runat="server"> 
                     
              <columns>
                <asp:boundfield datafield="au_lname"
                  headertext="Last Name"/>
                <asp:boundfield datafield="au_fname"
                  headertext="First Name"/>
              </columns>
                                    
            </asp:gridview>
          </td>
          <td>
            <asp:label id="Message" 
              forecolor="Red"
              runat="server"/>
          </td>
        </tr>
      </table>
            
      <!-- This example uses Microsoft SQL Server and connects -->
      <!-- to the Pubs sample database.                        -->
      <asp:sqldatasource id="AuthorsSqlDataSource"  
        selectcommand="SELECT [au_lname], [au_fname] FROM [authors] WHERE [state]='CA'"
        connectionstring="server=localhost;database=pubs;integrated security=SSPI"
        runat="server">
      </asp:sqldatasource>
          
    </form>
  </body>
</html>

Uwagi

Ta metoda umożliwia pobranie modułu wyliczającego, który można iterować liniowo w celu uzyskania dostępu do każdego elementu w elemencie GridViewRowCollection. Aby uzyskać dostęp do elementu w bieżącym położeniu modułu wyliczającego, użyj IEnumerator.Current właściwości . IEnumerator.MoveNext Użyj metody , aby przejść do następnego elementu w kolekcji. Aby przenieść moduł wyliczający do początkowej IEnumerator.Reset pozycji, użyj metody .

Uwaga

Po początkowym pobraniu modułu wyliczającego lub użyciu IEnumerator.Reset metody w celu przeniesienia modułu wyliczającego do pierwszego elementu w kolekcji należy wywołać metodę IEnumerator.MoveNext . W przeciwnym razie element reprezentowany przez IEnumerator.Current właściwość jest niezdefiniowany.

Dotyczy

Zobacz też