DataColumn DataColumn DataColumn DataColumn Class

定義

表示 DataTable 中資料行的結構描述。Represents the schema of a column in a DataTable.

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

範例

下列範例會建立DataTable具有數個DataColumn物件的。The following example creates a DataTable with several DataColumn objects.

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 DataTable = new DataTable("Product") 

    ' Create a DataColumn and set various properties. 
    Dim column As DataColumn = 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來建立架構的基本建立區塊。The DataColumn is the fundamental building block for creating the schema of a DataTable. 您可以藉由將一或多個DataColumn物件加入DataColumnCollection至來建立架構。You build the schema by adding one or more DataColumn objects to the DataColumnCollection. 如需詳細資訊, 請參閱將資料行加入至 DataTableFor more information, see Adding Columns to a DataTable.

DataColumn個都DataType有一個屬性, 可決定所DataColumn包含的資料類型。Each DataColumn has a DataType property that determines the kind of data the DataColumn contains. 例如, 您可以將資料類型限制為整數或字串或十進位數。For example, you can restrict the data type to integers, or strings, or decimals. 因為所包含的DataTable資料通常會合並回其原始資料來源, 所以您必須將資料類型與資料來源中的相同。Because data that is contained by the DataTable is typically merged back into its original data source, you must match the data types to those in the data source. 如需詳細資訊, 請參閱ADO.NET 中的資料類型對應。For more information, see Data Type Mappings in ADO.NET.

AllowDBNullUnique和等ReadOnly屬性會對資料的進入和更新進行限制, 因而有助於保證資料完整性。Properties such as AllowDBNull, Unique, and ReadOnly put restrictions on the entry and updating of data, thereby helping to guarantee data integrity. 您也可以使用AutoIncrementAutoIncrementSeedAutoIncrementStep屬性來控制自動資料產生。You can also use the AutoIncrement, AutoIncrementSeed, and AutoIncrementStep properties to control automatic data generation. 如需有關AutoIncrement資料行的詳細資訊, 請參閱建立自動遞增資料行For more information about AutoIncrement columns, see Creating AutoIncrement Columns. 如需詳細資訊, 請參閱定義主要索引鍵For more information, see Defining Primary Keys.

您也DataColumn可以建立UniqueConstraint DataTable , 並將它DataColumn加入至所屬之的, 以確定中的值是唯一的。 ConstraintCollectionYou can also make sure that values in a DataColumn are unique by creating a UniqueConstraint and adding it to the ConstraintCollection of the DataTable to which the DataColumn belongs. 如需詳細資訊, 請參閱DataTable 條件約束For more information, see DataTable Constraints.

若要建立物件之間DataColumn的關聯性, DataRelation請建立物件, DataRelationCollection並將DataSet它加入至的。To create a relation between DataColumn objects, create a DataRelation object and add it to the DataRelationCollection of a DataSet.

您可以使用DataColumn物件Expression的屬性來計算資料行中的值, 或建立匯總資料行。You can use the Expression property of the DataColumn object to calculate the values in a column, or create an aggregate column. 如需詳細資訊, 請參閱建立運算式資料行For more information, see Creating Expression Columns.

建構函式

DataColumn() DataColumn() DataColumn() DataColumn()

初始化 DataColumn 類別的新執行個體 (Instance) 為型別字串。Initializes a new instance of a DataColumn class as type string.

DataColumn(String) DataColumn(String) DataColumn(String) DataColumn(String)

使用指定的資料行名稱,初始化 DataColumn 類別的新執行個體為類型字串。Initializes a new instance of the DataColumn class, as type string, using the specified column name.

DataColumn(String, Type) DataColumn(String, Type) DataColumn(String, Type) DataColumn(String, Type)

使用指定的資料行名稱和資料類型,初始化 DataColumn 類別的新執行個體。Initializes a new instance of the DataColumn class using the specified column name and data type.

DataColumn(String, Type, String) DataColumn(String, Type, String) DataColumn(String, Type, String) DataColumn(String, Type, String)

使用指定的名稱、資料型別和運算式,初始化 DataColumn 類別的新執行個體。Initializes a new instance of the DataColumn class using the specified name, data type, and expression.

DataColumn(String, Type, String, MappingType) DataColumn(String, Type, String, MappingType) DataColumn(String, Type, String, MappingType) DataColumn(String, Type, String, MappingType)

使用指定名稱、資料型別、運算式和判斷資料行是否是屬性 (Attribute) 的值,初始化 DataColumn 類別的新執行個體。Initializes a new instance of the DataColumn class using the specified name, data type, expression, and value that determines whether the column is an attribute.

屬性

AllowDBNull AllowDBNull AllowDBNull AllowDBNull

取得或設定值,指出對於屬於資料表的資料列而言,這個資料行中是否允許 Null 值。Gets or sets a value that indicates whether null values are allowed in this column for rows that belong to the table.

AutoIncrement AutoIncrement AutoIncrement AutoIncrement

取得或設定值,指出對於加入至資料表的新資料列而言,該資料行是否自動遞增資料行的值。Gets or sets a value that indicates whether the column automatically increments the value of the column for new rows added to the table.

AutoIncrementSeed AutoIncrementSeed AutoIncrementSeed AutoIncrementSeed

取得或設定資料行的開始值,這個資料行的 AutoIncrement 屬性設定為 trueGets or sets the starting value for a column that has its AutoIncrement property set to true. 預設值為 0。The default is 0.

AutoIncrementStep AutoIncrementStep AutoIncrementStep AutoIncrementStep

取得或設定資料行所使用的遞增,這個資料行的 AutoIncrement 屬性設定為 trueGets or sets the increment used by a column with its AutoIncrement property set to true.

Caption Caption Caption Caption

取得或設定資料行的標題。Gets or sets the caption for the column.

ColumnMapping ColumnMapping ColumnMapping ColumnMapping

取得或設定資料行的 MappingTypeGets or sets the MappingType of the column.

ColumnName ColumnName ColumnName ColumnName

取得或設定在 DataColumnCollection 中的資料行名稱。Gets or sets the name of the column in the DataColumnCollection.

Container Container Container Container

取得元件的容器。Gets the container for the component.

(Inherited from MarshalByValueComponent)
DataType DataType DataType DataType

取得或設定儲存在資料行中的資料型別。Gets or sets the type of data stored in the column.

DateTimeMode DateTimeMode DateTimeMode DateTimeMode

取得或設定資料行的 DateTimeModeGets or sets the DateTimeMode for the column.

DefaultValue DefaultValue DefaultValue DefaultValue

在建立新資料列時,取得或設定資料行的預設值。Gets or sets the default value for the column when you are creating new rows.

DesignMode DesignMode DesignMode DesignMode

取得值,表示元件目前是否處於設計模式。Gets a value indicating whether the component is currently in design mode.

(Inherited from MarshalByValueComponent)
Events Events Events Events

取得附加在這個元件上的事件處理常式清單。Gets the list of event handlers that are attached to this component.

(Inherited from MarshalByValueComponent)
Expression Expression Expression Expression

取得或設定運算式,用來篩選資料列、計算資料行中的值或建立彙總資料行。Gets or sets the expression used to filter rows, calculate the values in a column, or create an aggregate column.

ExtendedProperties ExtendedProperties ExtendedProperties ExtendedProperties

取得與 DataColumn 相關聯的自訂使用者資訊集合。Gets the collection of custom user information associated with a DataColumn.

MaxLength MaxLength MaxLength MaxLength

取得或設定文字資料行的最大長度。Gets or sets the maximum length of a text column.

Namespace Namespace Namespace Namespace

取得或設定 DataColumn 的命名空間。Gets or sets the namespace of the DataColumn.

Ordinal Ordinal Ordinal Ordinal

取得 DataColumnCollection 集合中 (以零起始) 的資料行位置。Gets the (zero-based) position of the column in the DataColumnCollection collection.

Prefix Prefix Prefix Prefix

取得或設定建立 DataTable 命名空間別名的 XML 前置詞。Gets or sets an XML prefix that aliases the namespace of the DataTable.

ReadOnly ReadOnly ReadOnly ReadOnly

取得或設定值,指出是否資料列一加入至資料表,就允許變更資料行。Gets or sets a value that indicates whether the column allows for changes as soon as a row has been added to the table.

Site Site Site Site

取得或設定元件的站台。Gets or sets the site of the component.

(Inherited from MarshalByValueComponent)
Table Table Table Table

取得資料行所屬的 DataTableGets the DataTable to which the column belongs to.

Unique Unique Unique Unique

取得或設定值,指出在資料行之每個資料列中的值是否必須是唯一的。Gets or sets a value that indicates whether the values in each row of the column must be unique.

方法

CheckNotAllowNull() CheckNotAllowNull() CheckNotAllowNull() CheckNotAllowNull()

此成員支援 .NET Framework 基礎結構,並不能直接使用於您的程式碼中。This member supports the .NET Framework infrastructure and is not intended to be used directly from your code.

CheckUnique() CheckUnique() CheckUnique() CheckUnique()

此成員支援 .NET Framework 基礎結構,並不能直接使用於您的程式碼中。This member supports the .NET Framework infrastructure and is not intended to be used directly from your code.

Dispose() Dispose() Dispose() Dispose()

釋放 MarshalByValueComponent 所使用的所有資源。Releases all resources used by the MarshalByValueComponent.

(Inherited from MarshalByValueComponent)
Dispose(Boolean) Dispose(Boolean) Dispose(Boolean) Dispose(Boolean)

釋放 MarshalByValueComponent 所使用的 Unmanaged 資源,並選擇性地釋放 Managed 資源。Releases the unmanaged resources used by the MarshalByValueComponent and optionally releases the managed resources.

(Inherited from MarshalByValueComponent)
Equals(Object) Equals(Object) Equals(Object) Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(Inherited from Object)
GetService(Type) GetService(Type) GetService(Type) GetService(Type)

取得 IServiceProvider 的實作器。Gets the implementer of the IServiceProvider.

(Inherited from MarshalByValueComponent)
GetType() GetType() GetType() GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(Inherited from Object)
OnPropertyChanging(PropertyChangedEventArgs) OnPropertyChanging(PropertyChangedEventArgs) OnPropertyChanging(PropertyChangedEventArgs) OnPropertyChanging(PropertyChangedEventArgs)

此成員支援 .NET Framework 基礎結構,並不能直接使用於您的程式碼中。This member supports the .NET Framework infrastructure and is not intended to be used directly from your code.

RaisePropertyChanging(String) RaisePropertyChanging(String) RaisePropertyChanging(String) RaisePropertyChanging(String)

此成員支援 .NET Framework 基礎結構,並不能直接使用於您的程式碼中。This member supports the .NET Framework infrastructure and is not intended to be used directly from your code.

SetOrdinal(Int32) SetOrdinal(Int32) SetOrdinal(Int32) SetOrdinal(Int32)

DataColumn 的序數或位置變更為指定的序數或位置。Changes the ordinal or position of the DataColumn to the specified ordinal or position.

ToString() ToString() ToString() ToString()

取得資料行的 Expression (如果存在)。Gets the Expression of the column, if one exists.

事件

Disposed Disposed Disposed Disposed

加入事件處理常式來接聽 (Listen) 元件上的 Disposed 事件。Adds an event handler to listen to the Disposed event on the component.

(Inherited from MarshalByValueComponent)

適用於

執行緒安全性

這種類型對於多執行緒讀取作業而言是安全的。This type is safe for multithreaded read operations. 您必須同步處理任何寫入作業。You must synchronize any write operations.

另請參閱