DataColumnCollection Class

Definition

Represents a collection of DataColumn objects for a DataTable.

public sealed class DataColumnCollection : System.Data.InternalDataCollectionBase
Inheritance
DataColumnCollection

Inherited Members

System.Data.InternalDataCollectionBase

System.Object

Examples

The following example iterates through a collection of DataColumn objects and prints various properties of each column in the collection.

private void PrintDataTableColumnInfo(DataTable table)
{
    // Use a DataTable object's DataColumnCollection.
    DataColumnCollection columns = table.Columns;

    // Print the ColumnName and DataType for each column.
    foreach(DataColumn column in columns)
    {
        Console.WriteLine(column.ColumnName);
        Console.WriteLine(column.DataType);
    }
}
Private Sub PrintDataTableColumnInfo(table As DataTable)
    
    ' Use a DataTable object's DataColumnCollection.
    Dim columns As DataColumnCollection = table.Columns

    ' Print the ColumnName and DataType for each column.
    Dim column As DataColumn
    For Each column in columns
       Console.WriteLine(column.ColumnName)
       Console.WriteLine(column.DataType.ToString)
    Next
End Sub

Remarks

The DataColumnCollection defines the schema of a DataTable, and determines what kind of data each DataColumn can contain. You can access the DataColumnCollection through the Columns property of the DataTable object.

The DataColumnCollection uses the Add and Remove methods to insert and delete DataColumn objects. Use the Count property to determine how many DataColumn objects are in the collection. Use the Contains method to verify whether a specified index or column name exists in the collection.

Properties

Item[Int32]

Gets the DataColumn from the collection at the specified index.

Item[String]

Gets the DataColumn from the collection with the specified name.

Methods

Add()

Creates and adds a DataColumn object to the DataColumnCollection.

Add(DataColumn)

Creates and adds the specified DataColumn object to the DataColumnCollection.

Add(String)

Creates and adds a DataColumn object that has the specified name to the DataColumnCollection.

Add(String, Type)

Creates and adds a DataColumn object that has the specified name and type to the DataColumnCollection.

Add(String, Type, String)

Creates and adds a DataColumn object that has the specified name, type, and expression to the DataColumnCollection.

AddRange(DataColumn[])

Copies the elements of the specified DataColumn array to the end of the collection.

CanRemove(DataColumn)

Checks whether a specific column can be removed from the collection.

Clear()

Clears the collection of any columns.

Contains(String)

Checks whether the collection contains a column with the specified name.

CopyTo(DataColumn[], Int32)

Copies the entire collection into an existing array, starting at a specified index within the array.

IndexOf(DataColumn)

Gets the index of a column specified by name.

IndexOf(String)

Gets the index of the column with the specific name (the name is not case sensitive).

Remove(DataColumn)

Removes the specified DataColumn object from the collection.

Remove(String)

Removes the DataColumn object that has the specified name from the collection.

RemoveAt(Int32)

Removes the column at the specified index from the collection.

Events

CollectionChanged

Occurs when the columns collection changes, either by adding or removing a column.

Extension Methods

Cast<TResult>(IEnumerable)
OfType<TResult>(IEnumerable)
AsParallel(IEnumerable)
AsQueryable(IEnumerable)

Thread Safety

This type is safe for multithreaded read operations. You must synchronize any write operations.