GridViewRowCollection 类

定义

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

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

示例

下面的示例演示如何循环访问控件的 Rows 集合并在 GridView 页上显示列的值。The following example demonstrates how to iterate through the Rows collection of a GridView control and display the values of a column 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)
    {      
      
      // Get the number of items in the Rows collection.
      int count = AuthorsGridView.Rows.Count;

      // If the GridView control contains any records, display 
      // the last name of each author in the GridView control.
      if (count > 0)
      {      
        Message.Text = "The authors are:<br />";
        
        foreach (GridViewRow row in AuthorsGridView.Rows)
        {
          Message.Text += row.Cells[0].Text + "<br />";
        }
      }
      
    }
  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>GridViewRowCollection Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>GridViewRowCollection 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
      
      ' Get the number of items in the Rows collection.
      Dim count As Integer = AuthorsGridView.Rows.Count

      ' If the GridView control contains any records, display 
      ' the last name of each author in the GridView control.
      If count > 0 Then
           
        Message.Text = "The authors are:<br />"
        
        Dim row As GridViewRow
        For Each row In AuthorsGridView.Rows
        
          Message.Text &= row.Cells(0).Text & "<br />"
        
        Next
        
      End If
        
    End If
        
  End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>GridViewRowCollection Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>GridViewRowCollection 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类用于存储和管理 GridViewRow 控件中对象的集合 GridViewThe GridViewRowCollection class is used to store and manage a collection of GridViewRow objects in a GridView control. 控件中的每一行 GridView 都由一个 GridViewRow 对象表示。Each row in a GridView control is represented by a GridViewRow object. GridView控件将其所有数据行存储在集合中 RowsThe GridView control stores all of its data rows in the Rows collection.

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

  • 使用 Item[] 索引器可以直接检索 GridViewRow 特定的从零开始的索引处的对象。Use the Item[] indexer to directly retrieve a GridViewRow object 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 方法将集合中的项复制到 System.Array 对象中,然后可以使用该对象访问集合中的项。Use the CopyTo method to copy the items in the collection into an System.Array object, 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

构造函数

GridViewRowCollection(ArrayList)

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

属性

Count

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

IsReadOnly

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

IsSynchronized

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

Item[Int32]

获取位于指定索引位置的 GridViewRow 对象。Gets the GridViewRow object at the specified index.

SyncRoot

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

方法

CopyTo(GridViewRow[], Int32)

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

Equals(Object)

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

(继承自 Object)
GetEnumerator()

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

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()

返回表示当前对象的字符串。Returns a string that represents the current object.

(继承自 Object)

显式接口实现

ICollection.CopyTo(Array, Int32)

有关此成员的说明,请参见 CopyTo(Array, Int32)For a description of this member, see CopyTo(Array, Int32).

扩展方法

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.

适用于

另请参阅