下面的示例演示如何使用 GetEnumerator 方法检索一个枚举数,该枚举数包含集合中的值。然后,循环访问该枚举数,并将第一个单元格的值显示在页上。
view plaincopy to clipboardprint ?
<% @ Page language = " C# " %>
< 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 >
< FORM runat = " server " >
< H3 > GridViewRowCollection GetEnumerator Example </ H3 >
< TABLE >< TBODY >< TR >< TD >< ASP:GRIDVIEW id = AuthorsGridView runat = " server " onrowcreated = " AuthorsGridView_RowCreated " autogeneratecolumns = " false " datasourceid = " AuthorsSqlDataSource " >< COLUMNS >< ASP:BOUNDFIELD headertext = " Last Name " datafield = " au_lname " />< ASP:BOUNDFIELD headertext = " First Name " datafield = " au_fname " /></ COLUMNS ></ ASP:GRIDVIEW ></ TD >< TD >< ASP:LABEL id = Message runat = " server " forecolor = " Red " /></ TD ></ TR ></ TBODY ></ TABLE >
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the Pubs sample database. -->
< ASP:SQLDATASOURCE id = AuthorsSqlDataSource runat = " server " connectionstring = " server=localhost;database=pubs;integrated security=SSPI " selectcommand = " SELECT [au_lname], [au_fname] FROM [authors] WHERE [state]='CA' " >
</ ASP:SQLDATASOURCE >
</ FORM >
view plaincopy to clipboardprint ?
<% @ Page language = " C# " %>
< 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 >
< FORM runat = " server " >
< H3 > GridViewRowCollection GetEnumerator Example </ H3 >
< TABLE >< TBODY >< TR >< TD >< ASP:GRIDVIEW id = AuthorsGridView runat = " server " onrowcreated = " AuthorsGridView_RowCreated " autogeneratecolumns = " false " datasourceid = " AuthorsSqlDataSource " >< COLUMNS >< ASP:BOUNDFIELD headertext = " Last Name " datafield = " au_lname " />< ASP:BOUNDFIELD headertext = " First Name " datafield = " au_fname " /></ COLUMNS ></ ASP:GRIDVIEW ></ TD >< TD >< ASP:LABEL id = Message runat = " server " forecolor = " Red " /></ TD ></ TR ></ TBODY ></ TABLE >
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the Pubs sample database. -->
< ASP:SQLDATASOURCE id = AuthorsSqlDataSource runat = " server " connectionstring = " server=localhost;database=pubs;integrated security=SSPI " selectcommand = " SELECT [au_lname], [au_fname] FROM [authors] WHERE [state]='CA' " >
</ ASP:SQLDATASOURCE >
</ FORM >