次の方法で共有


DataColumnCollection.Item プロパティ (Int32)

指定したインデックス位置にある DataColumn をコレクションから取得します。

[C#] C# では、このプロパティは DataColumnCollection クラスのインデクサになります。

Overloads Public Overridable Default ReadOnly Property Item( _
   ByVal index As Integer _) As DataColumn
[C#]
public virtual DataColumn this[intindex] {get;}
[C++]
public: __property virtual DataColumn* get_Item(intindex);
[JScript]
returnValue = DataColumnCollectionObject.Item(index);またはreturnValue = DataColumnCollectionObject(index);

[JScript] JScript では、この型で定義されている既定のインデックス プロパティを使用することができます。しかし、独自のインデックス プロパティを明示的に定義することはできません。ただし、このクラスの expando 属性を指定すると、既定のインデックス プロパティが提供されます。提供されるインデックス プロパティの型は Object 型であり、インデックス型は String になります。

引数 [JScript]

  • index
    返される列の 0 から始まるインデックス。

パラメータ [Visual Basic, C#, C++]

  • index
    返される列の 0 から始まるインデックス。

プロパティ値

指定したインデックス位置にある DataColumn

例外

例外の種類 条件
IndexOutOfRangeException インデックス値が、コレクション内の項目数を超える値です。

解説

Item を使用する前に、 Contains メソッドを使用して、列があるかどうかを確認できます。

使用例

[Visual Basic, C#, C++] Item プロパティを使用して、インデックスで指定した DataColumn オブジェクトの ColumnName 値を出力する例を次に示します。この例では、System.Windows.Forms.DataGrid コントロールに格納されている DataTable を使用します。

 
Private Sub PrintColumnNamesByIndex(dt As DataTable)
    Dim cols As DataColumnCollection
    ' Get the DataColumnCollection from a DataTable in a DataSet.
    cols = dt.Columns
    ' Print each column's name using the Index.
    Dim i As Integer
    For i = 0 To cols.Count - 1
        Console.WriteLine(cols(i))
    Next i
End Sub

[C#] 
private void PrintColumnNamesByIndex(DataTable dt){
    DataColumnCollection cols ;
   // Get the DataColumnCollection from a DataTable in a DataSet.
   cols = dt.Columns;
   // Print each column's name using the Index.
   for (int i = 0 ;i <cols.Count ;i++)
       Console.WriteLine(cols[i]);
}

[C++] 
private:
 void PrintColumnNamesByIndex(DataTable* dt){
     DataColumnCollection* cols ;
    // Get the DataColumnCollection from a DataTable in a DataSet.
    cols = dt->Columns;
    // Print each column's name using the Index.
    for (int i = 0 ;i <cols->Count ;i++)
        Console::WriteLine(cols->Item[i]);
 }

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

参照

DataColumnCollection クラス | DataColumnCollection メンバ | System.Data 名前空間 | DataColumnCollection.Item オーバーロードの一覧 | ColumnName | Contains | DataColumn