DataGridColumnCollection 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.
Kumpulan DataGridColumnobjek kolom -turunan yang mewakili kolom dalam DataGrid kontrol. Kelas ini tidak dapat diwariskan.
public ref class DataGridColumnCollection sealed : System::Collections::ICollection, System::Web::UI::IStateManager
public sealed class DataGridColumnCollection : System.Collections.ICollection, System.Web.UI.IStateManager
type DataGridColumnCollection = class
interface ICollection
interface IEnumerable
interface IStateManager
Public NotInheritable Class DataGridColumnCollection
Implements ICollection, IStateManager
- Warisan
-
DataGridColumnCollection
- Penerapan
Contoh
Contoh kode berikut menunjukkan cara menggunakan DataGridColumnCollection koleksi untuk menambahkan kolom secara dinamis ke DataGrid kontrol. Perhatikan bahwa Columns properti DataGrid kontrol adalah instans DataGridColumnCollection kelas .
<%@ Page Language="C#" 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" >
<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)));
// 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);
}
DataView dv = new DataView(dt);
return dv;
}
void Page_Load(Object sender, EventArgs e)
{
// Create a DataGrid control.
DataGrid ItemsGrid = new DataGrid();
// Set the properties of the DataGrid.
ItemsGrid.ID = "ItemsGrid";
ItemsGrid.BorderColor = System.Drawing.Color.Black;
ItemsGrid.CellPadding = 3;
ItemsGrid.AutoGenerateColumns = false;
// Set the styles for the DataGrid.
ItemsGrid.HeaderStyle.BackColor =
System.Drawing.Color.FromArgb(0x0000aaaa);
// Create the columns for the DataGrid control. The DataGrid
// columns are dynamically generated. Therefore, the columns
// must be re-created each time the page is refreshed.
// Create and add the columns to the collection.
ItemsGrid.Columns.Add(CreateBoundColumn("IntegerValue", "Item"));
ItemsGrid.Columns.Add(
CreateBoundColumn("StringValue", "Description"));
ItemsGrid.Columns.Add(
CreateBoundColumn("CurrencyValue", "Price", "{0:c}",
HorizontalAlign.Right));
ItemsGrid.Columns.Add(
CreateLinkColumn("http://www.microsoft.com", "_self",
"Microsoft", "Related link"));
// Specify the data source and bind it to the control.
ItemsGrid.DataSource = CreateDataSource();
ItemsGrid.DataBind();
// Add the DataGrid control to the Controls collection of
// the PlaceHolder control.
Place.Controls.Add(ItemsGrid);
}
BoundColumn CreateBoundColumn(String DataFieldValue,
String HeaderTextValue)
{
// This version of the CreateBoundColumn method sets only the
// DataField and HeaderText properties.
// Create a BoundColumn.
BoundColumn column = new BoundColumn();
// Set the properties of the BoundColumn.
column.DataField = DataFieldValue;
column.HeaderText = HeaderTextValue;
return column;
}
BoundColumn CreateBoundColumn(String DataFieldValue,
String HeaderTextValue, String FormatValue,
HorizontalAlign AlignValue)
{
// This version of CreateBoundColumn method sets the DataField,
// HeaderText, and DataFormatString properties. It also sets the
// HorizontalAlign property of the ItemStyle property of the column.
// Create a BoundColumn using the overloaded CreateBoundColumn method.
BoundColumn column = CreateBoundColumn(DataFieldValue, HeaderTextValue);
// Set the properties of the BoundColumn.
column.DataFormatString = FormatValue;
column.ItemStyle.HorizontalAlign = AlignValue;
return column;
}
HyperLinkColumn CreateLinkColumn(String NavUrlValue,
String TargetValue, String TextValue, String HeaderTextValue)
{
// Create a BoundColumn.
HyperLinkColumn column = new HyperLinkColumn();
// Set the properties of the ButtonColumn.
column.NavigateUrl = NavUrlValue;
column.Target = TargetValue;
column.Text = TextValue;
column.HeaderText = HeaderTextValue;
return column;
}
</script>
<head runat="server">
<title>DataGrid Constructor Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>DataGrid Constructor Example</h3>
<b>Product List</b>
<asp:PlaceHolder id="Place"
runat="server"/>
</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" >
<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(Int32)))
dt.Columns.Add(New DataColumn("StringValue", GetType(string)))
dt.Columns.Add(New DataColumn("CurrencyValue", GetType(double)))
' 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 i
Dim dv As DataView = New DataView(dt)
Return dv
End Function
Sub Page_Load(sender As Object, e As EventArgs)
' Create a DataGrid control.
Dim ItemsGrid As DataGrid = New DataGrid()
' Set the properties of the DataGrid.
ItemsGrid.ID = "ItemsGrid"
ItemsGrid.BorderColor = System.Drawing.Color.Black
ItemsGrid.CellPadding = 3
ItemsGrid.AutoGenerateColumns = False
' Set the styles for the DataGrid.
ItemsGrid.HeaderStyle.BackColor = System.Drawing.Color.FromArgb(&H0000aaaa)
' Create the columns for the DataGrid control. The DataGrid
' columns are dynamically generated. Therefore, the columns
' must be re-created each time the page is refreshed.
' Create and add the columns to the collection.
ItemsGrid.Columns.Add(CreateBoundColumn("IntegerValue", "Item"))
ItemsGrid.Columns.Add( _
CreateBoundColumn("StringValue", "Description"))
ItemsGrid.Columns.Add( _
CreateBoundColumn("CurrencyValue", "Price", "{0:c}", _
HorizontalAlign.Right))
ItemsGrid.Columns.Add( _
CreateLinkColumn("http:'www.microsoft.com", "_self", _
"Microsoft", "Related link"))
' Specify the data source and bind it to the control.
ItemsGrid.DataSource = CreateDataSource()
ItemsGrid.DataBind()
' Add the DataGrid control to the Controls collection of
' the PlaceHolder control.
Place.Controls.Add(ItemsGrid)
End Sub
Function CreateBoundColumn(DataFieldValue As String, HeaderTextValue As String) As BoundColumn
' This version of CreateBoundColumn method sets only the
' DataField and HeaderText properties.
' Create a BoundColumn.
Dim column As BoundColumn = New BoundColumn()
' Set the properties of the BoundColumn.
column.DataField = DataFieldValue
column.HeaderText = HeaderTextValue
Return column
End Function
Function CreateBoundColumn(DataFieldValue As String, _
HeaderTextValue As String, FormatValue As String, _
AlignValue As HorizontalAlign) As BoundColumn
' This version of CreateBoundColumn method sets the DataField,
' HeaderText, and DataFormatString properties. It also sets the
' HorizontalAlign property of the ItemStyle property of the column.
' Create a BoundColumn using the overloaded CreateBoundColumn method.
Dim column As BoundColumn = CreateBoundColumn(DataFieldValue, HeaderTextValue)
' Set the properties of the BoundColumn.
column.DataFormatString = FormatValue
column.ItemStyle.HorizontalAlign = AlignValue
Return column
End Function
Function CreateLinkColumn(NavUrlValue As String, TargetValue As String, _
TextValue As String, HeaderTextValue As String) As HyperLinkColumn
' Create a BoundColumn.
Dim column As HyperLinkColumn = New HyperLinkColumn()
' Set the properties of the ButtonColumn.
column.NavigateUrl = NavUrlValue
column.Target = TargetValue
column.Text = TextValue
column.HeaderText = HeaderTextValue
Return column
End Function
</script>
<head runat="server">
<title>DataGrid Constructor Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>DataGrid Constructor Example</h3>
<b>Product List</b>
<asp:PlaceHolder id="Place"
runat="server"/>
</form>
</body>
</html>
Keterangan
DataGridColumnCollection Gunakan koleksi untuk mengelola kumpulan objek kolom -turunan DataGridColumnsecara terprogram. Objek ini mewakili kolom dalam DataGrid kontrol. Anda dapat menambahkan, menghapus, atau menyisipkan kolom dalam DataGridColumnCollection koleksi.
Nota
AutoGenerateColumns Ketika properti diatur ke true, kolom yang dibuat oleh DataGrid kontrol tidak ditambahkan ke Columns koleksi.
Kontrol DataGrid tidak menyimpan isi koleksinya Columns dalam status tampilan. Untuk menambahkan atau menghapus kolom secara dinamis, Anda harus menambahkan atau menghapus kolom secara terprogram setiap kali halaman di-refresh.
Page_Init Berikan fungsi yang menambahkan atau menghapus kolom sebelum DataGrid status kontrol dimuat ulang dan kontrol dibangun kembali. Jika tidak, perubahan pada Columns koleksi tidak tercermin dalam DataGrid kontrol saat ditampilkan.
Nota
Meskipun Anda dapat secara terprogram menambahkan kolom ke atau menghapus kolom dari Columns kumpulan DataGrid kontrol, lebih mudah untuk mencantumkan kolom secara statis lalu menggunakan Visible properti untuk menampilkan atau menyembunyikan setiap kolom.
Urutan kolom dalam koleksi menentukan urutan kolom ditampilkan dalam DataGrid kontrol.
Tabel berikut mencantumkan berbagai kelas kolom yang berasal dari DataGridColumn kelas .
| Kelas Kolom | Deskripsi |
|---|---|
| BoundColumn | Kolom yang terikat ke bidang di sumber data. Ini menampilkan setiap item di bidang sebagai teks. Ini adalah jenis kolom default untuk DataGrid kontrol. |
| ButtonColumn | Kolom yang menampilkan tombol perintah untuk setiap item di kolom. Ini memungkinkan Anda membuat kolom kontrol tombol kustom, seperti tombol Tambahkan atau Hapus. |
| EditCommandColumn | Kolom yang berisi perintah pengeditan untuk setiap item di kolom. |
| HyperLinkColumn | Kolom yang menampilkan setiap item dalam kolom sebagai hyperlink. Konten kolom dapat terikat ke bidang di sumber data, atau ke teks statis. |
| TemplateColumn | Kolom yang menampilkan setiap item dalam kolom sesuai dengan templat tertentu. Ini memungkinkan Anda mengontrol konten kolom, misalnya untuk menampilkan gambar. |
Nota
Kelas DataGridColumn adalah kelas dasar untuk kelas kolom yang tercantum. Ini tidak digunakan langsung dalam DataGridColumnCollection koleksi.
Konstruktor
| Nama | Deskripsi |
|---|---|
| DataGridColumnCollection(DataGrid, ArrayList) |
Menginisialisasi instans baru dari kelas DataGridColumnCollection. |
Properti
| Nama | Deskripsi |
|---|---|
| Count |
Mendapatkan jumlah kolom dalam DataGridColumnCollection koleksi. |
| IsReadOnly |
Mendapatkan nilai yang menunjukkan apakah kolom dalam DataGridColumnCollection koleksi dapat dimodifikasi. |
| IsSynchronized |
Mendapatkan nilai yang menunjukkan apakah akses ke koleksi disinkronkan DataGridColumnCollection (utas aman). |
| Item[Int32] |
DataGridColumnMendapatkan objek kolom -turunan dari DataGridColumnCollection koleksi pada indeks yang ditentukan. |
| SyncRoot |
Mendapatkan objek yang dapat digunakan untuk menyinkronkan akses ke DataGridColumnCollection koleksi. |
Metode
| Nama | Deskripsi |
|---|---|
| Add(DataGridColumn) |
Menambahkan objek kolom -turunan yang ditentukan DataGridColumnke akhir DataGridColumnCollection koleksi. |
| AddAt(Int32, DataGridColumn) |
DataGridColumnMenyisipkan objek kolom -turunan dalam DataGridColumnCollection koleksi pada indeks yang ditentukan. |
| Clear() |
Menghapus semua DataGridColumnobjek kolom -turunan dari DataGridColumnCollection koleksi. |
| CopyTo(Array, Int32) |
Menyalin item dari DataGridColumnCollection koleksi ke yang ditentukan Array, dimulai dari indeks yang ditentukan di Array. |
| Equals(Object) |
Menentukan apakah objek yang ditentukan sama dengan objek saat ini. (Diperoleh dari Object) |
| GetEnumerator() |
Mengembalikan IEnumerator antarmuka yang berisi semua DataGridColumnobjek kolom -turunan dalam DataGridColumnCollection koleksi. |
| GetHashCode() |
Berfungsi sebagai fungsi hash default. (Diperoleh dari Object) |
| GetType() |
Mendapatkan Type instans saat ini. (Diperoleh dari Object) |
| IndexOf(DataGridColumn) |
Mengembalikan indeks objek kolom -turunan yang ditentukan DataGridColumndari DataGridColumnCollection koleksi. |
| MemberwiseClone() |
Membuat salinan dangkal dari Objectsaat ini. (Diperoleh dari Object) |
| Remove(DataGridColumn) |
Menghapus objek kolom -turunan yang ditentukan DataGridColumndari DataGridColumnCollection koleksi. |
| RemoveAt(Int32) |
DataGridColumnMenghapus objek kolom -turunan dari DataGridColumnCollection koleksi pada indeks yang ditentukan. |
| ToString() |
Mengembalikan string yang mewakili objek saat ini. (Diperoleh dari Object) |
Implementasi Antarmuka Eksplisit
| Nama | Deskripsi |
|---|---|
| IStateManager.IsTrackingViewState |
Mendapatkan nilai yang menunjukkan apakah koleksi melacak perubahan status tampilannya. |
| IStateManager.LoadViewState(Object) |
Memuat status yang disimpan sebelumnya. |
| IStateManager.SaveViewState() |
Mengembalikan objek yang berisi perubahan status. |
| IStateManager.TrackViewState() |
Mulai melacak perubahan status. |
Metode Ekstensi
| Nama | Deskripsi |
|---|---|
| AsParallel(IEnumerable) |
Mengaktifkan paralelisasi kueri. |
| AsQueryable(IEnumerable) |
Mengonversi IEnumerable menjadi IQueryable. |
| Cast<TResult>(IEnumerable) |
Melemparkan elemen IEnumerable ke jenis yang ditentukan. |
| OfType<TResult>(IEnumerable) |
Memfilter elemen IEnumerable berdasarkan jenis tertentu. |