Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
56 lines (42 sloc) 3.98 KB
title dev_langs ms.assetid
Adding Columns to a DataTable

Adding Columns to a DataTable

A xref:System.Data.DataTable contains a collection of xref:System.Data.DataColumn objects referenced by the Columns property of the table. This collection of columns, along with any constraints, defines the schema, or structure, of the table.

You create DataColumn objects within a table by using the DataColumn constructor, or by calling the Add method of the Columns property of the table, which is a xref:System.Data.DataColumnCollection. The Add method accepts optional ColumnName, DataType, and Expression arguments and creates a new DataColumn as a member of the collection. It also accepts an existing DataColumn object and adds it to the collection, and returns a reference to the added DataColumn if requested. Because DataTable objects are not specific to any data source, .NET Framework types are used when specifying the data type of a DataColumn.

The following example adds four columns to a DataTable.

Dim workTable As DataTable = New DataTable("Customers")  
Dim workCol As DataColumn = workTable.Columns.Add( _  
    "CustID", Type.GetType("System.Int32"))  
workCol.AllowDBNull = false  
workCol.Unique = true  
workTable.Columns.Add("CustLName", Type.GetType("System.String"))  
workTable.Columns.Add("CustFName", Type.GetType("System.String"))  
workTable.Columns.Add("Purchases", Type.GetType("System.Double"))  
DataTable workTable = new DataTable("Customers");  
DataColumn workCol = workTable.Columns.Add("CustID", typeof(Int32));  
workCol.AllowDBNull = false;  
workCol.Unique = true;  
workTable.Columns.Add("CustLName", typeof(String));  
workTable.Columns.Add("CustFName", typeof(String));  
workTable.Columns.Add("Purchases", typeof(Double));  

In the example, notice that the properties for the CustID column are set to not allow DBNull values and to constrain values to be unique. However, if you define the CustID column as the primary key column of the table, the AllowDBNull property will automatically be set to false and the Unique property will automatically be set to true. For more information, see Defining Primary Keys.

[!CAUTION] If a column name is not supplied for a column, the column is given an incremental default name of ColumnN, starting with "Column1", when it is added to the DataColumnCollection. We recommend that you avoid the naming convention of "ColumnN" when you supply a column name, because the name you supply may conflict with an existing default column name in the DataColumnCollection. If the supplied name already exists, an exception is thrown.

If you are using xref:System.Xml.Linq.XElement as the xref:System.Data.DataColumn.DataType%2A of a xref:System.Data.DataColumn in the xref:System.Data.DataTable, XML serialization will not work when you read in data. For example, if you write out a xref:System.Xml.XmlDocument by using the DataTable.WriteXml method, upon serialization to XML there is an additional parent node in the xref:System.Xml.Linq.XElement. To work around this problem, use the xref:System.Data.SqlTypes.SqlXml type instead of xref:System.Xml.Linq.XElement. ReadXml and WriteXml work correctly with xref:System.Data.SqlTypes.SqlXml.

See also

You can’t perform that action at this time.