DataColumn Class

Definition

表示 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
Inheritance

Examples

下面的示例创建具有多个 DataColumn 对象的 DataTableThe 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

Remarks

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.

属性(例如 AllowDBNullUniqueReadOnly 对数据的输入和更新施加限制,从而有助于保证数据的完整性。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.

您还可以通过创建 UniqueConstraint 并将其添加到 DataColumn 所属的 DataTable ConstraintCollection,确保 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 对象并将其添加到 DataSetDataRelationCollectionTo 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.

Constructors

DataColumn()

DataColumn 类的新实例初始化为类型字符串。Initializes a new instance of a DataColumn class as type string.

DataColumn(String)

使用指定的列名称将 DataColumn 类的新实例初始化为类型字符串。Initializes a new instance of the DataColumn class, as type string, using the specified column name.

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 类的新实例。Initializes a new instance of the DataColumn class using the specified name, data type, and expression.

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.

Properties

AllowDBNull

获取或设置一个值,该值指示对于属于该表的行,此列中是否允许空值。Gets or sets a value that indicates whether null values are allowed in this column for rows that belong to the table.

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

获取或设置其 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

获取或设置其 AutoIncrement 属性设置为 true 的列使用的增量。Gets or sets the increment used by a column with its AutoIncrement property set to true.

Caption

获取或设置列的标题。Gets or sets the caption for the column.

ColumnMapping

获取或设置列的 MappingTypeGets or sets the MappingType of the column.

ColumnName

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

Container

获取组件的容器。Gets the container for the component.

(Inherited from MarshalByValueComponent)
DataType

获取或设置存储在列中的数据的类型。Gets or sets the type of data stored in the column.

DateTimeMode

获取或设置列的 DateTimeModeGets or sets the DateTimeMode for the column.

DefaultValue

在创建新行时获取或设置列的默认值。Gets or sets the default value for the column when you are creating new rows.

DesignMode

获取指示组件当前是否处于设计模式的值。Gets a value indicating whether the component is currently in design mode.

(Inherited from MarshalByValueComponent)
Events

获取附加到该组件的事件处理程序的列表。Gets the list of event handlers that are attached to this component.

(Inherited from MarshalByValueComponent)
Expression

获取或设置表达式,用于筛选行、计算列中的值或创建聚合列。Gets or sets the expression used to filter rows, calculate the values in a column, or create an aggregate column.

ExtendedProperties

获取与 DataColumn 相关的自定义用户信息的集合。Gets the collection of custom user information associated with a DataColumn.

MaxLength

获取或设置文本列的最大长度。Gets or sets the maximum length of a text column.

Namespace

获取或设置 DataColumn 的命名空间。Gets or sets the namespace of the DataColumn.

Ordinal

获取列在 DataColumnCollection 集合中的位置(从零开始)。Gets the (zero-based) position of the column in the DataColumnCollection collection.

Prefix

获取或设置一个 XML 前缀,该前缀是 DataTable 的命名空间的别名。Gets or sets an XML prefix that aliases the namespace of the DataTable.

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

获取或设置组件的站点。Gets or sets the site of the component.

(Inherited from MarshalByValueComponent)
Table

获取列所属的 DataTableGets the DataTable to which the column belongs to.

Unique

获取或设置一个值,该值指示列的每一行中的值是否必须是唯一的。Gets or sets a value that indicates whether the values in each row of the column must be unique.

Methods

CheckNotAllowNull()

此成员支持 .NET Framework 基础结构,不能在代码中直接使用。This member supports the .NET Framework infrastructure and is not intended to be used directly from your code.

CheckUnique()

此成员支持 .NET Framework 基础结构,不能在代码中直接使用。This member supports the .NET Framework infrastructure and is not intended to be used directly from your code.

Dispose()

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

(Inherited from MarshalByValueComponent)
Dispose(Boolean)

释放 MarshalByValueComponent 使用的非托管资源,并选择性地释放托管资源。Releases the unmanaged resources used by the MarshalByValueComponent and optionally releases the managed resources.

(Inherited from MarshalByValueComponent)
Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(Inherited from Object)
GetService(Type)

获取 IServiceProvider 的实施者。Gets the implementer of the IServiceProvider.

(Inherited from MarshalByValueComponent)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
OnPropertyChanging(PropertyChangedEventArgs)

此成员支持 .NET Framework 基础结构,不能在代码中直接使用。This member supports the .NET Framework infrastructure and is not intended to be used directly from your code.

RaisePropertyChanging(String)

此成员支持 .NET Framework 基础结构,不能在代码中直接使用。This member supports the .NET Framework infrastructure and is not intended to be used directly from your code.

SetOrdinal(Int32)

DataColumn 的序号或位置更改为指定的序号或位置。Changes the ordinal or position of the DataColumn to the specified ordinal or position.

ToString()

获取列的 Expression(如果存在的话)。Gets the Expression of the column, if one exists.

Events

Disposed

添加用于侦听组件的 Disposed 事件的事件处理程序。Adds an event handler to listen to the Disposed event on the component.

(Inherited from MarshalByValueComponent)

Applies to

Thread Safety

对于多线程读取操作,此类型是安全的。This type is safe for multithreaded read operations. 必须同步任何写入操作。You must synchronize any write operations.

See also