Represents one column of data in a DataTable.
[Visual Basic] Public Class DataColumn Inherits Component [C#] public class DataColumn : Component [C++] public __gc class DataColumn : public Component [JScript] public class DataColumn extends Component
The DataColumn is the fundamental building block for creating the schema of a DataTable. The schema defines the columns in a table, and thus determines what kind of data can be entered into the table. You might think of the schema as an empty mold which holds the shape of the data table. When you finally "pour" the data into the mold, the table attains its unique shape. The first step in creating that shape, however, is to build the schema by adding one DataColumn at a time to the ColumnsCollection.
Each DataColumn object has a DataType property that determines the kind of data the column holds. For example, you may restrict the data type to integers, or strings, or currency. Since data contained by the DataTable is usually merged back into a pre-existing database, such as SQLServer, the data type of any column must be matched to the native data base system's data type.
Properties such as AllowNull, Unique, and ReadOnly place restrictions on the entry and updating of data, thereby helping to protect the data's integrity. You can also use the AutoIncrement, AutoIncrementSeed, and AutoIncrementStep properties to control automatic data generation.
You can also ensure unique values in a column by creating a UniqueConstraint and adding it to the ConstraintsCollection for the DataTable to which the column belongs.
A feature of the DataColumn is its ability to run regular expressions using the Expression property. By using the built-in operators, computed columns can be created as needed. [NOTE to Editor: We should make sure we have a good definition for "computed column."]
Once a DataColumn object has been created, it can be added to the DataTable object's columns collection, as represented by the ColumnsCollection class.
Namespace: System.Data
Assembly: System.Data.dll
The following example creates several columns setting several different properties. The columns are added to a DataTable object's ColumnsCollection; a DataView object is then created and its Table property set to the DataTable object. Finally, the code sets a System.WinForms.DataGrid control's DataSource property to the DataView object.
[Visual Basic]
Private Sub MakeTable() Dim t As New DataTable ' Make columns using functions defined below. Dim c As DataColumn ' Create a new column. c = MakeIDCol ' Add the column to the Columns collection. t.Columns.Add(c) ' Make 3 other columns and add them to the collection. c = MakeStringCol() t.Columns.Add(c) c = MakeCurrencyCol() t.Columns.Add(c) c = MakeTaxCol() t.Columns.Add(c) c = MakeTotalCol() t.Columns.Add(c) ' Create A DataView to bind the control to. Dim dView As DataView dView = New DataView(t) dView.Open DataGrid1.DataSource = dView DataGrid1.PopulateColumns ' Add new rows to the table. Dim i As Integer Dim r As DataRow For i = 0 To 3 r = t.NewRow ' Put a random value into the currency column. r(2) = rnd(.314) * 100 ' Add the DataRow to the collection. t.Rows.Add r Next End Sub Private Function MakeIDCol() As DataColumn ' This column is set to a integer data type. ' The column also has AutoIncrement implemented. Dim dc As DataColumn dc = New DataColumn dc.DataType = System.Type.GetType("System.Int16") With dc .AllowNull = False .AutoIncrement = True .AutoIncrementSeed = 1 .AutoIncrementStep = 1 .Caption = "id" .ColumnName = "id" End With ' Return the DataColumn object to the caller. MakeIDCol = dc End Function Private Function MakeStringCol() As DataColumn ' This creates a simple string type column. Dim dc As DataColumn dc = New DataColumn dc.DataType = System.Type.GetType("System.String") With dc .AllowNull = False .AutoIncrement = False .Caption = "Item" .ColumnName = "Item" .DefaultValue = "Item" .ReadOnly = False .Unique = False End With MakeStringCol = dc End Function Private Function MakeCurrencyCol() As DataColumn ' This function creates a currency column. Dim dc As DataColumn dc = New DataColumn dc.DataType = System.Type.GetType("System.Currency") dc.AllowNull = False dc.Caption = "Price" dc.ColumnName = "Price" dc.DefaultValue = 25 MakeCurrencyCol = dc End Function Private Function MakeTaxCol() As DataColumn ' Create a column that calculates tax on the Price column. Dim dc As DataColumn dc = New DataColumn dc.DataType = System.Type.GetType("System.Currency") dc.ColumnName = "Tax" dc.caption = "Tax @ " & CStr(taxRate) dc.Expression = "Price * " & .0862 MakeTaxCol = dc End Function Private Function MakeTotalCol() As DataColumn ' Create a calculated column that adds price plus tax. Dim dc As DataColumn dc = New DataColumn dc.DataType = System.Type.GetType("System.Currency") dc.Expression = "Price + Tax" MakeTotalCol = dc End Function
DataColumn Members | System.Data Namespace | Add | ColumnsCollection | Constraints | ConstraintsCollection | DataRow | DataTable | NewRow | RowsCollection | UniqueConstraint