DataColumn.DefaultValue 屬性

定義

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

public:
 property System::Object ^ DefaultValue { System::Object ^ get(); void set(System::Object ^ value); };
[System.ComponentModel.TypeConverter(typeof(System.Data.DefaultValueTypeConverter))]
[System.Data.DataSysDescription("DataColumnDefaultValueDescr")]
public object DefaultValue { get; set; }
member this.DefaultValue : obj with get, set
Public Property DefaultValue As Object

屬性值

適合資料行的 DataType 之值。A value appropriate to the column's DataType.

屬性

例外狀況

在加入資料列時,預設值不是資料行資料型別的執行個體。When you are adding a row, the default value is not an instance of the column's data type.

範例

下列範例會建立數DataColumn個具有不同資料類型的物件,並將適當的預設值設定為每個資料行。The following example creates several DataColumn objects that have different data types, and sets appropriate default values to each column.

Private Sub CreateColumns()
    Dim column As DataColumn
    Dim table As New DataTable
 
    column = New DataColumn
    With column
       .DataType = System.Type.GetType("System.String")
       .DefaultValue = "Address"
       .Unique = False
    End With
    table.Columns.Add(column)
    
    column = New DataColumn
    With column
       .DataType = System.Type.GetType("System.Int32")
       .DefaultValue = 100
    End With
    table.Columns.Add(column)
 
    column = New DataColumn
    With column
       .DataType = System.Type.GetType("System.DateTime")
       .DefaultValue = "1/1/2001"
    End With
    table.Columns.Add(column)
 
    Dim row As DataRow
    ' Add one row. Since it has default values, 
    ' no need to set values yet.
    row = table.NewRow
   
    table.Rows.Add(row)
 End Sub

備註

預設值是建立時DataRow ,自動指派給資料行的值(例如,建立的DataRow日期和時間)。A default value is the value that is automatically assigned to the column when a DataRow is created (for example, the date and time when the DataRow was created.

AutoIncrement設定為 true 時,就不能有預設值。When AutoIncrement is set to true, there can be no default value.

您可以使用ItemArray DataRow類別的屬性來建立新的資料列,並將值的陣列傳遞給方法。You can create a new row using the ItemArray property of the DataRow class and passing the method an array of values. 這是具有預設值之資料行的潛在問題,因為它的值會自動產生。This is a potential problem for a column with a default value because its value is generated automatically. 若要將ItemArray屬性與這類資料行搭配null使用,請將資料行位置放在陣列中。To use the ItemArray property with such a column, place null in the column's position in the array. 如需詳細資訊,請參閱 ItemArray 屬性 (Property)。For more information, see the ItemArray property.

適用於

另請參閱