Edit

Share via


DataColumnCollection.Item[] Property

Definition

Gets the specified DataColumn from the collection.

Overloads

Item[Int32]

Gets the DataColumn from the collection at the specified index.

Item[String]

Gets the DataColumn from the collection with the specified name.

Item[Int32]

Source:
DataColumnCollection.cs
Source:
DataColumnCollection.cs
Source:
DataColumnCollection.cs

Gets the DataColumn from the collection at the specified index.

public System.Data.DataColumn this[int index] { get; }
public virtual System.Data.DataColumn this[int index] { get; }

Parameters

index
Int32

The zero-based index of the column to return.

Property Value

The DataColumn at the specified index.

Exceptions

The index value is greater than the number of items in the collection.

Examples

The following example uses the Item[] property to print the ColumnName value of a DataColumn object specified by index. The example uses the DataTable that is contained by a System.Windows.Forms.DataGrid control.

private void PrintColumnNamesByIndex(DataTable table)
{
    // Get the DataColumnCollection from a DataTable in a DataSet.
    DataColumnCollection columns = table.Columns;

    // Print each column's name using the Index.
    for (int i = 0 ;i <columns.Count ;i++)
        Console.WriteLine(columns[i]);
}

Remarks

The Contains method can be used to test for the existence of a column. This is useful before you try to use Item[].

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Item[String]

Source:
DataColumnCollection.cs
Source:
DataColumnCollection.cs
Source:
DataColumnCollection.cs

Gets the DataColumn from the collection with the specified name.

public System.Data.DataColumn? this[string name] { get; }
public System.Data.DataColumn this[string name] { get; }
public virtual System.Data.DataColumn this[string name] { get; }

Parameters

name
String

The ColumnName of the column to return.

Property Value

The DataColumn in the collection with the specified ColumnName; otherwise a null value if the DataColumn does not exist.

Examples

The following example uses the Item[] property to print the DataType value of a DataColumn object specified by index.

private void PrintDataType(DataTable table)
{
    // Get the DataColumnCollection from a DataTable in a DataSet.
    DataColumnCollection columns = table.Columns;

    // Print the column's data type.
    Console.WriteLine(columns["id"].DataType);
}

Remarks

Item[] is conditionally case-sensitive when it searches for column names. For example, if one DataColumn is named "mydatacolumn" and another is named "Mydatacolumn", a string used to search for one of the columns is regarded as case-sensitive. However, if "mydatacolumn" exists and "Mydatacolumn" does not, the search string is regarded as case-insensitive.

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1