Gets or sets the name of the column within the ColumnsCollection.
[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);
The name of the column.
Exception Type | Condition |
---|---|
ArgumentException | The property is set to a null reference (in Visual Basic Nothing) 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. When the DataColumn is added to a DataTable object's ColumnsCollection, however, it is given a default name ("Column1", "Column2", etc.).
The Caption value is set to the ColumnName value by default.
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(ds As DataSet) Dim t As DataTable Dim dc As DataColumn ' For each table in the DataSet, print the ColumnName. For Each t in ds.Tables For Each dc in t.Columns Console.WriteLine(dc.ColumnName) Next Next End Sub Private Sub AddColumn(myTable As DataTable) Dim dc As DataColumn dc = New DataColumn("SupplierID") With dc .DataType = System.Type.GetType("System.String") .Unique = True .AutoIncrement = False .Caption = "SupplierID" .ReadOnly = False .Sparse = True End With ' Add the column to the table's columns collection. myTable.Columns.Add(dc) End Sub
DataColumn Class | DataColumn Members | System.Data Namespace | Caption