DataColumn 建構函式

定義

初始化 DataColumn 類別的新執行個體。Initializes a new instance of the DataColumn class.

多載

DataColumn()

初始化 DataColumn 類別的新執行個體 (Instance) 為型別字串。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)

使用指定名稱、資料型別、運算式和判斷資料行是否是屬性 (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.

DataColumn()

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

public:
 DataColumn();
public DataColumn ();
Public Sub New ()

範例

下列範例會建立新DataColumn的、設定各種屬性,並將它新增DataColumnCollectionDataTable物件的。The following example creates a new DataColumn, sets various properties, and adds it to a DataColumnCollection for the DataTable object.

private void AddDataColumn(DataTable table)
{
    DataColumn column = new DataColumn();

    // Set various properties.
    column.ColumnName = "id";
    column.DataType = System.Type.GetType("System.Int32");
    column.AutoIncrement = true;
    column.AutoIncrementSeed = 1;
    column.AutoIncrementStep = 1;
    column.ReadOnly = true;

    // Add to Columns collection.
    table.Columns.Add(column);
}
Private Sub AddDataColumn(ByVal table As DataTable)
    Dim column As New DataColumn()

    ' Set various properties.
    With column
       .ColumnName = "id"
       .DataType = System.Type.GetType("System.Int32")
       .AutoIncrement = True
       .AutoIncrementSeed = 1
       .AutoIncrementStep = 1
       .ReadOnly = True
     End With

    ' Add to Columns collection.
    table.Columns.Add(column)
End Sub

備註

建立時, DataColumn物件沒有預設值ColumnNameCaptionWhen created, a DataColumn object has no default ColumnName or Caption. 當您將它加入至DataColumnCollection時,如果尚未將名稱指派ColumnName給,就會產生預設名稱("Column1"、"Column2" 等等)。When you add it to a DataColumnCollection, a default name ("Column1", "Column2", and so on) will be generated if a name has not been assigned to the ColumnName.

另請參閱

DataColumn(String)

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

public:
 DataColumn(System::String ^ columnName);
public DataColumn (string columnName);
new System.Data.DataColumn : string -> System.Data.DataColumn
Public Sub New (columnName As String)

參數

columnName
String

字串,表示要建立的資料行名稱。A string that represents the name of the column to be created. 如果設定為 null 或空字串 (""),則在加入資料行集合時將指定預設名稱。If set to null or an empty string (""), a default name will be specified when added to the columns collection.

範例

下列範例會使用指定DataColumn ColumnName的建立新的。The following example creates a new DataColumn with a specified ColumnName.

private void AddDataColumn(DataTable table)
{
    DataColumn column = new DataColumn("id");

    // Set various properties.
    column.DataType = System.Type.GetType("System.Int32");
    column.AutoIncrement = true;
    column.AutoIncrementSeed = 1;
    column.AutoIncrementStep = 1;
    column.ReadOnly = true;

    // Add to Columns collection.
    table.Columns.Add(column);
}

Private Sub AddDataColumn(ByVal table As DataTable)
    Dim column As DataColumn 
    column = New DataColumn("id")

    ' Set various properties.
    With column
       .DataType = System.Type.GetType("System.Int32")
       .AutoIncrement = True
       .AutoIncrementSeed = 1
       .AutoIncrementStep = 1
       .ReadOnly = True
     End With

    ' Add to Columns collection.
    table.Columns.Add(column)
End Sub

備註

根據預設,資料行特有的名稱會成為Caption屬性值。By default, the name specific to a column becomes the Caption property value.

另請參閱

DataColumn(String, Type)

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

public:
 DataColumn(System::String ^ columnName, Type ^ dataType);
public DataColumn (string columnName, Type dataType);
new System.Data.DataColumn : string * Type -> System.Data.DataColumn
Public Sub New (columnName As String, dataType As Type)

參數

columnName
String

字串,表示要建立的資料行名稱。A string that represents the name of the column to be created. 如果設定為 null 或空字串 (""),則在加入資料行集合時將指定預設名稱。If set to null or an empty string (""), a default name will be specified when added to the columns collection.

dataType
Type

支援的 DataTypeA supported DataType.

例外狀況

未指定任何的 dataTypeNo dataType was specified.

範例

下列範例會使用指定DataColumn ColumnName的和DataType來建立新的。The following example creates a new DataColumn with a specified ColumnName and DataType.

private void AddDataColumn(DataTable table)
{
    System.Type typeInt32 = 
        System.Type.GetType("System.Int32");
    DataColumn column = new DataColumn("id", typeInt32);

    // Set various properties.
    column.AutoIncrement = true;
    column.AutoIncrementSeed = 1;
    column.AutoIncrementStep = 1;
    column.ReadOnly = true;

    // Add to Columns collection.
    table.Columns.Add(column);
}

Private Sub AddDataColumn(ByVal table As DataTable)
    
   Dim typeInt32 As System.Type = _
      System.Type.GetType("System.Int32")
   Dim column As DataColumn = _
       New DataColumn("id", typeInt32)

   ' Set various properties.
   With column
      .AutoIncrement = True
      .AutoIncrementSeed = 1
      .AutoIncrementStep = 1
      .ReadOnly = True
    End With

   ' Add to Columns collection.
   table.Columns.Add(column)
End Sub

另請參閱

DataColumn(String, Type, String)

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

public:
 DataColumn(System::String ^ columnName, Type ^ dataType, System::String ^ expr);
public DataColumn (string columnName, Type dataType, string expr);
new System.Data.DataColumn : string * Type * string -> System.Data.DataColumn
Public Sub New (columnName As String, dataType As Type, expr As String)

參數

columnName
String

字串,表示要建立的資料行名稱。A string that represents the name of the column to be created. 如果設定為 null 或空字串 (""),則在加入資料行集合時將指定預設名稱。If set to null or an empty string (""), a default name will be specified when added to the columns collection.

dataType
Type

支援的 DataTypeA supported DataType.

expr
String

用來建立資料行的運算式。The expression used to create this column. 如需詳細資訊,請參閱 Expression 屬性 (Property)。For more information, see the Expression property.

例外狀況

未指定任何的 dataTypeNo dataType was specified.

範例

下列範例會建立計算資料行。The following example creates a computed column.

private void AddDataColumn(DataTable table)
{
    System.Type decimalType;
    decimalType = System.Type.GetType("System.Decimal");

    // Create the column. The name is 'Tax,' with data type Decimal,and 
    // an expression ('UnitPrice * .0862) to calculate the tax.
    DataColumn column = new DataColumn("Tax", 
        decimalType, "UnitPrice * .0862");

    // Set various properties.
    column.AutoIncrement = false;
    column.ReadOnly = true;

    // Add to Columns collection.;
    table.Columns.Add(column);
}
Private Sub AddDataColumn(ByVal table As DataTable)
    Dim column As DataColumn 
    Dim decimalType As System.Type
 
    decimalType = System.Type.GetType("System.Decimal")
    column = New DataColumn("Tax", decimalType, "UnitPrice * .0862")

    ' Set various properties.
    With column
       .AutoIncrement = False
       .ReadOnly = True
    End With

    ' Add to Columns collection.
    table.Columns.Add(column)
 End Sub

另請參閱

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.

public:
 DataColumn(System::String ^ columnName, Type ^ dataType, System::String ^ expr, System::Data::MappingType type);
public DataColumn (string columnName, Type dataType, string expr, System.Data.MappingType type);
new System.Data.DataColumn : string * Type * string * System.Data.MappingType -> System.Data.DataColumn

參數

columnName
String

字串,表示要建立的資料行名稱。A string that represents the name of the column to be created. 如果設定為 null 或空字串 (""),則在加入資料行集合時將指定預設名稱。If set to null or an empty string (""), a default name will be specified when added to the columns collection.

dataType
Type

支援的 DataTypeA supported DataType.

expr
String

用來建立資料行的運算式。The expression used to create this column. 如需詳細資訊,請參閱 Expression 屬性 (Property)。For more information, see the Expression property.

type
MappingType

其中一個 MappingType 值。One of the MappingType values.

例外狀況

未指定任何的 dataTypeNo dataType was specified.

範例

下列範例會建立一個計算資料行。The following example constructs a computed column.

private void CreateComputedColumn(DataTable table)
{
    System.Type myDataType = 
        System.Type.GetType("System.Decimal");

    // The expression multiplies the "Price" column value 
    // by the "Quantity" to create the "Total" column.
    string expression = "Price * Quantity";

    // Create the column, setting the type to Attribute.
    DataColumn column = new DataColumn("Total", myDataType, 
        expression, MappingType.Attribute);

    // Set various properties.
    column.AutoIncrement = false;
    column.ReadOnly = true;

    // Add the column to a DataTable object's to DataColumnCollection.
    DataSet1.Tables["OrderDetails"].Columns.Add(column);
}
Private Sub CreateComputedColumn(ByVal table As DataTable)
    Dim column As DataColumn
    Dim decimalType As System.Type = _
        System.Type.GetType("System.Decimal")

    ' The expression multiplies the "Price" column value by the 
    ' "Quantity" to create the "Total" column.
    Dim expression As String = "Price * Quantity"

    ' Create the column, setting the type to Attribute.
    column = New DataColumn("Total", decimalType, _
        expression, MappingType.Attribute)

    ' Set various properties.
    column.AutoIncrement = False
    column.ReadOnly = True

    ' Add the column to a DataTable object's DataColumnCollection.
    DataSet1.Tables("OrderDetails").Columns.Add(column)
End Sub

備註

引數會ColumnMapping設定屬性。 typeThe type argument sets the ColumnMapping property. 屬性會指定DataSet當轉換DataColumn成 XML 檔時,如何對應。The property specifies how a DataColumn is mapped when a DataSet is transformed into an XML document. 例如,如果資料行名為 "fName",而它所包含的值是 "Bob",而且type設定為MappingType.Attribute,則 XML 元素會如下所示:For example, if the column is named "fName," and the value it contains is "Bob," and type is set to MappingType.Attribute, the XML element would be as follows:

<Name fName = 'Bob'/><Name fName = 'Bob'/>

如需如何將資料行對應至元素或屬性的詳細資訊, ColumnMapping請參閱屬性。For more information about how columns are mapped to elements or attributes, see the ColumnMapping property.

另請參閱

適用於