DataColumn.ColumnName 属性

定义

获取或设置 DataColumnCollection 中的列的名称。Gets or sets the name of the column in the DataColumnCollection.

public:
 property System::String ^ ColumnName { System::String ^ get(); void set(System::String ^ value); };
public string ColumnName { get; set; }
[System.Data.DataSysDescription("DataColumnColumnNameDescr")]
public string ColumnName { get; set; }
member this.ColumnName : string with get, set
Public Property ColumnName As String

属性值

String

列的名称。The name of the column.

属性

例外

属性设置为 null 或空字符串,且列属于一个集合。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.

示例

下面的示例获取 DataSet的每个表中的每一列的 ColumnNameThe following examples gets the ColumnName for every column in every table in a DataSet. 该示例还演示了如何使用新 ColumnName创建 DataColumnThe 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);
}
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

注解

创建 DataColumn 时,它没有 ColumnName 值。When a DataColumn is created, it has no ColumnName value. 但是,将 DataColumn 添加到 DataTable 对象的 DataColumnCollection 时,会为其提供默认名称("Column1"、"Column2" 等)。However, when the DataColumn is added to a DataColumnCollection for a DataTable object, it is given a default name ("Column1", "Column2", and so on).

默认情况下,Caption 值设置为 ColumnName 值。By default, the Caption value is set to the ColumnName value.

适用于

另请参阅