다음을 통해 공유


DetailsViewRowCollection.GetEnumerator 메서드

정의

DetailsViewRow 개체의 모든 DetailsViewRowCollection 개체를 포함하는 열거자를 반환합니다.

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에 있는 모든 DetailsViewRow 개체를 포함하는 DetailsViewRowCollection 구현 개체입니다.

구현

예제

다음 코드 예제를 사용 하는 방법에 설명 합니다 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 SubmitButton_Click(Object sender, EventArgs e)
  {

    // Use the Count property to determine whether the
    // Rows collection contains any item.
    if (ItemDetailsView.Rows.Count > 0)
    {

      MessageLabel.Text = "The row values are: <br/><br/>";

      // Get the enumerator that contains the data rows in the 
      // DetailsView control.
      IEnumerator rowEnumerator = ItemDetailsView.Rows.GetEnumerator();

      // Iterate though the enumerator and display the field values.
      while (rowEnumerator.MoveNext())
      {
        DetailsViewRow row = (DetailsViewRow)rowEnumerator.Current;

        // Use the Text property to access the value of 
        // each cell. In this example, the cells in the 
        // first column (index 0) contains the field names, 
        // while the cells in the second column (index 1)
        // contains the field value. 
        MessageLabel.Text += row.Cells[0].Text + " = " +
          row.Cells[1].Text + "<br/>";
      }

    }
    else
    {
      MessageLabel.Text = "No items.";
    }

  }
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>DetailsViewRowCollection GetEnumerator Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>DetailsViewRowCollection GetEnumerator Example</h3>
  
      <asp:detailsview id="ItemDetailsView"
        datasourceid="DetailsViewSource"
        allowpaging="true"
        autogeneraterows="false" 
        runat="server">
        <fields>
          <asp:boundfield datafield="CustomerID"
            headertext="Customer ID"/>
          <asp:boundfield datafield="CompanyName"
            headertext="Company Name"/>
          <asp:boundfield datafield="Address"
            headertext="Address"/>
          <asp:boundfield datafield="City"
            headertext="City"/>
          <asp:boundfield datafield="PostalCode"
            headertext="ZIP Code"/>
          <asp:boundfield datafield="Country"
            headertext="Country"/>
        </fields>
      </asp:detailsview>
      
      <br/>
      
      <asp:button id="SubmitButton" 
        text="Display Row Values"
        onclick="SubmitButton_Click"
        runat="server"/>
        
      <br/><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="DetailsViewSource"
        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 SubmitButton_Click(ByVal sender As Object, ByVal e As EventArgs)

    ' Use the Count property to determine whether the
    ' Rows collection contains any item.
    If ItemDetailsView.Rows.Count > 0 Then

      MessageLabel.Text = "The row values are: <br/><br/>"

      ' Get the enumerator that contains the data rows in the 
      ' DetailsView control.
      Dim rowEnumerator As IEnumerator = ItemDetailsView.Rows.GetEnumerator()

      ' Iterate though the enumerator and display the field values.
      While rowEnumerator.MoveNext()
 
        Dim row As DetailsViewRow = CType(rowEnumerator.Current, DetailsViewRow)

        ' Use the Text property to access the value of 
        ' each cell. In this example, the cells in the 
        ' first column (index 0) contains the field names, 
        ' while the cells in the second column (index 1)
        ' contains the field value. 
        MessageLabel.Text &= row.Cells(0).Text & " = " & _
          row.Cells(1).Text & "<br/>"
      
      End While
    
    Else
    
      MessageLabel.Text = "No items."
        
    End If

  End Sub
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>DetailsViewRowCollection GetEnumerator Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>DetailsViewRowCollection GetEnumerator Example</h3>
  
      <asp:detailsview id="ItemDetailsView"
        datasourceid="DetailsViewSource"
        allowpaging="true"
        autogeneraterows="false" 
        runat="server">
        <fields>
          <asp:boundfield datafield="CustomerID"
            headertext="Customer ID"/>
          <asp:boundfield datafield="CompanyName"
            headertext="Company Name"/>
          <asp:boundfield datafield="Address"
            headertext="Address"/>
          <asp:boundfield datafield="City"
            headertext="City"/>
          <asp:boundfield datafield="PostalCode"
            headertext="ZIP Code"/>
          <asp:boundfield datafield="Country"
            headertext="Country"/>
        </fields>
      </asp:detailsview>
      
      <br/>
      
      <asp:button id="SubmitButton" 
        text="Display Row Values"
        onclick="SubmitButton_Click"
        runat="server"/>
        
      <br/><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="DetailsViewSource"
        selectcommand="Select [CustomerID], [CompanyName], [Address], 
          [City], [PostalCode], [Country] From [Customers]"
        connectionstring=
          "<%$ ConnectionStrings:NorthWindConnectionString%>" 
        runat="server"/>  
  
    </form>
  </body>
</html>

설명

이 메서드를 사용 하 여 각 항목에 액세스 하기 위해 지속적으로 반복할 수 있는 열거자를 가져오기는 DetailsViewRowCollection 개체입니다. 열거자의 현재 위치에 있는 항목에 액세스 하려면 사용 된 Current 속성입니다. 사용 된 MoveNext 컬렉션의 다음 항목으로 이동 하는 방법입니다. 열거자를 초기 위치로 이동 하려면 사용 된 Reset 메서드.

참고

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

적용 대상

추가 정보