DataGridViewColumnCollection 類別

定義

代表 DataGridViewColumn 控制項中 DataGridView 物件的集合。Represents a collection of DataGridViewColumn objects in a DataGridView control.

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
type DataGridViewColumnCollection = class
    inherit BaseCollection
    interface IList
    interface ICollection
    interface IEnumerable
Public Class DataGridViewColumnCollection
Inherits BaseCollection
Implements IList
繼承
DataGridViewColumnCollection
屬性
實作

範例

下列程式碼範例說明如何使用此類型。The following code example illustrates the use of this type.

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控制項的屬性來抓取這個類別的實例。You can retrieve an instance of this class through the Columns property of the DataGridView control. 集合會透過DataGridView屬性維護對控制項的參考。The collection maintains a reference to the control through the DataGridView property.

建構函式

DataGridViewColumnCollection(DataGridView)

為指定的 DataGridViewColumnCollection,初始化 DataGridView 類別的新執行個體。Initializes a new instance of the DataGridViewColumnCollection class for the given DataGridView.

屬性

Count

取得集合中的項目總數。Gets the total number of elements in the collection.

(繼承來源 BaseCollection)
DataGridView

在集合執行資料行相關作業時取得 DataGridViewGets the DataGridView upon which the collection performs column-related operations.

IsReadOnly

取得值,表示集合是否為唯讀。Gets a value indicating whether the collection is read-only.

(繼承來源 BaseCollection)
IsSynchronized

取得值,表示對 ICollection 的存取是否同步化。Gets a value indicating whether access to the ICollection is synchronized.

(繼承來源 BaseCollection)
Item[Int32]

在集合中的指定索引處取得或設定資料行。Gets or sets the column at the given index in the collection.

Item[String]

在集合中取得或設定指定名稱的資料行。Gets or sets the column of the given name in the collection.

List

取得包含於 BaseCollection 執行個體中的元素清單。Gets the list of elements contained in the BaseCollection instance.

SyncRoot

取得可用以同步存取 BaseCollection 的物件。Gets an object that can be used to synchronize access to the BaseCollection.

(繼承來源 BaseCollection)

方法

Add(DataGridViewColumn)

將指定的資料行加入至集合。Adds the given column to the collection.

Add(String, String)

將具有指定資料行名稱和資料行行首文字的 DataGridViewTextBoxColumn 加入集合。Adds a DataGridViewTextBoxColumn with the given column name and column header text to the collection.

AddRange(DataGridViewColumn[])

將某個範圍的資料行加入至集合。Adds a range of columns to the collection.

Clear()

清除集合。Clears the collection.

Contains(DataGridViewColumn)

判斷集合是否包含指定的資料行。Determines whether the collection contains the given column.

Contains(String)

判斷集合是否包含指定名稱所參考的資料行。Determines whether the collection contains the column referred to by the given name.

CopyTo(Array, Int32)

將目前一維 Array 的所有元素複製到指定的一維 Array (從指定的目的 Array 索引開始)。Copies all the elements of the current one-dimensional Array to the specified one-dimensional Array starting at the specified destination Array index.

(繼承來源 BaseCollection)
CopyTo(DataGridViewColumn[], Int32)

將項目從集合中複製至指定的陣列。Copies the items from the collection to the given array.

CreateObjRef(Type)

建立包含所有相關資訊的物件,這些資訊是產生用來與遠端物件通訊的所需 Proxy。Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.

(繼承來源 MarshalByRefObject)
Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
GetColumnCount(DataGridViewElementStates)

傳回符合指定之篩選條件需求的資料行數。Returns the number of columns that meet the given filter requirements.

GetColumnsWidth(DataGridViewElementStates)

傳回要顯示所有符合指定之篩選條件需求的資料行所需的寬度 (以像素為單位)。Returns the width, in pixels, required to display all of the columns that meet the given filter requirements.

GetEnumerator()

取得允許逐一查看集合之成員的物件。Gets the object that enables iterating through the members of the collection.

(繼承來源 BaseCollection)
GetFirstColumn(DataGridViewElementStates)

傳回顯示順序中第一個符合所指定包含篩選條件需求的資料行。Returns the first column in display order that meets the given inclusion-filter requirements.

GetFirstColumn(DataGridViewElementStates, DataGridViewElementStates)

傳回顯示順序中第一個符合所指定包含篩選條件需求和排除篩選條件需求的資料行。Returns the first column in display order that meets the given inclusion-filter and exclusion-filter requirements.

GetHashCode()

作為預設雜湊函數。Serves as the default hash function.

(繼承來源 Object)
GetLastColumn(DataGridViewElementStates, DataGridViewElementStates)

傳回顯示順序中最後一個符合所指定篩選條件需求的資料行。Returns the last column in display order that meets the given filter requirements.

GetLifetimeService()

擷取控制這個執行個體存留期 (Lifetime) 原則的目前存留期服務物件。Retrieves the current lifetime service object that controls the lifetime policy for this instance.

(繼承來源 MarshalByRefObject)
GetNextColumn(DataGridViewColumn, DataGridViewElementStates, DataGridViewElementStates)

取得位在顯示順序中指定資料行後面且符合所指定篩選條件需求的第一個資料行。Gets the first column after the given column in display order that meets the given filter requirements.

GetPreviousColumn(DataGridViewColumn, DataGridViewElementStates, DataGridViewElementStates)

取得位在顯示順序中指定資料行前面且符合所指定篩選條件需求的最後一個資料行。Gets the last column prior to the given column in display order that meets the given filter requirements.

GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
IndexOf(DataGridViewColumn)

取得集合中指定 DataGridViewColumn 的索引。Gets the index of the given DataGridViewColumn in the collection.

InitializeLifetimeService()

取得存留期服務物件,以控制這個執行個體的存留期原則。Obtains a lifetime service object to control the lifetime policy for this instance.

(繼承來源 MarshalByRefObject)
Insert(Int32, DataGridViewColumn)

在集合中的指定索引處插入資料行。Inserts a column at the given index in the collection.

MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
MemberwiseClone(Boolean)

建立目前 MarshalByRefObject 物件的淺層複本。Creates a shallow copy of the current MarshalByRefObject object.

(繼承來源 MarshalByRefObject)
OnCollectionChanged(CollectionChangeEventArgs)

引發 CollectionChanged 事件。Raises the CollectionChanged event.

Remove(DataGridViewColumn)

從集合中移除指定的資料行。Removes the specified column from the collection.

Remove(String)

從集合移除具有指定名稱的資料行。Removes the column with the specified name from the collection.

RemoveAt(Int32)

從集合中移除指定索引處的資料行。Removes the column at the given index in the collection.

ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)

事件

CollectionChanged

發生於集合變更時。Occurs when the collection changes.

明確介面實作

ICollection.CopyTo(Array, Int32)

將集合的整個內容複製到相容的一維 Array (從目標陣列的指定索引處開始)。Copies the entire contents of the collection to a compatible one-dimensional Array, starting at the specified index of the target array.

ICollection.Count

取得集合中的項目數。Gets the number of elements in the collection.

ICollection.IsSynchronized

取得值,表示對集合的存取是否為同步的。Gets a value indicating whether access to the collection is synchronized.

ICollection.SyncRoot

取得物件,這個物件可以用來對集合進行同步存取。Gets an object that can be used to synchronize access to the collection.

IEnumerable.GetEnumerator()

傳回逐一查看集合的列舉值。Returns an enumerator that iterates through the collection.

IList.Add(Object)

將物件加入至集合的結尾。Adds an object to the end of the collection.

IList.Clear()

從集合中移除所有項目。Removes all elements from the collection.

IList.Contains(Object)

判斷物件是否在集合中。Determines whether an object is in the collection.

IList.IndexOf(Object)

決定集合中指定項目的索引。Determines the index of a specific item in the collection.

IList.Insert(Int32, Object)

將元素插入至集合中的指定索引處。Inserts an element into the collection at the specified index.

IList.IsFixedSize

取得值,這個值表示集合的大小是否是固定的。Gets a value indicating whether the collection has a fixed size.

IList.IsReadOnly

取得值,表示集合是否為唯讀。Gets a value indicating whether the collection is read-only.

IList.Item[Int32]

在指定的索引位置上取得或設定項目。Gets or sets the element at the specified index.

IList.Remove(Object)

從集合中移除指定物件的第一個符合項目。Removes the first occurrence of the specified object from the collection.

IList.RemoveAt(Int32)

移除集合中具有指定索引的項目。Removes the element with the specified index from the collection.

擴充方法

Cast<TResult>(IEnumerable)

IEnumerable 的項目轉換成指定的型別。Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable)

根據指定的型別來篩選 IEnumerable 的項目。Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable)

啟用查詢的平行化作業。Enables parallelization of a query.

AsQueryable(IEnumerable)

IEnumerable 轉換成 IQueryableConverts an IEnumerable to an IQueryable.

適用於

另請參閱