DataGridViewColumnCollection 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
表示控制項中的DataGridViewColumn一組DataGridView物件。
public ref class DataGridViewColumnCollection : System::Windows::Forms::BaseCollection, System::Collections::IList
[System.ComponentModel.ListBindable(false)]
public class DataGridViewColumnCollection : System.Windows.Forms.BaseCollection, System.Collections.IList
[<System.ComponentModel.ListBindable(false)>]
type DataGridViewColumnCollection = class
inherit BaseCollection
interface IList
interface ICollection
interface IEnumerable
Public Class DataGridViewColumnCollection
Inherits BaseCollection
Implements IList
- 繼承
- 屬性
- 實作
範例
以下程式碼範例說明此類型的使用方式。
private DataGridView dataGridView1 = new DataGridView();
private void AddColorColumn()
{
DataGridViewComboBoxColumn comboBoxColumn =
new DataGridViewComboBoxColumn();
comboBoxColumn.Items.AddRange(
Color.Red, Color.Yellow, Color.Green, Color.Blue);
comboBoxColumn.ValueType = typeof(Color);
dataGridView1.Columns.Add(comboBoxColumn);
dataGridView1.EditingControlShowing +=
new DataGridViewEditingControlShowingEventHandler(
dataGridView1_EditingControlShowing);
}
private void dataGridView1_EditingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e)
{
ComboBox combo = e.Control as ComboBox;
if (combo != null)
{
// Remove an existing event-handler, if present, to avoid
// adding multiple handlers when the editing control is reused.
combo.SelectedIndexChanged -=
new EventHandler(ComboBox_SelectedIndexChanged);
// Add the event handler.
combo.SelectedIndexChanged +=
new EventHandler(ComboBox_SelectedIndexChanged);
}
}
private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
((ComboBox)sender).BackColor = (Color)((ComboBox)sender).SelectedItem;
}
Private WithEvents dataGridView1 As New DataGridView()
Private Sub AddColorColumn()
Dim comboBoxColumn As New DataGridViewComboBoxColumn()
comboBoxColumn.Items.AddRange( _
Color.Red, Color.Yellow, Color.Green, Color.Blue)
comboBoxColumn.ValueType = GetType(Color)
dataGridView1.Columns.Add(comboBoxColumn)
End Sub
Private Sub dataGridView1_EditingControlShowing(ByVal sender As Object, _
ByVal e As DataGridViewEditingControlShowingEventArgs) _
Handles dataGridView1.EditingControlShowing
Dim combo As ComboBox = CType(e.Control, ComboBox)
If (combo IsNot Nothing) Then
' Remove an existing event-handler, if present, to avoid
' adding multiple handlers when the editing control is reused.
RemoveHandler combo.SelectedIndexChanged, _
New EventHandler(AddressOf ComboBox_SelectedIndexChanged)
' Add the event handler.
AddHandler combo.SelectedIndexChanged, _
New EventHandler(AddressOf ComboBox_SelectedIndexChanged)
End If
End Sub
Private Sub ComboBox_SelectedIndexChanged( _
ByVal sender As Object, ByVal e As EventArgs)
Dim comboBox1 As ComboBox = CType(sender, ComboBox)
comboBox1.BackColor = _
CType(CType(sender, ComboBox).SelectedItem, Color)
End Sub
備註
你可以透過 Columns 控制項的 DataGridView 屬性取得這個類別的實例。 該收藏透過該物業維持對控制 DataGridView 的參考。
建構函式
| 名稱 | Description |
|---|---|
| DataGridViewColumnCollection(DataGridView) |
初始化給定DataGridView類別的新實例DataGridViewColumnCollection。 |
屬性
| 名稱 | Description |
|---|---|
| Count |
取得集合中元素的總數。 (繼承來源 BaseCollection) |
| DataGridView |
取得集合執行欄位相關操作的 。DataGridView |
| IsReadOnly |
會獲得一個值,表示該集合是否為唯讀。 (繼承來源 BaseCollection) |
| IsSynchronized |
會取得一個值,表示存取是否 ICollection 同步。 (繼承來源 BaseCollection) |
| Item[Int32] |
取得或將欄位設於集合中給定的索引。 |
| Item[String] |
取得或設定集合中名字的欄位。 |
| List |
取得實例中包含的 BaseCollection 元素清單。 |
| SyncRoot |
取得一個物件,可用來同步存取 BaseCollection。 (繼承來源 BaseCollection) |
方法
事件
| 名稱 | Description |
|---|---|
| CollectionChanged |
發生於集合變更時。 |
明確介面實作
| 名稱 | Description |
|---|---|
| ICollection.CopyTo(Array, Int32) |
將整個集合內容複製到相容的一維 Array,從目標陣列指定的索引開始。 |
| ICollection.Count |
取得集合中的項目數目。 |
| ICollection.IsSynchronized |
會獲得一個值,表示存取該集合是否同步。 |
| ICollection.SyncRoot |
取得對象,這個物件可用來同步存取集合。 |
| IEnumerable.GetEnumerator() |
傳回逐一查看集合的列舉值。 |
| IList.Add(Object) |
在集合的末尾新增一個物件。 |
| IList.Clear() |
移除集合中的所有元素。 |
| IList.Contains(Object) |
判斷物件是否屬於集合中。 |
| IList.IndexOf(Object) |
決定集合中特定專案的索引。 |
| IList.Insert(Int32, Object) |
在指定的索引處插入一個元素到集合中。 |
| IList.IsFixedSize |
取得值,指出集合是否具有固定大小。 |
| IList.IsReadOnly |
會獲得一個值,表示該集合是否為唯讀。 |
| IList.Item[Int32] |
取得或設定位於指定索引處的專案。 |
| IList.Remove(Object) |
從集合中移除第一個出現的指定物件。 |
| IList.RemoveAt(Int32) |
從集合中移除具有指定索引的元素。 |
擴充方法
| 名稱 | Description |
|---|---|
| AsParallel(IEnumerable) |
啟用查詢的平行處理。 |
| AsQueryable(IEnumerable) |
將 IEnumerable 轉換成 IQueryable。 |
| Cast<TResult>(IEnumerable) |
將 IEnumerable 的項目轉換成指定的型別。 |
| OfType<TResult>(IEnumerable) |
根據指定的型別篩選 IEnumerable 的專案。 |