GridViewRowCollection.GetEnumerator 메서드

정의

GridViewRow의 모든 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

IEnumerator의 모든 GridViewRow 개체를 포함하는 GridViewRowCollection 구현 개체입니다.

구현

예제

다음 예제에서는 사용 하는 방법에 설명 합니다 GetEnumerator 컬렉션에서 값을 포함 하는 열거자를 검색 하는 방법입니다. 열거자를 반복 하 고 페이지의 첫 번째 셀의 값이 표시 됩니다.


<%@ 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합니다. 열거자의 현재 위치에 있는 항목에 액세스 하려면 사용 된 IEnumerator.Current 속성입니다. 사용 된 IEnumerator.MoveNext 컬렉션의 다음 항목으로 이동 하는 방법입니다. 열거자를 초기 위치로 이동 하려면 사용 된 IEnumerator.Reset 메서드.

참고

처음에 열거자를 가져옵니다. 하거나 때 사용 합니다 IEnumerator.Reset 열거자는 컬렉션의 첫 번째 항목으로 이동 하는 방법 호출 해야 합니다는 IEnumerator.MoveNext 메서드. 이 고, 그렇지 나타내는 항목을 IEnumerator.Current 속성 정의 되지 않습니다.

적용 대상

추가 정보