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); };
[System.Data.DataSysDescription("DataColumnColumnNameDescr")]
public string ColumnName { get; set; }
member this.ColumnName : string with get, set
Public Property ColumnName As 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.

範例

下列範例會取得ColumnName DataSet中每個資料表之每個資料行的。The following examples gets the ColumnName for every column in every table in a DataSet. 此範例也會示範如何DataColumn使用新ColumnName的來建立。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);
   }
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

備註

當建立時,它沒有任何ColumnName值。 DataColumnWhen a DataColumn is created, it has no ColumnName value. 不過,當新增DataColumn DataColumnCollectionDataTable物件的時,會提供預設名稱("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.

適用於

另請參閱