DataColumn Class
TOC
Collapse the table of content
Expand the table of content

DataColumn Class

 

Represents the schema of a column in a DataTable.

Namespace:   System.Data
Assembly:  System.Data (in System.Data.dll)


public class DataColumn : MarshalByValueComponent

NameDescription
System_CAPS_pubmethodDataColumn()

Initializes a new instance of a DataColumn class as type string.

System_CAPS_pubmethodDataColumn(String)

Initializes a new instance of the DataColumn class, as type string, using the specified column name.

System_CAPS_pubmethodDataColumn(String, Type)

Initializes a new instance of the DataColumn class using the specified column name and data type.

System_CAPS_pubmethodDataColumn(String, Type, String)

Initializes a new instance of the DataColumn class using the specified name, data type, and expression.

System_CAPS_pubmethodDataColumn(String, Type, String, MappingType)

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.

NameDescription
System_CAPS_pubpropertyAllowDBNull

Gets or sets a value that indicates whether null values are allowed in this column for rows that belong to the table.

System_CAPS_pubpropertyAutoIncrement

Gets or sets a value that indicates whether the column automatically increments the value of the column for new rows added to the table.

System_CAPS_pubpropertyAutoIncrementSeed

Gets or sets the starting value for a column that has its AutoIncrement property set to true. The default is 0.

System_CAPS_pubpropertyAutoIncrementStep

Gets or sets the increment used by a column with its AutoIncrement property set to true.

System_CAPS_pubpropertyCaption

Gets or sets the caption for the column.

System_CAPS_pubpropertyColumnMapping

Gets or sets the MappingType of the column.

System_CAPS_pubpropertyColumnName

Gets or sets the name of the column in the DataColumnCollection.

System_CAPS_pubpropertyContainer

Gets the container for the component.(Inherited from MarshalByValueComponent.)

System_CAPS_pubpropertyDataType

Gets or sets the type of data stored in the column.

System_CAPS_pubpropertyDateTimeMode

Gets or sets the DateTimeMode for the column.

System_CAPS_pubpropertyDefaultValue

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

System_CAPS_pubpropertyDesignMode

Gets a value indicating whether the component is currently in design mode.(Inherited from MarshalByValueComponent.)

System_CAPS_protpropertyEvents

Gets the list of event handlers that are attached to this component.(Inherited from MarshalByValueComponent.)

System_CAPS_pubpropertyExpression

Gets or sets the expression used to filter rows, calculate the values in a column, or create an aggregate column.

System_CAPS_pubpropertyExtendedProperties

Gets the collection of custom user information associated with a DataColumn.

System_CAPS_pubpropertyMaxLength

Gets or sets the maximum length of a text column.

System_CAPS_pubpropertyNamespace

Gets or sets the namespace of the DataColumn.

System_CAPS_pubpropertyOrdinal

Gets the (zero-based) position of the column in the DataColumnCollection collection.

System_CAPS_pubpropertyPrefix

Gets or sets an XML prefix that aliases the namespace of the DataTable.

System_CAPS_pubpropertyReadOnly

Gets or sets a value that indicates whether the column allows for changes as soon as a row has been added to the table.

System_CAPS_pubpropertySite

Gets or sets the site of the component.(Inherited from MarshalByValueComponent.)

System_CAPS_pubpropertyTable

Gets the DataTable to which the column belongs to.

System_CAPS_pubpropertyUnique

Gets or sets a value that indicates whether the values in each row of the column must be unique.

NameDescription
System_CAPS_protmethodCheckNotAllowNull()

This API supports the product infrastructure and is not intended to be used directly from your code. This member supports the .NET Framework infrastructure and is not intended to be used directly from your code.

System_CAPS_protmethodCheckUnique()

This API supports the product infrastructure and is not intended to be used directly from your code. This member supports the .NET Framework infrastructure and is not intended to be used directly from your code.

System_CAPS_pubmethodDispose()

Releases all resources used by the MarshalByValueComponent.(Inherited from MarshalByValueComponent.)

System_CAPS_protmethodDispose(Boolean)

Releases the unmanaged resources used by the MarshalByValueComponent and optionally releases the managed resources.(Inherited from MarshalByValueComponent.)

System_CAPS_pubmethodEquals(Object)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_protmethodFinalize()

Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from MarshalByValueComponent.)

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetService(Type)

Gets the implementer of the IServiceProvider.(Inherited from MarshalByValueComponent.)

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_protmethodMemberwiseClone()

Creates a shallow copy of the current Object.(Inherited from Object.)

System_CAPS_protmethodOnPropertyChanging(PropertyChangedEventArgs)

This member supports the .NET Framework infrastructure and is not intended to be used directly from your code.

System_CAPS_protmethodRaisePropertyChanging(String)

This member supports the .NET Framework infrastructure and is not intended to be used directly from your code.

System_CAPS_pubmethodSetOrdinal(Int32)

Changes the ordinal or position of the DataColumn to the specified ordinal or position.

System_CAPS_pubmethodToString()

Gets the Expression of the column, if one exists.(Overrides MarshalByValueComponent.ToString().)

NameDescription
System_CAPS_pubeventDisposed

Adds an event handler to listen to the Disposed event on the component.(Inherited from MarshalByValueComponent.)

The DataColumn is the fundamental building block for creating the schema of a DataTable. You build the schema by adding one or more DataColumn objects to the DataColumnCollection. For more information, see Adding Columns to a DataTable.

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. 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. For more information, see Data Type Mappings in ADO.NET.

Properties such as AllowDBNull, Unique, and ReadOnly put restrictions on the entry and updating of data, thereby helping to guarantee data integrity. You can also use the AutoIncrement, AutoIncrementSeed, and AutoIncrementStep properties to control automatic data generation. For more information about AutoIncrement columns, see Creating AutoIncrement Columns. For more information, see Defining Primary Keys.

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. For more information, see DataTable Constraints.

To create a relation between DataColumn objects, create a DataRelation object and add it to the DataRelationCollection of a DataSet.

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.

The 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); 
    } 
}

.NET Framework
Available since 1.1

This type is safe for multithreaded read operations. You must synchronize any write operations.

Return to top
Show:
© 2016 Microsoft