DataKeyCollection Kelas
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Mewakili koleksi yang berisi bidang kunci dari setiap rekaman dalam sumber data. Kelas ini tidak dapat diwariskan.
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
- Warisan
-
DataKeyCollection
- Penerapan
Contoh
<%@ 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>
Keterangan
Kelas DataKeyCollection mewakili kumpulan bidang kunci di sumber data. Bidang kunci dari setiap rekaman di sumber data disimpan dalam koleksi ini. Ini memungkinkan Anda untuk menyimpan bidang kunci dengan kontrol daftar data tanpa menampilkannya di kontrol. Koleksi ini secara otomatis diisi dengan nilai dari bidang yang ditentukan oleh BaseDataList.DataKeyField properti . Koleksi ini tidak memperbolehkan Anda menambahkan atau menghapus item secara manual dari koleksi.
Bidang kunci umumnya digunakan dalam handler untuk suatu peristiwa, seperti ItemCommand atau DeleteCommand, sebagai bagian dari string kueri pembaruan untuk merevisi rekaman tertentu di sumber data. Bidang kunci membantu string kueri pembaruan mengidentifikasi rekaman yang sesuai untuk diubah.
Count Gunakan properti untuk menentukan jumlah item dalam koleksi. Untuk mengambil bidang kunci secara terprogram dari DataKeyCollection, gunakan salah satu metode berikut:
Gunakan pengindeks untuk mendapatkan satu bidang kunci dari koleksi, menggunakan notasi array.
CopyTo Gunakan metode untuk menyalin konten koleksi ke System.Array objek, yang kemudian dapat digunakan untuk mendapatkan item dari koleksi.
GetEnumerator Gunakan metode untuk membuat System.Collections.IEnumerator objek yang diimplementasikan, yang kemudian dapat digunakan untuk mendapatkan item dari koleksi.
Konstruktor
| Nama | Deskripsi |
|---|---|
| DataKeyCollection(ArrayList) |
Menginisialisasi instans baru dari kelas DataKeyCollection. |
Properti
| Nama | Deskripsi |
|---|---|
| Count |
Mendapatkan jumlah item dalam koleksi. |
| IsReadOnly |
Mendapatkan nilai yang menunjukkan apakah item dalam DataKeyCollection dapat dimodifikasi. |
| IsSynchronized |
Mendapatkan nilai yang menunjukkan apakah DataKeyCollection disinkronkan (aman utas). |
| Item[Int32] |
Mendapatkan bidang kunci pada indeks yang ditentukan dalam koleksi. |
| SyncRoot |
Mendapatkan objek yang digunakan untuk menyinkronkan akses ke DataKeyCollection. |
Metode
| Nama | Deskripsi |
|---|---|
| CopyTo(Array, Int32) |
Menyalin semua item dari DataKeyCollection ke objek yang ditentukan Array , dimulai dari indeks yang ditentukan dalam Array objek. |
| Equals(Object) |
Menentukan apakah objek yang ditentukan sama dengan objek saat ini. (Diperoleh dari Object) |
| GetEnumerator() |
IEnumerator Membuat objek yang diimplementasikan yang berisi semua bidang kunci di DataKeyCollection. |
| GetHashCode() |
Berfungsi sebagai fungsi hash default. (Diperoleh dari Object) |
| GetType() |
Mendapatkan Type instans saat ini. (Diperoleh dari Object) |
| MemberwiseClone() |
Membuat salinan dangkal dari Objectsaat ini. (Diperoleh dari Object) |
| ToString() |
Mengembalikan string yang mewakili objek saat ini. (Diperoleh dari Object) |
Metode Ekstensi
| Nama | Deskripsi |
|---|---|
| AsParallel(IEnumerable) |
Mengaktifkan paralelisasi kueri. |
| AsQueryable(IEnumerable) |
IEnumerable Mengonversi ke IQueryable. |
| Cast<TResult>(IEnumerable) |
Melemparkan elemen ke jenis yang IEnumerable ditentukan. |
| OfType<TResult>(IEnumerable) |
Memfilter elemen berdasarkan IEnumerable jenis tertentu. |