DetailsViewRowCollection 类

定义

表示 DetailsViewRow 控件中 DetailsView 对象的集合。Represents a collection of DetailsViewRow objects in a DetailsView control.

public ref class DetailsViewRowCollection : System::Collections::ICollection
public class DetailsViewRowCollection : System.Collections.ICollection
type DetailsViewRowCollection = class
    interface ICollection
    interface IEnumerable
Public Class DetailsViewRowCollection
Implements ICollection
继承
DetailsViewRowCollection
实现

示例

下面的代码示例演示如何循环访问DetailsViewRow DetailsView控件Rows集合中的对象。The following code example demonstrates how to iterate through the DetailsViewRow objects in the Rows collection of a DetailsView control. 然后, 将在页面上显示每行的值。The value of each row is then 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)
    {
      // Iterate through the Rows collection and display
      // the value of each field.
      MessageLabel.Text = "The row values are: <br/><br/>";

      foreach (DetailsViewRow row in ItemDetailsView.Rows)
      {
        // 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 Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>DetailsViewRowCollection 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
    
      ' Iterate through the Rows collection and display
      ' the value of each field.
      MessageLabel.Text = "The row values are: <br/><br/>"
    
      Dim row As DetailsViewRow
    
      For Each row In ItemDetailsView.Rows
    
        ' 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/>"
    
      Next
    
    Else
      
      MessageLabel.Text = "No items."
    
    End If
    
  End Sub
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>DetailsViewRowCollection Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>DetailsViewRowCollection 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>

注解

类用于存储和管理DetailsView控件中对象的DetailsViewRow集合。 DetailsViewRowCollectionThe DetailsViewRowCollection class is used to store and manage a collection of DetailsViewRow objects in a DetailsView control. DetailsView控件中的每一行都由一个DetailsViewRow对象表示。Each row in a DetailsView control is represented by a DetailsViewRow object. 控件使用类作为其Rows属性。 DetailsViewRowCollection DetailsViewThe DetailsView control uses the DetailsViewRowCollection class for its Rows property.

备注

Rows属性只包含数据行。The Rows property contains only the data rows. 若要访问标题行、表尾行、顶部页导航行和底部页导航行, HeaderRowFooterRow分别TopPagerRow使用、 BottomPagerRow 、和属性。To access the header row, footer row, top pager row, and bottom pager row, use the HeaderRow, FooterRow, TopPagerRow, and BottomPagerRow properties, respectively.

DetailsViewRowCollection类支持多种方法来访问集合中的项:The DetailsViewRowCollection class supports several ways to access the items in the collection:

  • 使用索引器直接从集合中DetailsViewRow检索特定的从零开始的索引处的对象。 Item[Int32]Use the Item[Int32] indexer to directly retrieve a DetailsViewRow object from the collection at a specific zero-based index.

  • GetEnumerator使用方法检索可用于循环访问集合的枚举器。Use the GetEnumerator method to retrieve an enumerator that can be used to iterate through the collection.

  • CopyTo使用方法将集合中的项复制到数组中, 然后可以使用该数组访问集合中的项。Use the CopyTo method to copy the items in the collection into an array, which can then be used to access the items in the collection.

若要确定集合中的总项数, 请使用Count属性。To determine the total number of items in the collection, use the Count property.

构造函数

DetailsViewRowCollection(ArrayList)

使用指定的 ArrayList 对象初始化 DetailsViewRowCollection 类的新实例。Initializes a new instance of the DetailsViewRowCollection class using the specified ArrayList object.

属性

Count

获取 DetailsViewRowCollection 对象中的项数。Gets the number of items in the DetailsViewRowCollection object.

IsReadOnly

获取一个值,该值指示 DetailsViewRowCollection 对象中的行是否可修改。Gets a value indicating whether the rows in the DetailsViewRowCollection object can be modified.

IsSynchronized

获取一个值,该值指示 DetailsViewRowCollection 对象是否是同步的(线程安全)。Gets a value indicating whether the DetailsViewRowCollection object is synchronized (thread safe).

Item[Int32]

从集合中获取指定索引处的 DetailsViewRow 对象。Gets the DetailsViewRow object from the collection at the specified index.

SyncRoot

获取用于同步访问集合的对象。Gets the object used to synchronize access to the collection.

方法

CopyTo(DetailsViewRow[], Int32)

将此 DetailsViewRowCollection 对象中的所有项复制到指定的 Array 对象,从 Array 中的指定索引处开始。Copies all the items from this DetailsViewRowCollection object to the specified Array object, starting at the specified index in the Array.

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetEnumerator()

返回一个枚举数,该枚举数包含 DetailsViewRow 对象中的所有 DetailsViewRowCollection 对象。Returns an enumerator that contains all DetailsViewRow objects in the DetailsViewRowCollection object.

GetHashCode()

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
ToString()

返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)

显式界面实现

ICollection.CopyTo(Array, Int32)

将此 DetailsViewRowCollection 对象中的所有项复制到指定的 Array 对象,从 Array 中的指定索引处开始。Copies all the items from this DetailsViewRowCollection object to the specified Array object, starting at the specified index in the Array.

扩展方法

Cast<TResult>(IEnumerable)

IEnumerable 的元素强制转换为指定的类型。Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable)

根据指定类型筛选 IEnumerable 的元素。Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable)

启用查询的并行化。Enables parallelization of a query.

AsQueryable(IEnumerable)

IEnumerable 转换为 IQueryableConverts an IEnumerable to an IQueryable.

适用于

另请参阅