Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

DataTable.Columns Property

Gets the collection of columns that belong to this table.

[Visual Basic]
Public ReadOnly Property Columns As DataColumnCollection
[C#]
public DataColumnCollection Columns {get;}
[C++]
public: __property DataColumnCollection* get_Columns();
[JScript]
public function get Columns() : DataColumnCollection;

Property Value

A DataColumnCollection that contains the collection of DataColumn objects for the table; otherwise a null value if no DataColumn objects exist.

Remarks

The DataColumnCollection determines the schema of a table by defining the data type of each column.

Example

[Visual Basic, C#, C++] The following example prints each value of each row in a table using the Columns property.

[Visual Basic] 
Private Sub PrintValues(ByVal myTable As DataTable)
    Dim myRow As DataRow
    Dim myColumn As DataColumn
    For Each myRow in myTable.Rows
       For Each myColumn In myTable.Columns
          Console.WriteLine(myRow(myColumn))
       Next
    Next
 End Sub

[C#] 
private void PrintValues(DataTable myTable){
    foreach(DataRow myRow in myTable.Rows){
       foreach(DataColumn myCol in myTable.Columns){
          Console.WriteLine(myRow[myCol]);
       }
    }
 }

[C++] 
private:
void PrintValues(DataTable* myTable){
    System::Collections::IEnumerator* myEnum = myTable->Rows->GetEnumerator();
    while (myEnum->MoveNext())
    {
       DataRow* myRow = __try_cast<DataRow*>(myEnum->Current);
       System::Collections::IEnumerator* myEnum1 = myTable->Columns->GetEnumerator();
       while (myEnum1->MoveNext())
       {
          DataColumn* myCol = __try_cast<DataColumn*>(myEnum1->Current);
          Console::WriteLine(myRow->Item[myCol]);
       }
    }
 }

[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

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

See Also

DataTable Class | DataTable Members | System.Data Namespace | DataColumnCollection | DataColumn

Show:
© 2015 Microsoft