DataColumn.ColumnName Property

Definition

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

C#
public string ColumnName { get; set; }
C#
[System.Data.DataSysDescription("DataColumnColumnNameDescr")]
public string ColumnName { get; set; }

Property Value

The name of the column.

Attributes

Exceptions

The property is set to null or an empty string and the column belongs to a collection.

A column with the same name already exists in the collection. The name comparison is not case sensitive.

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.

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);
}

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.

Applies to

Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

See also