DataColumn.ColumnName Property
Gets or sets the name of the column in the DataColumnCollection.
Namespace: System.Data
Assembly: System.Data (in System.Data.dll)
| Exception | Condition |
|---|---|
| ArgumentException | The property is set to null 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. |
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.
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.
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); }
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.