2 out of 2 rated this helpful - Rate this topic

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

The DataColumn type exposes the following members.

  Name Description
Public method Supported by the XNA Framework DataColumn() Initializes a new instance of a DataColumn class as type string.
Public method Supported by the XNA Framework DataColumn(String) Inititalizes a new instance of the DataColumn class, as type string, using the specified column name.
Public method Supported by the XNA Framework DataColumn(String, Type) Inititalizes a new instance of the DataColumn class using the specified column name and data type.
Public method Supported by the XNA Framework DataColumn(String, Type, String) Initializes a new instance of the DataColumn class using the specified name, data type, and expression.
Public method Supported by the XNA Framework DataColumn(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.
Top
  Name Description
Public property Supported by the XNA Framework AllowDBNull Gets or sets a value that indicates whether null values are allowed in this column for rows that belong to the table.
Public property Supported by the XNA Framework AutoIncrement Gets or sets a value that indicates whether the column automatically increments the value of the column for new rows added to the table.
Public property Supported by the XNA Framework AutoIncrementSeed Gets or sets the starting value for a column that has its AutoIncrement property set to true.
Public property Supported by the XNA Framework AutoIncrementStep Gets or sets the increment used by a column with its AutoIncrement property set to true.
Public property Supported by the XNA Framework Caption Gets or sets the caption for the column.
Public property Supported by the XNA Framework ColumnMapping Gets or sets the MappingType of the column.
Public property Supported by the XNA Framework ColumnName Gets or sets the name of the column in the DataColumnCollection.
Public property Supported by the XNA Framework Container Gets the container for the component. (Inherited from MarshalByValueComponent.)
Public property Supported by the XNA Framework DataType Gets or sets the type of data stored in the column.
Public property Supported by the XNA Framework DateTimeMode Gets or sets the DateTimeMode for the column.
Public property Supported by the XNA Framework DefaultValue Gets or sets the default value for the column when you are creating new rows.
Public property Supported by the XNA Framework DesignMode Gets a value indicating whether the component is currently in design mode. (Inherited from MarshalByValueComponent.)
Protected property Supported by the XNA Framework Events Gets the list of event handlers that are attached to this component. (Inherited from MarshalByValueComponent.)
Public property Supported by the XNA Framework Expression Gets or sets the expression used to filter rows, calculate the values in a column, or create an aggregate column.
Public property Supported by the XNA Framework ExtendedProperties Gets the collection of custom user information associated with a DataColumn.
Public property Supported by the XNA Framework MaxLength Gets or sets the maximum length of a text column.
Public property Supported by the XNA Framework Namespace Gets or sets the namespace of the DataColumn.
Public property Supported by the XNA Framework Ordinal Gets the position of the column in the DataColumnCollection collection.
Public property Supported by the XNA Framework Prefix Gets or sets an XML prefix that aliases the namespace of the DataTable.
Public property Supported by the XNA Framework ReadOnly Gets or sets a value that indicates whether the column allows for changes as soon as a row has been added to the table.
Public property Supported by the XNA Framework Site Gets or sets the site of the component. (Inherited from MarshalByValueComponent.)
Public property Supported by the XNA Framework Table Gets the DataTable to which the column belongs to.
Public property Supported by the XNA Framework Unique Gets or sets a value that indicates whether the values in each row of the column must be unique.
Top
  Name Description
Protected method Supported by the XNA Framework CheckNotAllowNull Infrastructure. This member supports the .NET Framework infrastructure and is not intended to be used directly from your code.
Protected method Supported by the XNA Framework CheckUnique Infrastructure. This member supports the .NET Framework infrastructure and is not intended to be used directly from your code.
Public method Supported by the XNA Framework Dispose() Releases all resources used by the MarshalByValueComponent. (Inherited from MarshalByValueComponent.)
Protected method Supported by the XNA Framework Dispose(Boolean) Releases the unmanaged resources used by the MarshalByValueComponent and optionally releases the managed resources. (Inherited from MarshalByValueComponent.)
Public method Supported by the XNA Framework Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Supported by the XNA Framework Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from MarshalByValueComponent.)
Public method Supported by the XNA Framework GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method Supported by the XNA Framework GetService Gets the implementer of the IServiceProvider. (Inherited from MarshalByValueComponent.)
Public method Supported by the XNA Framework GetType Gets the Type of the current instance. (Inherited from Object.)
Protected method Supported by the XNA Framework MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Protected method Supported by the XNA Framework OnPropertyChanging This member supports the .NET Framework infrastructure and is not intended to be used directly from your code.
Protected method Supported by the XNA Framework RaisePropertyChanging This member supports the .NET Framework infrastructure and is not intended to be used directly from your code.
Public method Supported by the XNA Framework SetOrdinal Changes the ordinal or position of the DataColumn to the specified ordinal or position.
Public method Supported by the XNA Framework ToString Gets the Expression of the column, if one exists. (Overrides MarshalByValueComponent.ToString().)
Top
  Name Description
Public event Supported by the XNA Framework Disposed Adds an event handler to listen to the Disposed event on the component. (Inherited from MarshalByValueComponent.)
Top

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 (ADO.NET).

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 (ADO.NET). For more information, see Defining Primary Keys (ADO.NET).

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 (ADO.NET).

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 (ADO.NET).

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

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

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

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ