O'Reilly logo

ADO.NET in a Nutshell by Bill Hamilton, Matthew MacDonald

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Name

Columns

Synopsis


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.

Examples

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required