DataKeyArray クラス

定義

DataKey オブジェクトのコレクションを表します。 このクラスは継承できません。

public ref class DataKeyArray sealed : System::Collections::ICollection, System::Web::UI::IStateManager
public sealed class DataKeyArray : System.Collections.ICollection, System.Web.UI.IStateManager
type DataKeyArray = class
    interface ICollection
    interface IEnumerable
    interface IStateManager
Public NotInheritable Class DataKeyArray
Implements ICollection, IStateManager
継承
DataKeyArray
実装

次のコード例では、インデクサーを使用してコレクションからオブジェクトを DataKey 取得する方法を DataKeyArray 示します。


<%@ 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 CustomerGridView_DataBound(Object sender, EventArgs e)
  {           
    // Use the indexer to retrieve the DataKey object for the 
    // first record.
    DataKey key = CustomerGridView.DataKeys[0];

    // Display the value of the primary key for the first
    // record displayed in the GridView control.
    MessageLabel.Text = "The primary key of the first record displayed is " +
      key.Value.ToString() + ".";
  }

  void CopyArray_Click(Object sender, EventArgs e)
  {
      DataKeyArray theKeys = CustomerGridView.DataKeys;
      DataKey[] myNewArray = new DataKey[theKeys.Count];
      theKeys.CopyTo(myNewArray, 0);
      Label2.Visible = true;

      // Display first page key values from the new array.
      for (int i = 0; i < myNewArray.Length; i++)
      {
          Label2.Text += "<br />" + myNewArray[i].Value;
      }

  }

</script>

<html  >

  <head id="Head1" runat="server">
    <title>DataKeyArray Example</title>
</head>
<body>
    <form id="form1" 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"/>

        <asp:Button ID="CopyArray" 
            runat="server" 
            Text="Copy DataKeyArray to Array" 
            OnClick="CopyArray_Click" />

        <br />

        <asp:label id="Label2"
          runat="server"
          Visible="false"  
          Text="First page of Copied Array Key Values" />


      </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 CustomerGridView_DataBound(ByVal sender As Object, ByVal e As EventArgs) Handles CustomerGridView.DataBound
          
    ' Use the indexer to retrieve the DataKey object for the 
    ' first record.
    Dim key As DataKey = CustomerGridView.DataKeys(0)

    ' Display the value of the primary key for the first
    ' record displayed in the GridView control.
    MessageLabel.Text = "The primary key of the first record displayed is " & _
      key.Value.ToString() & "."
  
    End Sub
    
    Sub CopyArray_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim theKeys As DataKeyArray = CustomerGridView.DataKeys
        Dim myNewArray(theKeys.Count - 1) As DataKey
        theKeys.CopyTo(myNewArray, 0)
        Label2.Visible = True

        ' Display first page key values from the new array.  
        For i As Integer = 0 To myNewArray.Length - 1
            Label2.Text &= "<br />" & myNewArray(i).Value
        Next i

    End Sub
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >

  <head id="Head1" runat="server">
    <title>DataKeyArray Example</title>
</head>
<body>
    <form id="form1" 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"/>

        <asp:Button ID="CopyArray" 
            runat="server" 
            Text="Copy DataKeyArray to Array" 
            OnClick="CopyArray_Click" />

        <br />

        <asp:label id="Label2"
          runat="server"
          Visible="false"  
          Text="First page of Copied Array Key Values" />
            
      </form>
  </body>
</html>

次のコード例は、コレクションを反復処理する方法を DataKeyArray 示しています。


<%@ 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 CustomerGridView_DataBound(Object sender, EventArgs e)
  {    
    // Display the value of the primary key for each
    // record in the GridView control.
    MessageLabel.Text = "The primary key of each record displayed are: <br/><br/>";

    foreach (DataKey key in CustomerGridView.DataKeys)
    {
       MessageLabel.Text += key.Value.ToString() + "<br/>";
    }
  }
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >

  <head runat="server">
    <title>DataKeyArray Example</title>
</head>
<body>
    <form id="form1" 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>

<%@ 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 CustomerGridView_DataBound(ByVal sender As Object, ByVal e As EventArgs) Handles CustomerGridView.DataBound
    
    ' Display the value of the primary key for each
    ' record in the GridView control.
    MessageLabel.Text = "The primary key of each record displayed are: <br/><br/>"

    Dim key As DataKey
    For Each key In CustomerGridView.DataKeys
    
      MessageLabel.Text += key.Value.ToString() + "<br/>"
      
    Next
    
  End Sub
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >

  <head runat="server">
    <title>DataKeyArray Example</title>
</head>
<body>
    <form id="form1" 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>

注釈

クラスは DataKeyArray 、オブジェクトの DataKey コレクションを格納および管理するために使用されます。 オブジェクトは DataKey 、データ バインド コントロール内のレコードの主キーを表します。 一般に、複数のレコードを表示するデータ バインド コントロール (コントロールなど GridView ) では、 オブジェクトを DataKeyArray 使用して、コントロールに表示されるレコードのオブジェクトを格納 DataKey します。

クラスでは DataKeyArray 、コレクション内の項目にアクセスするいくつかの方法がサポートされています。

  • インデクサーを Item[] 使用して、特定の DataKey 0 から始まるインデックスのコレクションからオブジェクトを直接取得します。

  • メソッドを GetEnumerator 使用して、コレクションの反復処理に使用できる列挙子を取得します。

  • コレクション内の CopyTo 項目を配列にコピーするには、 メソッドを使用します。このメソッドを使用すると、コレクション内の項目にアクセスできます。

コレクション内の項目の合計数を確認するには、 プロパティを Count 使用します。

コンストラクター

DataKeyArray(ArrayList)

DataKeyArray クラスの新しいインスタンスを初期化します。

プロパティ

Count

コレクション内の項目の数を取得します。

IsReadOnly

コレクション内の項目を変更できるかどうかを示す値を取得します。

IsSynchronized

DataKeyArray コレクションが同期されている (スレッド セーフである) かどうかを示す値を取得します。

Item[Int32]

指定したインデックスにある DataKey オブジェクトをコレクションから取得します。

SyncRoot

コレクションへのアクセスを同期するために使用するオブジェクトを取得します。

メソッド

CopyTo(DataKey[], Int32)

指定した DataKey オブジェクトの配列にこのコレクションのすべての項目をコピーします。コピー操作は、配列内の指定したインデックス位置から開始されます。

Equals(Object)

指定されたオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
GetEnumerator()

コレクション内のすべての DataKey オブジェクトを格納している列挙子を返します。

GetHashCode()

既定のハッシュ関数として機能します。

(継承元 Object)
GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
MemberwiseClone()

現在の Object の簡易コピーを作成します。

(継承元 Object)
ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)

明示的なインターフェイスの実装

ICollection.CopyTo(Array, Int32)

指定した Array にこのコレクションのすべての項目をコピーします。コピー操作は、Array 内の指定したインデックス位置から開始されます。

IStateManager.IsTrackingViewState

DataKeyArray オブジェクトがビューステートの変更を追跡しているかどうかを示す値を取得します。

IStateManager.LoadViewState(Object)

以前に保存した DataKeyArray オブジェクトのビューステートを読み込みます。

IStateManager.SaveViewState()

DataKeyArray オブジェクトの現在のビューステートを保存します。

IStateManager.TrackViewState()

DataKeyArray オブジェクトに対するビューステートの変更の追跡と保存を始める開始点にマークを付けます。

拡張メソッド

Cast<TResult>(IEnumerable)

IEnumerable の要素を、指定した型にキャストします。

OfType<TResult>(IEnumerable)

指定された型に基づいて IEnumerable の要素をフィルター処理します。

AsParallel(IEnumerable)

クエリの並列化を有効にします。

AsQueryable(IEnumerable)

IEnumerableIQueryable に変換します。

適用対象

こちらもご覧ください