Export (0) Print
Expand All
1 out of 3 rated this helpful - Rate this topic

DataColumnCollection Class

Represents a collection of DataColumn objects for a DataTable.

For a list of all members of this type, see DataColumnCollection Members.

System.Object
   System.Data.InternalDataCollectionBase
      System.Data.DataColumnCollection

[Visual Basic]
<Serializable>
Public Class DataColumnCollection
   Inherits InternalDataCollectionBase
[C#]
[Serializable]
public class DataColumnCollection : InternalDataCollectionBase
[C++]
[Serializable]
public __gc class DataColumnCollection : public
   InternalDataCollectionBase
[JScript]
public
   Serializable
class DataColumnCollection extends
   InternalDataCollectionBase

Thread Safety

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

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.

Example

[Visual Basic, C#, C++] The following example iterates through a collection of DataColumn objects and prints various properties of each column in the collection.

[Visual Basic] 
Private Sub PrintDataTableColumnInfo(myTable As DataTable)
    Dim cols As DataColumnCollection
    ' Use a DataTable object's DataColumnCollection.
    cols = myTable.Columns
    ' Print the ColumnName and DataType for each column.
    Dim col As DataColumn
    For Each col in cols
       Console.WriteLine(col.ColumnName)
       Console.WriteLine(col.DataType.ToString)
    Next
 End Sub

[C#] 
private void PrintDataTableColumnInfo(DataTable myTable){
    DataColumnCollection cols;
    // Use a DataTable object's DataColumnCollection.
    cols = myTable.Columns;
    // Print the ColumnName and DataType for each column.
    foreach(DataColumn col in cols){
       Console.WriteLine(col.ColumnName);
       Console.WriteLine(col.DataType);
    }
 }

[C++] 
private:
void PrintDataTableColumnInfo(DataTable* myTable){
   DataColumnCollection* cols;
   // Use a DataTable object's DataColumnCollection.
   cols = myTable->Columns;
   // Print the ColumnName and DataType for each column.
   System::Collections::IEnumerator* myEnum = cols->GetEnumerator();
   while (myEnum->MoveNext())
   {
      DataColumn* col = __try_cast<DataColumn*>(myEnum->Current);
      Console::WriteLine(col->ColumnName);
      Console::WriteLine(col->DataType);
   }
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Data

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: System.Data (in System.Data.dll)

See Also

DataColumnCollection Members | System.Data Namespace | Columns | DataColumn | DataTable | DataRelation | DataRow | DataSet

Show:
© 2014 Microsoft. All rights reserved.