แก้ไข

แชร์ผ่าน


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:
 property System::Data::DataColumn ^ default[int] { System::Data::DataColumn ^ get(int index); };
public:
 virtual property System::Data::DataColumn ^ default[int] { System::Data::DataColumn ^ get(int index); };
public System.Data.DataColumn this[int index] { get; }
public virtual System.Data.DataColumn this[int index] { get; }
member this.Item(int) : System.Data.DataColumn
Default Public ReadOnly Property Item(index As Integer) As DataColumn
Default Public Overridable ReadOnly Property Item(index As Integer) As DataColumn

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]);
}
Private Sub PrintColumnNamesByIndex(table As DataTable)
    ' Get the DataColumnCollection from a DataTable in a DataSet.
    Dim columns As DataColumnCollection = table.Columns

    ' Print each column's name using the Index.
    Dim i As Integer
    For i = 0 To columns.Count - 1
        Console.WriteLine(columns(i))
    Next i
End Sub

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

Item[String]

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

Gets the DataColumn from the collection with the specified name.

public:
 property System::Data::DataColumn ^ default[System::String ^] { System::Data::DataColumn ^ get(System::String ^ name); };
public:
 virtual property System::Data::DataColumn ^ default[System::String ^] { System::Data::DataColumn ^ get(System::String ^ 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; }
member this.Item(string) : System.Data.DataColumn
Default Public ReadOnly Property Item(name As String) As DataColumn
Default Public Overridable ReadOnly Property Item(name As String) As DataColumn

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);
}
Private Sub PrintDataType(table As DataTable)
     ' Get the DataColumnCollection from a DataTable in a DataSet.
     Dim columns As DataColumnCollection = table.Columns

     ' Print the column's data type.
     Console.WriteLine(columns("id").DataType)
End Sub

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