The schema for a table is defined by the columns in the table and the
constraints on those columns. Columns belonging to the
DataTable
are stored as
DataColumn
objects in a
DataColumnCollection
object and are accessed
through the Columns
property of the
DataTable
. This section examines some methods and
properties of the DataColumnCollection
.
There are two methods that can add a column to a table. The
Add( )
method optionally takes arguments
that specify the name, type, and expression of the column to be
added. An existing column can be added by passing a reference to an
existing column. If no arguments are passed, the default names
Column1
, Column2
,
Column3
, and so on are assigned to the new
columns. The following examples show how to create columns within the
table:
// adding a column using a reference to an existing column DataColumn col = new DataColumn("MyColumn, typeof(System.Int32)); dt.Columns.Add(col); // adding and creating a column in the same statement dt.Columns.Add("MyColumn", typeof(System.Int32));
The second method for adding columns is the AddRange( )
method, which allows more than one column stored in a
DataColumn
array to be added to the table in a
single statement, as shown in the following example:
DataTable dt = new DataTable("MyTable"); // create and add two columns to the DataColumn array DataColumn[] dca = new DataColumn[] {new DataColumn("Col1", typeof(System.Int32)), new DataColumn("Col2", typeof(System.Int32))}; // add the columns in the array to the table dt.Columns.AddRange(dca);
There are several properties and methods that interrogate the
collection of columns within a table. The
Count
property returns the number of columns
in a table:
Int32 colCount = dt.Columns.Count;
The Contains( )
method returns a value indicating
whether a column with a specified name exists in the collection. The
method takes a String
argument containing the
column name:
Boolean colExists = dt.Columns.Exists("MyColumn");
The IndexOf( )
method
returns the zero-based index of a column with a specified name within
the collection. The method returns the index if the specified column
exists or -1 if the column doesn’t exist in the
collection. The method takes a single argument containing the column
name.
Int32 colIndex = dt.Columns.IndexOf("MyColumn");
The Remove( )
, RemoveAt( )
, and
Clear( )
methods remove columns from the
DataSet
. The Remove( )
method
takes an argument that specifies either a column name or a reference
to the column to be removed, as shown in the following example:
// remove a column by specifying the name of the column dt.Columns.Remove("MyColumn") // remove a column by specifying a reference to the column DataColumn col = new DataColumn("MyColumn"); dt.Columns.Add(col); // ... do some work dt.Columns.Remove(col);
The RemoveAt( )
method
removes a column with a specified column index from the collection as
shown in the following example:
// remove the first column from the collection dt.Columns.RemoveAt(0);
The Clear( )
method
removes all columns from the column collection:
dt.Columns.Clear();