Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

DetailsViewRowCollection.GetEnumerator Method

Returns an enumerator that contains all DetailsViewRow objects in the DetailsViewRowCollection object.

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)

public IEnumerator GetEnumerator()

Return Value

Type: System.Collections.IEnumerator
An IEnumerator-implemented object that contains all DetailsViewRow objects in the DetailsViewRowCollection.

Implements

IEnumerable.GetEnumerator()

Use this method to get an enumerator that can be iterated through linearly to access each item in the DetailsViewRowCollection object. To access the item at the current position in the enumerator, use the Current property. Use the MoveNext method to move to the next item in the collection. To move the enumerator to its initial position, use the Reset method.

NoteNote:

When you initially get the enumerator or use the Reset method to move the enumerator to the first item in the collection, you must call the MoveNext method. Otherwise, the item represented by the Current property is undefined.

The following code example demonstrates how to use the GetEnumerator method to retrieve an enumerator that contains the items from the collection. The enumerator is then iterated through and the value of each row is 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 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>

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0

Community Additions

ADD
Show:
© 2015 Microsoft