Export (0) Print
Expand All

DataColumn.ColumnName Property

Gets or sets the name of the column in the DataColumnCollection.

[Visual Basic]
Public Property ColumnName As String
[C#]
public string ColumnName {get; set;}
[C++]
public: __property String* get_ColumnName();
public: __property void set_ColumnName(String*);
[JScript]
public function get ColumnName() : String;
public function set ColumnName(String);

Property Value

The name of the column.

Exceptions

Exception Type Condition
ArgumentException The property is set to a null reference (Nothing in Visual Basic) or an empty string and the column belongs to a collection.
DuplicateNameException A column with the same name already exists in the collection. The name comparison is not case sensitive.

Remarks

When a DataColumn is created, it has no ColumnName value. When the DataColumn is added to a DataTable object's DataColumnCollection, however, it is given a default name ("Column1", "Column2", etc.).

The Caption value is set to the ColumnName value by default.

Example

[Visual Basic, C#, C++] The following examples gets the ColumnName for every column in every table in a DataSet. The example also shows how to create a DataColumn with a new ColumnName.

[Visual Basic] 
Private Sub PrintColumnNames(myDataSet As DataSet)
 Dim myTable As DataTable
 Dim myColumn As DataColumn 
 ' For each table in the DataSet, print the ColumnName.
 For Each myTable in myDataSet.Tables
 For Each myColumn in myTable.Columns
 Console.WriteLine(myColumn.ColumnName)
 Next
 Next
 End Sub
 
 Private Sub AddColumn(myTable As DataTable)
 Dim myColumn As DataColumn
 myColumn = New DataColumn()
 With myColumn
 .ColumnName = "SupplierID"
 .DataType = System.Type.GetType("System.String")
 .Unique = True
 .AutoIncrement = False
 .Caption = "SupplierID"
 .ReadOnly = False
 End With
 ' Add the column to the table's columns collection.
 myTable.Columns.Add(myColumn)
 End Sub

[C#] 
private void PrintColumnNames(DataSet myDataSet){
    // For each table in the DataSet, print the ColumnName.
    foreach(DataTable myTable in myDataSet.Tables){
       foreach(DataColumn myColumn in myTable.Columns){
          Console.WriteLine(myColumn.ColumnName);
       }
    }
 }
 
 private void AddColumn(DataTable myTable){
    DataColumn myColumn;
    myColumn = new DataColumn();
    myColumn.ColumnName = "SupplierID";
    myColumn.DataType = System.Type.GetType("System.String");
    myColumn.Unique = true;
    myColumn.AutoIncrement = false;
    myColumn.Caption = "SupplierID";
    myColumn.ReadOnly = false;
    // Add the column to the table's columns collection.
    myTable.Columns.Add(myColumn);
 }

[C++] 
private:
void PrintColumnNames(DataSet* myDataSet){
    // For each table in the DataSet, print the ColumnName.
    System::Collections::IEnumerator* myEnum = myDataSet->Tables->GetEnumerator();
    while (myEnum->MoveNext())
    {
       DataTable* myTable = __try_cast<DataTable*>(myEnum->Current);
       System::Collections::IEnumerator* myEnum1 = myTable->Columns->GetEnumerator();
       while (myEnum1->MoveNext())
       {
          DataColumn* myColumn = __try_cast<DataColumn*>(myEnum1->Current);
          Console::WriteLine(myColumn->ColumnName);
       }
    }
 }
 
private:
 void AddColumn(DataTable* myTable){
    DataColumn* myColumn;
    myColumn = new DataColumn();
    myColumn->ColumnName = S"SupplierID";
    myColumn->DataType = System::Type::GetType(S"System.String");
    myColumn->Unique = true;
    myColumn->AutoIncrement = false;
    myColumn->Caption = S"SupplierID";
    myColumn->ReadOnly = false;
    // Add the column to the table's columns collection.
    myTable->Columns->Add(myColumn);
 }

[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

DataColumn Class | DataColumn Members | System.Data Namespace | Caption

Show:
© 2014 Microsoft