DataGridViewColumnCollection DataGridViewColumnCollection DataGridViewColumnCollection DataGridViewColumnCollection Class

Definition

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
Inheritance
DataGridViewColumnCollectionDataGridViewColumnCollectionDataGridViewColumnCollectionDataGridViewColumnCollection
Attributes
Implements

Examples

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

Remarks

You can retrieve an instance of this class through the Columns property of the DataGridView control. The collection maintains a reference to the control through the DataGridView property.

Constructors

DataGridViewColumnCollection(DataGridView) DataGridViewColumnCollection(DataGridView) DataGridViewColumnCollection(DataGridView) DataGridViewColumnCollection(DataGridView)

Initializes a new instance of the DataGridViewColumnCollection class for the given DataGridView.

Properties

Count Count Count Count

Gets the total number of elements in the collection.

(Inherited from BaseCollection)
DataGridView DataGridView DataGridView DataGridView

Gets the DataGridView upon which the collection performs column-related operations.

IsReadOnly IsReadOnly IsReadOnly IsReadOnly

Gets a value indicating whether the collection is read-only.

(Inherited from BaseCollection)
IsSynchronized IsSynchronized IsSynchronized IsSynchronized

Gets a value indicating whether access to the ICollection is synchronized.

(Inherited from BaseCollection)
Item[Int32] Item[Int32] Item[Int32] Item[Int32]

Gets or sets the column at the given index in the collection.

Item[String] Item[String] Item[String] Item[String]

Gets or sets the column of the given name in the collection.

List List List List

Gets the list of elements contained in the BaseCollection instance.

SyncRoot SyncRoot SyncRoot SyncRoot

Gets an object that can be used to synchronize access to the BaseCollection.

(Inherited from BaseCollection)

Methods

Add(DataGridViewColumn) Add(DataGridViewColumn) Add(DataGridViewColumn) Add(DataGridViewColumn)

Adds the given column to the collection.

Add(String, String) Add(String, String) Add(String, String) Add(String, String)

Adds a DataGridViewTextBoxColumn with the given column name and column header text to the collection.

AddRange(DataGridViewColumn[]) AddRange(DataGridViewColumn[]) AddRange(DataGridViewColumn[]) AddRange(DataGridViewColumn[])

Adds a range of columns to the collection.

Clear() Clear() Clear() Clear()

Clears the collection.

Contains(DataGridViewColumn) Contains(DataGridViewColumn) Contains(DataGridViewColumn) Contains(DataGridViewColumn)

Determines whether the collection contains the given column.

Contains(String) Contains(String) Contains(String) Contains(String)

Determines whether the collection contains the column referred to by the given name.

CopyTo(Array, Int32) CopyTo(Array, Int32) CopyTo(Array, Int32) CopyTo(Array, Int32)

Copies all the elements of the current one-dimensional Array to the specified one-dimensional Array starting at the specified destination Array index.

(Inherited from BaseCollection)
CopyTo(DataGridViewColumn[], Int32) CopyTo(DataGridViewColumn[], Int32) CopyTo(DataGridViewColumn[], Int32) CopyTo(DataGridViewColumn[], Int32)

Copies the items from the collection to the given array.

CreateObjRef(Type) CreateObjRef(Type) CreateObjRef(Type) CreateObjRef(Type)

Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.

(Inherited from MarshalByRefObject)
Equals(Object) Equals(Object) Equals(Object) Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetColumnCount(DataGridViewElementStates) GetColumnCount(DataGridViewElementStates) GetColumnCount(DataGridViewElementStates) GetColumnCount(DataGridViewElementStates)

Returns the number of columns that meet the given filter requirements.

GetColumnsWidth(DataGridViewElementStates) GetColumnsWidth(DataGridViewElementStates) GetColumnsWidth(DataGridViewElementStates) GetColumnsWidth(DataGridViewElementStates)

Returns the width, in pixels, required to display all of the columns that meet the given filter requirements.

GetEnumerator() GetEnumerator() GetEnumerator() GetEnumerator()

Gets the object that enables iterating through the members of the collection.

(Inherited from BaseCollection)
GetFirstColumn(DataGridViewElementStates) GetFirstColumn(DataGridViewElementStates) GetFirstColumn(DataGridViewElementStates) GetFirstColumn(DataGridViewElementStates)

Returns the first column in display order that meets the given inclusion-filter requirements.

GetFirstColumn(DataGridViewElementStates, DataGridViewElementStates) GetFirstColumn(DataGridViewElementStates, DataGridViewElementStates) GetFirstColumn(DataGridViewElementStates, DataGridViewElementStates) GetFirstColumn(DataGridViewElementStates, DataGridViewElementStates)

Returns the first column in display order that meets the given inclusion-filter and exclusion-filter requirements.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetLastColumn(DataGridViewElementStates, DataGridViewElementStates) GetLastColumn(DataGridViewElementStates, DataGridViewElementStates) GetLastColumn(DataGridViewElementStates, DataGridViewElementStates) GetLastColumn(DataGridViewElementStates, DataGridViewElementStates)

Returns the last column in display order that meets the given filter requirements.

GetLifetimeService() GetLifetimeService() GetLifetimeService() GetLifetimeService()

Retrieves the current lifetime service object that controls the lifetime policy for this instance.

(Inherited from MarshalByRefObject)
GetNextColumn(DataGridViewColumn, DataGridViewElementStates, DataGridViewElementStates) GetNextColumn(DataGridViewColumn, DataGridViewElementStates, DataGridViewElementStates) GetNextColumn(DataGridViewColumn, DataGridViewElementStates, DataGridViewElementStates) 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) GetPreviousColumn(DataGridViewColumn, DataGridViewElementStates, DataGridViewElementStates) GetPreviousColumn(DataGridViewColumn, DataGridViewElementStates, DataGridViewElementStates) GetPreviousColumn(DataGridViewColumn, DataGridViewElementStates, DataGridViewElementStates)

Gets the last column prior to the given column in display order that meets the given filter requirements.

GetType() GetType() GetType() GetType()

Gets the Type of the current instance.

(Inherited from Object)
IndexOf(DataGridViewColumn) IndexOf(DataGridViewColumn) IndexOf(DataGridViewColumn) IndexOf(DataGridViewColumn)

Gets the index of the given DataGridViewColumn in the collection.

InitializeLifetimeService() InitializeLifetimeService() InitializeLifetimeService() InitializeLifetimeService()

Obtains a lifetime service object to control the lifetime policy for this instance.

(Inherited from MarshalByRefObject)
Insert(Int32, DataGridViewColumn) Insert(Int32, DataGridViewColumn) Insert(Int32, DataGridViewColumn) Insert(Int32, DataGridViewColumn)

Inserts a column at the given index in the collection.

MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
MemberwiseClone(Boolean) MemberwiseClone(Boolean) MemberwiseClone(Boolean) MemberwiseClone(Boolean)

Creates a shallow copy of the current MarshalByRefObject object.

(Inherited from MarshalByRefObject)
OnCollectionChanged(CollectionChangeEventArgs) OnCollectionChanged(CollectionChangeEventArgs) OnCollectionChanged(CollectionChangeEventArgs) OnCollectionChanged(CollectionChangeEventArgs)

Raises the CollectionChanged event.

Remove(DataGridViewColumn) Remove(DataGridViewColumn) Remove(DataGridViewColumn) Remove(DataGridViewColumn)

Removes the specified column from the collection.

Remove(String) Remove(String) Remove(String) Remove(String)

Removes the column with the specified name from the collection.

RemoveAt(Int32) RemoveAt(Int32) RemoveAt(Int32) RemoveAt(Int32)

Removes the column at the given index in the collection.

ToString() ToString() ToString() ToString()

Returns a string that represents the current object.

(Inherited from Object)

Events

CollectionChanged CollectionChanged CollectionChanged CollectionChanged

Occurs when the collection changes.

Explicit Interface Implementations

ICollection.CopyTo(Array, Int32) ICollection.CopyTo(Array, Int32) ICollection.CopyTo(Array, Int32) ICollection.CopyTo(Array, Int32)

Copies the entire contents of the collection to a compatible one-dimensional Array, starting at the specified index of the target array.

ICollection.Count ICollection.Count ICollection.Count ICollection.Count

Gets the number of elements in the collection.

ICollection.IsSynchronized ICollection.IsSynchronized ICollection.IsSynchronized ICollection.IsSynchronized

Gets a value indicating whether access to the collection is synchronized.

ICollection.SyncRoot ICollection.SyncRoot ICollection.SyncRoot ICollection.SyncRoot

Gets an object that can be used to synchronize access to the collection.

IEnumerable.GetEnumerator() IEnumerable.GetEnumerator() IEnumerable.GetEnumerator() IEnumerable.GetEnumerator()

Returns an enumerator that iterates through the collection.

IList.Add(Object) IList.Add(Object) IList.Add(Object) IList.Add(Object)

Adds an object to the end of the collection.

IList.Clear() IList.Clear() IList.Clear() IList.Clear()

Removes all elements from the collection.

IList.Contains(Object) IList.Contains(Object) IList.Contains(Object) IList.Contains(Object)

Determines whether an object is in the collection.

IList.IndexOf(Object) IList.IndexOf(Object) IList.IndexOf(Object) IList.IndexOf(Object)

Determines the index of a specific item in the collection.

IList.Insert(Int32, Object) IList.Insert(Int32, Object) IList.Insert(Int32, Object) IList.Insert(Int32, Object)

Inserts an element into the collection at the specified index.

IList.IsFixedSize IList.IsFixedSize IList.IsFixedSize IList.IsFixedSize

Gets a value indicating whether the collection has a fixed size.

IList.IsReadOnly IList.IsReadOnly IList.IsReadOnly IList.IsReadOnly

Gets a value indicating whether the collection is read-only.

IList.Item[Int32] IList.Item[Int32] IList.Item[Int32] IList.Item[Int32]

Gets or sets the element at the specified index.

IList.Remove(Object) IList.Remove(Object) IList.Remove(Object) IList.Remove(Object)

Removes the first occurrence of the specified object from the collection.

IList.RemoveAt(Int32) IList.RemoveAt(Int32) IList.RemoveAt(Int32) IList.RemoveAt(Int32)

Removes the element with the specified index from the collection.

Extension Methods

Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable)

Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable)

Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable)

Enables parallelization of a query.

AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable)

Converts an IEnumerable to an IQueryable.

Applies to

See also