.NET Framework Class Library
DataColumn..::.ColumnName Property

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

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

Visual Basic (Declaration)
Public Property ColumnName As String
Visual Basic (Usage)
Dim instance As DataColumn
Dim value As String

value = instance.ColumnName

instance.ColumnName = value
C#
public string ColumnName { get; set; }
Visual C++
public:
property String^ ColumnName {
    String^ get ();
    void set (String^ value);
}
JScript
public function get ColumnName () : String
public function set ColumnName (value : String)

Property Value

Type: System..::.String
The name of the column.
Exceptions

ExceptionCondition
ArgumentException

The property is set to nullNothingnullptra 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. However, when the DataColumn is added to a DataColumnCollection for a DataTable object, it is given a default name ("Column1", "Column2", and so on).

By default, the Caption value is set to the ColumnName value.

Examples

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(dataSet As DataSet)
    Dim table As DataTable
    Dim column As DataColumn 

    ' For each DataTable, print the ColumnName.
    For Each table in dataSet.Tables
        For Each column in table.Columns
        Console.WriteLine(column.ColumnName)
        Next
    Next
End Sub

Private Sub AddColumn(table As DataTable)
    Dim column As DataColumn
    column = New DataColumn()

    With column
        .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.
    table.Columns.Add(column)
End Sub
C#
private void PrintColumnNames(DataSet dataSet)
{
    // For each DataTable, print the ColumnName.
    foreach(DataTable table in dataSet.Tables)
    {
        foreach(DataColumn column in table.Columns)
        {
            Console.WriteLine(column.ColumnName);
        }
    }
}

private void AddColumn(DataTable table)
{
    DataColumn column;
    column = new DataColumn();
    column.ColumnName = "SupplierID";
    column.DataType = System.Type.GetType("System.String");
    column.Unique = true;
    column.AutoIncrement = false;
    column.Caption = "SupplierID";
    column.ReadOnly = false;

    // Add the column to the table's columns collection.
    table.Columns.Add(column);
}
Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
See Also

Reference

Other Resources

Tags :


Page view tracker