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

获取 DataGridView,集合在此基础上执行与列相关的操作。Gets 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)

创建一个对象,该对象包含生成用于与远程对象进行通信的代理所需的全部相关信息。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()

检索控制此实例的生存期策略的当前生存期服务对象。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()

返回一个表示当前对象的 string。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.

适用于

另请参阅