DataKeyCollectionとは? わかりやすく解説

DataKeyCollection クラス

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

名前空間: System.Web.UI.WebControls
アセンブリ: System.Web (system.web.dll 内)
構文構文

Public NotInheritable Class
 DataKeyCollection
    Implements ICollection, IEnumerable
Dim instance As DataKeyCollection
public sealed class DataKeyCollection : ICollection,
 IEnumerable
public ref class DataKeyCollection sealed :
 ICollection, IEnumerable
public final class DataKeyCollection implements
 ICollection, IEnumerable
public final class DataKeyCollection implements
 ICollection, IEnumerable
解説解説

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

通常キー フィールドは、データ ソース内の特定のレコード改訂する更新クエリ文字列一部として、ItemCommandDeleteCommand などのイベントハンドラ内で使用されます。キー フィールドは、更新クエリ文字列変更対象適切なレコード識別できるようにします。

コレクション内の項目の数を確認するには、Count プロパティ使用しますDataKeyCollection からキー フィールドプログラムによって取得するには、次のメソッドいずれか使用します

使用例使用例
<%@ Page Language="VB" AutoEventWireup="True"
 %>
<%@ Import Namespace="System.Data"
 %>

<html>

<head>

   <script runat="server">

      Function CreateDataSource() As ICollection
 
      
         ' Create sample data for the DataGrid control.
         Dim dt As DataTable = New
 DataTable()
         Dim dr As DataRow
 
         ' Define the columns of the table.
         dt.Columns.Add(new DataColumn("IntegerValue",
 GetType(Integer)))
         dt.Columns.Add(new DataColumn("StringValue",
 GetType(String)))
         dt.Columns.Add(new DataColumn("CurrencyValue",
 GetType(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.
         Dim keys(1) As DataColumn
         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.
         Dim i As Integer

         For i = 0 To 8 
     
            dr = dt.NewRow()
 
            dr(0) = i
            dr(1) = "Item " & i.ToString()
            dr(2) = 1.23 * (i + 1)
 
            dt.Rows.Add(dr)

         Next

         ' To persist the data source between posts to the server, 
         ' store it in session state.  
         Session("Source") = dt
 
         Dim dv As DataView = New
 DataView(dt)
         Return dv

      End Function
 
      Sub Page_Load(sender As Object,
 e As EventArgs) 
 
         ' Load sample data only once, when the page is first loaded.
         If Not IsPostBack Then
 
        
            ItemsGrid.DataSource = CreateDataSource()
            ItemsGrid.DataBind()
         
         End If

      End Sub

      Sub Delete_Command(sender As Object,
 e As DataGridCommandEventArgs)

         ' Retrieve the data table from session state.
         Dim dt As DataTable = CType(Session("Source"),
 DataTable)

         ' 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.
 
         Dim row As DataRow
         row = dt.Rows.Find(ItemsGrid.DataKeys(e.Item.ItemIndex))

         ' Delete the item selected in the DataGrid from the data source.
         If Not row is Nothing
 Then
         
            dt.Rows.Remove(row)
         
         End If

         ' Save the data source.
         Session("Source") = dt

         ' Create a DataView and bind it to the DataGrid control.
         Dim dv As DataView = New
 DataView(dt)
         ItemsGrid.DataSource = dv
         ItemsGrid.DataBind()

      End Sub

   </script>

</head>

<body>

   <form runat="server">

      <h3>BaseDataList DataKeys and DataKeyField 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>

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

<html>

<head>

   <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 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>

.NET Framework のセキュリティ.NET Frameworkセキュリティ
継承階層継承階層
System.Object
  System.Web.UI.WebControls.DataKeyCollection
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
DataKeyCollection メンバ
System.Web.UI.WebControls 名前空間
BaseDataList クラス
BaseDataList.DataKeyField プロパティ
Count
CopyTo
System.Array
GetEnumerator
System.Collections.IEnumerator
その他の技術情報
DataList Web サーバー コントロール

DataKeyCollection コンストラクタ

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

名前空間: System.Web.UI.WebControls
アセンブリ: System.Web (system.web.dll 内)
構文構文

Public Sub New ( _
    keys As ArrayList _
)
Dim keys As ArrayList

Dim instance As New DataKeyCollection(keys)
public DataKeyCollection (
    ArrayList keys
)
public:
DataKeyCollection (
    ArrayList^ keys
)
public DataKeyCollection (
    ArrayList keys
)
public function DataKeyCollection (
    keys : ArrayList
)

パラメータ

keys

データ ソースキー フィールド格納している System.Collections.ArrayList。

解説解説
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
DataKeyCollection クラス
DataKeyCollection メンバ
System.Web.UI.WebControls 名前空間
その他の技術情報
DataList Web サーバー コントロール

DataKeyCollection プロパティ


パブリック プロパティパブリック プロパティ

  名前 説明
パブリック プロパティ Count コレクション内の項目の数を取得します
パブリック プロパティ IsReadOnly DataKeyCollection 内の項目を変更できるかどうかを示す値を取得します
パブリック プロパティ IsSynchronized DataKeyCollection同期されている (スレッド セーフである) かどうかを示す値を取得します
パブリック プロパティ Item コレクション内の指定したインデックス位置にあるキー フィールド取得します
パブリック プロパティ SyncRoot DataKeyCollection へのアクセス同期するために使用するオブジェクト取得します
参照参照

関連項目

DataKeyCollection クラス
System.Web.UI.WebControls 名前空間
BaseDataList クラス
BaseDataList.DataKeyField プロパティ
Count
CopyTo
System.Array
GetEnumerator
System.Collections.IEnumerator

その他の技術情報

DataList Web サーバー コントロール

DataKeyCollection メソッド


パブリック メソッドパブリック メソッド

プロテクト メソッドプロテクト メソッド
参照参照

関連項目

DataKeyCollection クラス
System.Web.UI.WebControls 名前空間
BaseDataList クラス
BaseDataList.DataKeyField プロパティ
Count
CopyTo
System.Array
GetEnumerator
System.Collections.IEnumerator

その他の技術情報

DataList Web サーバー コントロール

DataKeyCollection メンバ

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

DataKeyCollection データ型公開されるメンバを以下の表に示します


パブリック コンストラクタパブリック コンストラクタ
  名前 説明
パブリック メソッド DataKeyCollection DataKeyCollection クラス新しインスタンス初期化します。
パブリック プロパティパブリック プロパティ
  名前 説明
パブリック プロパティ Count コレクション内の項目の数を取得します
パブリック プロパティ IsReadOnly DataKeyCollection 内の項目を変更できるかどうかを示す値を取得します
パブリック プロパティ IsSynchronized DataKeyCollection同期されている (スレッド セーフである) かどうかを示す値を取得します
パブリック プロパティ Item コレクション内の指定したインデックス位置にあるキー フィールド取得します
パブリック プロパティ SyncRoot DataKeyCollection へのアクセス同期するために使用するオブジェクト取得します
パブリック メソッドパブリック メソッド
プロテクト メソッドプロテクト メソッド
参照参照

関連項目

DataKeyCollection クラス
System.Web.UI.WebControls 名前空間
BaseDataList クラス
BaseDataList.DataKeyField プロパティ
Count
CopyTo
System.Array
GetEnumerator
System.Collections.IEnumerator

その他の技術情報

DataList Web サーバー コントロール


このページでは「.NET Framework クラス ライブラリ リファレンス」からDataKeyCollectionを検索した結果を表示しています。
Weblioに収録されているすべての辞書からDataKeyCollectionを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からDataKeyCollection を検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「DataKeyCollection」の関連用語

DataKeyCollectionのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



DataKeyCollectionのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS