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 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

注解

是用于创建架构DataTable的基本构建基块。 DataColumnThe 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.

属性( AllowDBNull Unique例如、和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创建ConstraintCollection并将DataTable其添加到DataColumn所属的的中,确保中的值是唯一的。You 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 类的新实例初始化为类型字符串。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)

使用指定名称、数据类型、表达式和确定列是否为特性的值,初始化 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

获取或设置一个值,该值指示对于属于该表的行,此列中是否允许空值。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 属性设置为 true 的列的起始值。Gets 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 属性设置为 true 的列使用的增量。Gets 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

获取或设置一个 XML 前缀,该前缀是 DataTable 的命名空间的别名。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 占用的非托管资源,还可以另外再释放托管资源。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 的浅表副本。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

添加事件处理程序以侦听组件上的 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.

另请参阅