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 的專案未定義。