GridViewRowCollection.GetEnumerator 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
返回一个枚举数,该枚举数包含 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 的对象,该对象包含 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 项未定义。