GridViewRowCollection.GetEnumerator GridViewRowCollection.GetEnumerator GridViewRowCollection.GetEnumerator GridViewRowCollection.GetEnumerator Method

定義

GridViewRow 内のすべての GridViewRowCollection オブジェクトを格納している列挙子を返します。Returns an enumerator that contains all GridViewRow objects in the 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

戻り値

IEnumerator のすべての GridViewRow オブジェクトを格納している GridViewRowCollection 実装オブジェクト。An IEnumerator implemented object that contains all GridViewRow objects in the GridViewRowCollection.

実装

次の例では、 GetEnumeratorメソッドを使用して、コレクションの値を含む列挙子を取得する方法を示します。The following example demonstrates how to use the GetEnumerator method to retrieve an enumerator that contains the values from the collection. 列挙子は反復処理され、最初のセルの値がページに表示されます。The enumerator is iterated through and the values of the first cell are displayed on the page.


<%@ 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>

注釈

このメソッドを使用して、 GridViewRowCollection内の各項目にアクセスするために線形に反復処理できる列挙子を取得します。Use this method to get an enumerator that can be iterated through linearly to access each item in the GridViewRowCollection. 列挙子の現在位置にある項目にアクセスするには、 IEnumerator.Currentプロパティを使用します。To access the item at the current position in the enumerator, use the IEnumerator.Current property. IEnumerator.MoveNextメソッドを使用して、コレクション内の次の項目に移動します。Use the IEnumerator.MoveNext method to move to the next item in the collection. 列挙子を初期位置に移動するには、 IEnumerator.Resetメソッドを使用します。To move the enumerator to its initial position, use the IEnumerator.Reset method.

注意

最初に列挙子を取得する場合、 IEnumerator.Resetまたはメソッドを使用して列挙子をコレクション内の最初の項目にIEnumerator.MoveNext移動する場合は、メソッドを呼び出す必要があります。When you initially get the enumerator or use the IEnumerator.Reset method to move the enumerator to the first item in the collection, you must call the IEnumerator.MoveNext method. それ以外の場合、 IEnumerator.Currentプロパティによって表される項目は定義されません。Otherwise, the item that is represented by the IEnumerator.Current property is undefined.

適用対象

こちらもご覧ください