DataColumn 類別

定義

表示 DataTable 中資料行的結構描述。

public ref class DataColumn : System::ComponentModel::MarshalByValueComponent
public class DataColumn : System.ComponentModel.MarshalByValueComponent
type DataColumn = class
    inherit MarshalByValueComponent
Public Class DataColumn
Inherits MarshalByValueComponent
繼承

範例

下列範例會建立具有數DataColumnDataTable 物件的 。

private void MakeTable()
{
    // Create a DataTable.
    DataTable table = new DataTable("Product");

    // Create a DataColumn and set various properties.
    DataColumn column = new DataColumn();
    column.DataType = System.Type.GetType("System.Decimal");
    column.AllowDBNull = false;
    column.Caption = "Price";
    column.ColumnName = "Price";
    column.DefaultValue = 25;

    // Add the column to the table.
    table.Columns.Add(column);

    // Add 10 rows and set values.
    DataRow row;
    for(int i = 0; i < 10; i++)
    {
        row = table.NewRow();
        row["Price"] = i + 1;

        // Be sure to add the new row to the
        // DataRowCollection.
        table.Rows.Add(row);
    }
}
Private Sub MakeTable()
    ' Create a DataTable. 
    Dim table As New DataTable("Product")

    ' Create a DataColumn and set various properties. 
    Dim column As New DataColumn()
    column.DataType = System.Type.GetType("System.Decimal") 
    column.AllowDBNull = False 
    column.Caption = "Price"  
    column.ColumnName = "Price" 
    column.DefaultValue = 25 

    ' Add the column to the table. 
    table.Columns.Add(column) 

    ' Add 10 rows and set values. 
    Dim row As DataRow 
    Dim i As Integer  
    For i = 0 to 9 
        row = table.NewRow() 
        row("Price") = i + 1 

        ' Be sure to add the new row to 
        ' the DataRowCollection. 
        table.Rows.Add(row) 
    Next i 
End Sub

備註

DataColumn是建立架構DataTable的基本建置組塊。 您可以藉由將一或多個 DataColumn 物件新增至 DataColumnCollection來建置架構。 如需詳細資訊,請參閱 將數據行加入DataTable

每個 DataColumn 都有一個 DataType 屬性,可決定 包含的數據 DataColumn 種類。 例如,您可以將數據類型限製為整數、字串或小數點。 由於所包含的 DataTable 數據通常會合併回其原始數據源,因此您必須將數據類型與數據源中的數據類型相符。 如需詳細資訊,請參閱 ADO.NET 中的數據類型對應

UniqueReadOnlyAllowDBNull屬性會限制專案和更新數據,進而協助保證數據完整性。 您也可以使用 AutoIncrementAutoIncrementSeedAutoIncrementStep 屬性來控制自動產生資料。 如需數據行的詳細資訊 AutoIncrement ,請參閱 建立自動遞增數據行。 如需詳細資訊,請參閱定義主索引鍵

您也可以建立 ,UniqueConstraint並將其新增至 ConstraintCollection 所屬的 DataColumnDataTable ,以確保 中的DataColumn值是唯一的。 如需詳細資訊,請參閱 DataTable 條件約束

若要建立物件之間的 DataColumn 關聯性,請建立 DataRelation 物件,並將它新增至 DataRelationCollectionDataSet

您可以使用 Expression 物件的 屬性 DataColumn 來計算數據列中的值,或建立匯總數據行。 如需詳細資訊,請參閱 建立表達式數據行

建構函式

DataColumn()

將類別的新實例 DataColumn 初始化為型別字串。

DataColumn(String)

使用指定的資料行名稱,初始化 DataColumn 類別的新執行個體為類型字串。

DataColumn(String, Type)

使用指定的資料行名稱和資料類型,初始化 DataColumn 類別的新執行個體。

DataColumn(String, Type, String)

使用指定的名稱、資料型別和運算式,初始化 DataColumn 類別的新執行個體。

DataColumn(String, Type, String, MappingType)

使用指定名稱、資料型別、運算式和判斷資料行是否是屬性 (Attribute) 的值,初始化 DataColumn 類別的新執行個體。

屬性

AllowDBNull

取得或設定值,指出對於屬於資料表的資料列而言,這個資料行中是否允許 Null 值。

AutoIncrement

取得或設定值,指出對於加入至資料表的新資料列而言,該資料行是否自動遞增資料行的值。

AutoIncrementSeed

取得或設定資料行的開始值,這個資料行的 AutoIncrement 屬性設定為 true。 預設值是 0。

AutoIncrementStep

取得或設定資料行所使用的遞增,這個資料行的 AutoIncrement 屬性設定為 true

Caption

取得或設定資料行的標題。

ColumnMapping

取得或設定資料行的 MappingType

ColumnName

取得或設定在 DataColumnCollection 中的資料行名稱。

Container

取得元件的容器。

(繼承來源 MarshalByValueComponent)
DataType

取得或設定儲存在資料行中的資料型別。

DateTimeMode

取得或設定資料行的 DateTimeMode

DefaultValue

在建立新資料列時,取得或設定資料行的預設值。

DesignMode

取得值,表示元件目前是否處於設計模式。

(繼承來源 MarshalByValueComponent)
Events

取得附加在這個元件上的事件處理常式清單。

(繼承來源 MarshalByValueComponent)
Expression

取得或設定運算式,用來篩選資料列、計算資料行中的值或建立彙總資料行。

ExtendedProperties

取得與 DataColumn 相關聯的自訂使用者資訊集合。

MaxLength

取得或設定文字資料行的最大長度。

Namespace

取得或設定 DataColumn 的命名空間。

Ordinal

取得 DataColumnCollection 集合中 (以零起始) 的資料行位置。

Prefix

取得或設定建立 DataTable 命名空間別名的 XML 前置詞。

ReadOnly

取得或設定值,指出是否資料列一加入至資料表,就允許變更資料行。

Site

取得或設定元件的網站。

(繼承來源 MarshalByValueComponent)
Table

取得資料行所屬的 DataTable

Unique

取得或設定值,指出在資料行之每個資料列中的值是否必須是唯一的。

方法

CheckNotAllowNull()

這個成員支援 .NET 基礎結構,而且不適合直接從程式碼使用。

CheckUnique()

這個成員支援 .NET 基礎結構,而且不適合直接從程式碼使用。

Dispose()

釋放 MarshalByValueComponent 所使用的所有資源。

(繼承來源 MarshalByValueComponent)
Dispose(Boolean)

釋放 MarshalByValueComponent 所使用的 Unmanaged 資源,並選擇性地釋放 Managed 資源。

(繼承來源 MarshalByValueComponent)
Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetService(Type)

取得 IServiceProvider 的實作器。

(繼承來源 MarshalByValueComponent)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
OnPropertyChanging(PropertyChangedEventArgs)

這個成員支援 .NET 基礎結構,而且不適合直接從程式碼使用。

RaisePropertyChanging(String)

這個成員支援 .NET 基礎結構,而且不適合直接從程式碼使用。

SetOrdinal(Int32)

DataColumn 的序數或位置變更為指定的序數或位置。

ToString()

取得資料行的 Expression (如果存在)。

事件

Disposed

加入事件處理常式來接聽元件上的 Disposed 事件。

(繼承來源 MarshalByValueComponent)

適用於

執行緒安全性

此類型適用於多線程讀取作業。 您必須同步處理任何寫入作業。

另請參閱