DataKeyArray.GetEnumerator メソッド
アセンブリ: System.Web (system.web.dll 内)

Dim instance As DataKeyArray Dim returnValue As IEnumerator returnValue = instance.GetEnumerator
public IEnumerator GetEnumerator ()
public: virtual IEnumerator^ GetEnumerator () sealed
public final IEnumerator GetEnumerator ()
public final function GetEnumerator () : IEnumerator
コレクション内のすべての DataKey オブジェクトを格納している System.Collections.IEnumerator 実装オブジェクト。

GetEnumerator メソッドを使用して、コレクション内の各項目に直接アクセスするために反復処理できる列挙子を取得します。列挙子の現在の位置の項目にアクセスするには、IEnumerator.Current プロパティを使用します。IEnumerator.MoveNext メソッドを使用して、コレクション内の次の項目に移動します。列挙子を最初の位置まで移動するには、IEnumerator.Reset メソッドを使用します。
![]() |
---|
最初に列挙子を取得した場合、または IEnumerator.Reset メソッドを使用して列挙子をコレクションの最初の項目に移動した場合は、IEnumerator.MoveNext メソッドを呼び出す必要があります。このメソッドを呼び出さないと、IEnumerator.Current プロパティで表される項目は未定義になります。 |

GetEnumerator メソッドで作成された列挙子を反復処理する方法を次のコード例に示します。
<%@ Page language="VB" %> <script runat="server"> Sub CustomerGridView_DataBound(ByVal sender As Object, ByVal e As EventArgs) Handles CustomerGridView.DataBound ' Use the Count property to determine whether the ' DataKeys collection contains any items. If CustomerGridView.DataKeys.Count > 0 Then MessageLabel.Text = "The primary key of each record displayed are: <br/><br/>" ' Use the GetEnumerator method to create an enumerator that ' contains the DataKey objects for the GridView control. Dim keyEnumerator As IEnumerator = CustomerGridView.DataKeys.GetEnumerator() ' Iterate though the enumerator and display the primary key ' value of each record displayed. While keyEnumerator.MoveNext() Dim key As DataKey = CType(keyEnumerator.Current, DataKey) MessageLabel.Text &= key.Value.ToString() & "<br/>" End While Else MessageLabel.Text = "No DataKey objects." End If End Sub </script> <html> <body> <form runat="server"> <h3>DataKeyArray Example</h3> <asp:gridview id="CustomerGridView" datasourceid="CustomerDataSource" autogeneratecolumns="true" datakeynames="CustomerID" allowpaging="true" runat="server"> </asp:gridview> <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="CustomerDataSource" selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]" connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" runat="server"/> </form> </body> </html>
<%@ Page language="C#" %> <script runat="server"> void CustomerGridView_DataBound(Object sender, EventArgs e) { // Use the Count property to determine whether the // DataKeys collection contains any items. if (CustomerGridView.DataKeys.Count > 0) { MessageLabel.Text = "The primary key of each record displayed are: <br/><br/>"; // Use the GetEnumerator method to create an enumerator that // contains the DataKey objects for the GridView control. IEnumerator keyEnumerator = CustomerGridView.DataKeys.GetEnumerator(); // Iterate though the enumerator and display the primary key // value of each record displayed. while (keyEnumerator.MoveNext()) { DataKey key = (DataKey)keyEnumerator.Current; MessageLabel.Text += key.Value.ToString() + "<br/>"; } } else { MessageLabel.Text = "No DataKey objects."; } } </script> <html> <body> <form runat="server"> <h3>DataKeyArray Example</h3> <asp:gridview id="CustomerGridView" datasourceid="CustomerDataSource" autogeneratecolumns="true" datakeynames="CustomerID" allowpaging="true" ondatabound="CustomerGridView_DataBound" runat="server"> </asp:gridview> <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="CustomerDataSource" selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]" connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" runat="server"/> </form> </body> </html>

Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- DataKeyArray.GetEnumerator メソッドのページへのリンク