DataKeyCollection 类

定义

表示包含数据源中每个记录的键字段的集合。 此类不能被继承。

public ref class DataKeyCollection sealed : System::Collections::ICollection
public sealed class DataKeyCollection : System.Collections.ICollection
type DataKeyCollection = class
    interface ICollection
    interface IEnumerable
Public NotInheritable Class DataKeyCollection
Implements ICollection
继承
DataKeyCollection
实现

示例


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

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

<!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 and DataKeyField Example</title>
<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 id="form1" 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>

注解

DataKeyCollection类表示数据源中键字段的集合。 数据源中每条记录的键字段存储在此集合中。 这使你可以使用数据列表控件存储键字段,而无需在控件中显示它。 此集合会自动填充属性指定的 BaseDataList.DataKeyField 字段中的值。 此集合不允许手动添加或删除集合中的项。

键字段通常用于事件的处理程序(如 ItemCommandDeleteCommand),作为更新查询字符串的一部分,以修改数据源中的特定记录。 键字段可帮助更新查询字符串标识要修改的相应记录。

Count使用 属性确定集合中的项数。 若要以编程方式从 DataKeyCollection检索键字段,请使用以下方法之一:

  • 使用索引器通过数组表示法从集合中获取单个键字段。

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

IEnumerable 转换为 IQueryable

适用于

另请参阅