DataColumn.DefaultValue Propriedade

Definição

Obtém ou define o valor padrão para a coluna ao criar novas linhas.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))]
public object DefaultValue { get; set; }
public object DefaultValue { get; set; }
[System.ComponentModel.TypeConverter(typeof(System.Data.DefaultValueTypeConverter))]
[System.Data.DataSysDescription("DataColumnDefaultValueDescr")]
public object DefaultValue { get; set; }
[<System.ComponentModel.TypeConverter(typeof(System.Data.DefaultValueTypeConverter))>]
member this.DefaultValue : obj with get, set
member this.DefaultValue : obj with get, set
[<System.ComponentModel.TypeConverter(typeof(System.Data.DefaultValueTypeConverter))>]
[<System.Data.DataSysDescription("DataColumnDefaultValueDescr")>]
member this.DefaultValue : obj with get, set
Public Property DefaultValue As Object

Valor da propriedade

Object

Um valor apropriado para DataType da coluna.A value appropriate to the column's DataType.

Atributos

Exceções

Quando você estiver adicionando uma linha, o valor padrão não será uma instância do tipo de dados da coluna.When you are adding a row, the default value is not an instance of the column's data type.

Exemplos

O exemplo a seguir cria vários DataColumn objetos que têm tipos de dados diferentes e define valores padrão apropriados para cada coluna.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

Comentários

Um valor padrão é o valor atribuído automaticamente à coluna quando um DataRow é criado (por exemplo, a data e a hora em que o DataRow foi criado.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.

Quando AutoIncrement é definido como true, não pode haver nenhum valor padrão.When AutoIncrement is set to true, there can be no default value.

Você pode criar uma nova linha usando a ItemArray propriedade da DataRow classe e passando o método de uma matriz de valores.You can create a new row using the ItemArray property of the DataRow class and passing the method an array of values. Esse é um problema potencial para uma coluna com um valor padrão porque seu valor é gerado automaticamente.This is a potential problem for a column with a default value because its value is generated automatically. Para usar a ItemArray propriedade com tal coluna, coloque null na posição da coluna na matriz.To use the ItemArray property with such a column, place null in the column's position in the array. Para obter mais informações, consulte a propriedade ItemArray.For more information, see the ItemArray property.

Aplica-se a

Confira também