System.Data Namespace DataColumn Class
Sets or retrieves the name of the column in the DataColumnCollection.
Script |
DataColumn.ColumnName [ = strName ] |
strName |
The name of the column. |
The property is read/write with no default value.
Exception Type |
Condition |
ArgumentException |
Occurs when the property is set to a null reference or an empty string and the column belongs to a collection. |
DuplicateNameException |
Occurs when 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 DataColumnCollection, however, it is given a default name ( "Column1", "Column2", etc. ).
The Caption value is set to the ColumnName value by default.
The following example gets the ColumnName for every column in every table in a DataSet.
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 ) {
Response.Write ( myColumn.ColumnName );
}
}
}
private void AddColumn ( DataTable myTable ) {
DataColumn 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 );
}
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
Response.Write ( myColumn.ColumnName )
Next
Next
End Sub
Private Sub AddColumn ( myTable As DataTable )
Dim myColumn As 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# |
VB |
DataColumn Members Caption