Columns
DataColumnCollection dcc = DataTable.Columns;
Accesses the DataColumnCollection
for the
DataTable
, providing access to the
DataColumn
objects belonging to the
DataTable
. The Columns
property
can be used to add, remove, and examine the
DataColumn
objects in a
DataTable
.
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
, and so on,
are assigned to the new column. The following examples show how to
create a column using the Add( )
method:
// 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 is AddRange( )
, 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 can interrogate the
collection of columns within a table. The Count( )
method 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 the column name as an argument.
bool colExists = dt.Columns.Contains("MyColumn");
The IndexOf( )
method returns the index of a
column having a specified name within the collection. The method
returns the zero-based index value for the column if it exists in the
collection or the value of -1 if the column doesn’t
exist. The method takes a single argument containing the column name.
Int32 colIndex = dt.Columns.IndexOf("MyColumn");
Finally, there are three methods that remove columns from the
collection. The Remove( )
method removes a column
with a specified name from the collection. The method takes a single
argument containing either the column name or a reference to a
DataColumn
object. If the column
doesn’t exist in the collection, an
ArgumentException
is raised.
// 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. The method takes a single
argument containing the zero-based index of the column to be removed.
If the column doesn’t exist in the collection, an
IndexOutOfRangeException
is raised.
// remove the first column from the collection dt.Columns.RemoveAt(0);
Finally, the Clear( )
method removes all columns
from the column collection:
dt.Columns.Clear();
The DataColumnCollection
raises a single event,
CollectionChanged
, when a column is either added
or removed from the collection. The
CollectionChangeEventArgs
properties provide
information about the nature of the change as described in Table 23-5.
Property |
Description |
Action |
Describes how the collection changed. This is a value from the
|
Element |
Gets the column that was changed. |
Table 23-6 describes the values of the
CollectionChangeAction
enumeration, one of which
is assigned to the Action
property of the
CollectionChangeEventArgs
argument passed to the
event handling the change to the
DataColumnCollection
.
The following example demonstrates handling the
ColumnChange
event:
dt.Columns.CollectionChanged += new CollectionChangeEventHandler (dtColumns_CollectionChanged); private void dtColumns_CollectionChanged(object sender, CollectionChangeEventArgs e) { MessageBox.Show("Action = " + e.Action + Environment.NewLine + "Element = " + ((DataColumn) e.Element).ColumnName); }
The DataColumnCollection
class derives its
standard functionality from the
InternalDataCollectionBase
class from which it
inherits.
The commonly used public properties of the
DataColumnCollection
are listed and described in
Table 23-7.
The commonly used public methods of the
DataColumnCollection
are listed and described in
Table 23-8.