Export (0) Print
Expand All

DataColumn.DefaultValue Property

Gets or sets the default value for the column when creating new rows.

[Visual Basic]
Public Property DefaultValue As Object
[C#]
public object DefaultValue {get; set;}
[C++]
public: __property Object* get_DefaultValue();
public: __property void set_DefaultValue(Object*);
[JScript]
public function get DefaultValue() : Object;
public function set DefaultValue(Object);

Property Value

A value appropriate to the column's DataType.

Exceptions

Exception Type Condition
InvalidCastException When adding a row, the default value is not an instance of the column's data type.

Remarks

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.

When AutoIncrement is set to true, there can be no default value.

You can create a new row using the DataRow class's ItemArray property 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. To use the ItemArray property with such a column, place a null reference (Nothing in Visual Basic) in the column's position in the array. For more details, see the ItemArray property.

Example

[Visual Basic] The following example creates several DataColumn objects with different data types, and sets appropriate default values to each column.

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

[C#, C++, JScript] No example is available for C#, C++, or JScript. To view a Visual Basic example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

DataColumn Class | DataColumn Members | System.Data Namespace | DataType | ItemArray | AutoIncrement | UniqueConstraint

Show:
© 2014 Microsoft