DataColumn Class
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Represents the schema of a column in a DataTable.
public ref class DataColumn : System::ComponentModel::MarshalByValueComponent
public class DataColumn : System.ComponentModel.MarshalByValueComponent
type DataColumn = class
inherit MarshalByValueComponent
Public Class DataColumn
Inherits MarshalByValueComponent
- Inheritance
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);
}
}
Private Sub MakeTable()
' Create a DataTable.
Dim table As New DataTable("Product")
' Create a DataColumn and set various properties.
Dim column As 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.
Dim row As DataRow
Dim i As Integer
For i = 0 to 9
row = table.NewRow()
row("Price") = i + 1
' Be sure to add the new row to
' the DataRowCollection.
table.Rows.Add(row)
Next i
End Sub
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.
Data |
Initializes a new instance of the DataColumn class as type string. |
Data |
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. |
Data |
Initializes a new instance of the DataColumn class using the specified name, data type, and expression. |
Data |
Initializes a new instance of the DataColumn class using the specified column name and data type. |
Data |
Initializes a new instance of the DataColumn class, as type string, using the specified column name. |
Allow |
Gets or sets a value that indicates whether null values are allowed in this column for rows that belong to the table. |
Auto |
Gets or sets a value that indicates whether the column automatically increments the value of the column for new rows added to the table. |
Auto |
Gets or sets the starting value for a column that has its AutoIncrement property set to |
Auto |
Gets or sets the increment used by a column with its AutoIncrement property set to |
Caption |
Gets or sets the caption for the column. |
Column |
Gets or sets the MappingType of the column. |
Column |
Gets or sets the name of the column in the DataColumnCollection. |
Container |
Gets the container for the component. (Inherited from MarshalByValueComponent) |
Data |
Gets or sets the type of data stored in the column. |
Date |
Gets or sets the |
Default |
Gets or sets the default value for the column when you are creating new rows. |
Design |
Gets a value indicating whether the component is currently in design mode. (Inherited from MarshalByValueComponent) |
Events |
Gets the list of event handlers that are attached to this component. (Inherited from MarshalByValueComponent) |
Expression |
Gets or sets the expression used to filter rows, calculate the values in a column, or create an aggregate column. |
Extended |
Gets the collection of custom user information associated with a DataColumn. |
Max |
Gets or sets the maximum length of a text column. |
Namespace |
Gets or sets the namespace of the DataColumn. |
Ordinal |
Gets the (zero-based) position of the column in the DataColumnCollection collection. |
Prefix |
Gets or sets an XML prefix that aliases the namespace of the DataTable. |
Read |
Gets or sets a value that indicates whether the column allows for changes as soon as a row has been added to the table. |
Site |
Gets or sets the site of the component. (Inherited from MarshalByValueComponent) |
Table |
Gets the DataTable to which the column belongs to. |
Unique |
Gets or sets a value that indicates whether the values in each row of the column must be unique. |
Check |
This member supports .NET infrastructure and is not intended to be used directly from your code. |
Check |
This member supports .NET infrastructure and is not intended to be used directly from your code. |
Dispose() |
Releases all resources used by the MarshalByValueComponent. (Inherited from MarshalByValueComponent) |
Dispose(Boolean) |
Releases the unmanaged resources used by the MarshalByValueComponent and optionally releases the managed resources. (Inherited from MarshalByValueComponent) |
Equals(Object) |
Determines whether the specified object is equal to the current object. (Inherited from Object) |
Get |
Serves as the default hash function. (Inherited from Object) |
Get |
Gets the implementer of the IServiceProvider. (Inherited from MarshalByValueComponent) |
Get |
Gets the Type of the current instance. (Inherited from Object) |
Memberwise |
Creates a shallow copy of the current Object. (Inherited from Object) |
On |
This member supports .NET infrastructure and is not intended to be used directly from your code. |
Raise |
This member supports .NET infrastructure and is not intended to be used directly from your code. |
Set |
Changes the ordinal or position of the DataColumn to the specified ordinal or position. |
To |
Gets the Expression of the column, if one exists. |
Disposed |
Adds an event handler to listen to the Disposed event on the component. (Inherited from MarshalByValueComponent) |
Product | Versions |
---|---|
.NET | Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 2.0, 2.1 |
This type is safe for multithreaded read operations. You must synchronize any write operations.
.NET feedback
.NET is an open source project. Select a link to provide feedback: