英語で読む

次の方法で共有


DataKeyCollection クラス

定義

データ ソースの各レコードのキー フィールドを格納するコレクションを表します。 このクラスは継承できません。

C#
public sealed class DataKeyCollection : System.Collections.ICollection
継承
DataKeyCollection
実装

ASP.NET (C#)

<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">
    <title>BaseDataList DataKeys Example</title>
<script runat="server">

      ICollection CreateDataSource() 
      {
      
         // Create sample data for the DataGrid control.
         DataTable dt = new DataTable();
         DataRow dr;
 
         // Define the columns of the table.
         dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
         dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
         dt.Columns.Add(new DataColumn("CurrencyValue", typeof(double)));

         // Define the primary key for the table as the IntegerValue 
         // column (column 0). To do this, first create an array of 
         // DataColumns to represent the primary key. The primary key can
         // consist of multiple columns, but in this example, only
         // one column is used.
         DataColumn[] keys = new DataColumn[1];
         keys[0] = dt.Columns[0];

         // Then assign the array to the PrimaryKey property of the DataTable. 
         dt.PrimaryKey = keys;
 
         // Populate the table with sample values.
         for (int i = 0; i < 9; i++) 
         {
            dr = dt.NewRow();
 
            dr[0] = i;
            dr[1] = "Item " + i.ToString();
            dr[2] = 1.23 * (i + 1);
 
            dt.Rows.Add(dr);
         }

         // To persist the data source between posts to the server, 
         // store it in session state.  
         Session["Source"] = dt;
 
         DataView dv = new DataView(dt);
         return dv;

      }
 
      void Page_Load(Object sender, EventArgs e) 
      {
 
         // Load sample data only once, when the page is first loaded.
         if (!IsPostBack) 
         {
            ItemsGrid.DataSource = CreateDataSource();
            ItemsGrid.DataBind();
         }

      }

      void Delete_Command(Object sender, DataGridCommandEventArgs e)
      {

         // Retrieve the data table from session state.
         DataTable dt = (DataTable)Session["Source"];

         // Retrieve the data row to delete from the data table. 
         // Use the DataKeys property of the DataGrid control to get 
         // the primary key value of the selected row. 
         // Search the Rows collection of the data table for this value. 
         DataRow row;
         row = dt.Rows.Find(ItemsGrid.DataKeys[e.Item.ItemIndex]);

         // Delete the item selected in the DataGrid from the data source.
         if(row != null)
         {
            dt.Rows.Remove(row);
         }

         // Save the data source.
         Session["Source"] = dt;

         // Create a DataView and bind it to the DataGrid control.
         DataView dv = new DataView(dt);
         ItemsGrid.DataSource = dv;
         ItemsGrid.DataBind();

      }

   </script>

</head>

<body>

   <form id="form1" runat="server">

      <h3>BaseDataList DataKeys Example</h3>

      <asp:DataGrid id="ItemsGrid" 
           BorderColor="Black"
           ShowFooter="False" 
           CellPadding="3" 
           CellSpacing="0"
           HeaderStyle-BackColor="#aaaadd"
           DataKeyField="IntegerValue"
           OnDeleteCommand="Delete_Command"
           runat="server">

         <Columns>

            <asp:ButtonColumn Text="Delete"
                 CommandName="Delete"/>

         </Columns>

      </asp:DataGrid>

   </form>

</body>
</html>

注釈

クラスは DataKeyCollection 、データ ソース内のキー フィールドのコレクションを表します。 データ ソース内の各レコードのキー フィールドは、このコレクションに格納されます。 これにより、キー フィールドをコントロールに表示せずに、データ 一覧コントロールと共に格納できます。 このコレクションには、 プロパティで指定されたフィールドの値が自動的に BaseDataList.DataKeyField 入力されます。 このコレクションでは、コレクションの項目を手動で追加または削除することはできません。

キー フィールドは、データ ソース内の特定のレコードを変更するための更新クエリ文字列の一部として、 や DeleteCommandなどのItemCommandイベントのハンドラーで一般的に使用されます。 キー フィールドは、更新クエリ文字列が変更する適切なレコードを識別するのに役立ちます。

コレクション内の Count 項目の数を決定するには、 プロパティを使用します。 からキー フィールドをプログラムで取得するには、 DataKeyCollection次のいずれかの方法を使用します。

  • インデクサーを使用して、配列表記を使用してコレクションから 1 つのキー フィールドを取得します。

  • コレクションの CopyTo 内容を オブジェクトにコピーするには、 メソッドを System.Array 使用します。このオブジェクトを使用して、コレクションから項目を取得できます。

  • メソッドを GetEnumerator 使用して実装されたオブジェクトを System.Collections.IEnumerator 作成し、それを使用してコレクションから項目を取得できます。

コンストラクター

DataKeyCollection(ArrayList)

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

プロパティ

Count

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

IsReadOnly

DataKeyCollection 内の項目を変更できるかどうかを示す値を取得します。

IsSynchronized

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

Item[Int32]

コレクション内の指定したインデックス位置にあるキー フィールドを取得します。

SyncRoot

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

メソッド

CopyTo(Array, Int32)

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

Equals(Object)

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

(継承元 Object)
GetEnumerator()

IEnumerator 内のすべてのキー フィールドを格納している DataKeyCollection 実装オブジェクトを作成します。

GetHashCode()

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

(継承元 Object)
GetType()

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

(継承元 Object)
MemberwiseClone()

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

(継承元 Object)
ToString()

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

(継承元 Object)

拡張メソッド

Cast<TResult>(IEnumerable)

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

OfType<TResult>(IEnumerable)

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

AsParallel(IEnumerable)

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

AsQueryable(IEnumerable)

IEnumerableIQueryable に変換します。

適用対象

製品 バージョン
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

こちらもご覧ください