Export (0) Print
Expand All
1 out of 5 rated this helpful - Rate this topic

DataColumn Class

Represents the schema of a column in a DataTable.

For a list of all members of this type, see DataColumn Members.

System.Object
   System.ComponentModel.MarshalByValueComponent
      System.Data.DataColumn

[Visual Basic]
Public Class DataColumn
   Inherits MarshalByValueComponent
[C#]
public class DataColumn : MarshalByValueComponent
[C++]
public __gc class DataColumn : public MarshalByValueComponent
[JScript]
public class DataColumn extends MarshalByValueComponent

Thread Safety

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

Remarks

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 Table.

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 contained by the DataTable is usually merged back into its original data source, you must match the data types to those in the data source. For more information, see Mapping .NET Framework Data Provider Data Types to .NET Framework Data Types.

Properties such as AllowDBNull, Unique, and ReadOnly place restrictions on the entry and updating of data, thereby helping to ensure 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 a Primary Key for a Table.

You can also ensure 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 Adding Constraints to a Table.

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 calculate the values in a column, or create an aggregate column. For more information, see Creating Expression Columns.

Example

[Visual Basic, C#, C++] The following example creates a DataTable with several DataColumn objects.

[Visual Basic] 
Private Sub MakeTable()
    ' Create a DataTable. 
    Dim myTable As DataTable = new DataTable("myTable") 
    ' Create a DataColumn and set various properties. 
    Dim myColumn As DataColumn = New DataColumn 
    myColumn.DataType = System.Type.GetType("System.Decimal") 
    myColumn.AllowDBNull = False 
    myColumn.Caption = "Price"  
    myColumn.ColumnName = "Price" 
    myColumn.DefaultValue = 25 
    ' Add the column to the table. 
    myTable.Columns.Add(myColumn) 
    ' Add 10 rows and set values. 
    Dim myRow As DataRow 
    Dim i As Integer  
    For i = 0 to 9 
       myRow = myTable.NewRow() 
       myRow("Price") = i + 1 
       ' Be sure to add the new row to the DataRowCollection. 
       myTable.Rows.Add(myRow) 
    Next i 
 End Sub

[C#] 
private void MakeTable(DataTable myTable){ 
   // Create a DataTable. DataTable myTable = new DataTable("myTable");
   // Create a DataColumn and set various properties. 
   DataColumn myColumn = new DataColumn(); 
   myColumn.DataType = System.Type.GetType("System.Decimal"); 
   myColumn.AllowDBNull = false; 
   myColumn.Caption = "Price"; 
   myColumn.ColumnName = "Price"; 
   myColumn.DefaultValue = 25; 
   // Add the column to the table. 
   myTable.Columns.Add(myColumn); 
   // Add 10 rows and set values. 
   DataRow myRow; 
   for(int i = 0; i < 10; i++){ 
      myRow = myTable.NewRow(); myRow["Price"] = i + 1; 
      // Be sure to add the new row to the DataRowCollection. 
      myTable.Rows.Add(myRow); 
   } 
}

[C++] 
private:
 void MakeTable(DataTable* myTable){ 
    // Create a DataTable. DataTable myTable = new DataTable("myTable");
    // Create a DataColumn and set various properties. 
    DataColumn* myColumn = new DataColumn(); 
    myColumn->DataType = System::Type::GetType(S"System.Decimal"); 
    myColumn->AllowDBNull = false; 
    myColumn->Caption = S"Price"; 
    myColumn->ColumnName = S"Price"; 
    myColumn->DefaultValue = __box(25); 
    // Add the column to the table. 
    myTable->Columns->Add(myColumn); 
    // Add 10 rows and set values. 
    DataRow* myRow; 
    for(int i = 0; i < 10; i++){ 
       myRow = myTable->NewRow(); myRow->Item[S"Price"] = __box(i + 1); 
       // Be sure to add the new row to the DataRowCollection. 
       myTable->Rows->Add(myRow); 
    } 
 }

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

Requirements

Namespace: System.Data

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

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

See Also

DataColumn Members | System.Data Namespace | Add | DataColumnCollection | Constraints | ConstraintCollection | System.Windows.Forms.DataGrid | DataRow | DataTable | DataSet | NewRow | DataRowCollection | UniqueConstraint

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.