次の方法で共有


DataRow.Item プロパティ (String)

名前で指定した列に格納されているデータを取得または設定します。

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

Overloads Public Default Property Item( _
   ByVal columnName As String _) As Object
[C#]
public object this[stringcolumnName] {get; set;}
[C++]
public: __property Object* get_Item(String* columnName);public: __property void set_Item(String* columnName,   Object*);
[JScript]
returnValue = DataRowObject.Item(columnName);DataRowObject.Item(columnName) = returnValue;またはreturnValue = DataRowObject(columnName);DataRowObject(columnName) = returnValue;

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

引数 [JScript]

  • columnName
    列の名前。

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

  • columnName
    列の名前。

プロパティ値

データを格納している Object

例外

例外の種類 条件
ArgumentException columnName で指定した列が見つかりません。
DeletedRowInaccessibleException 削除した行に値を設定しようとしたときに発生します。
InvalidCastException 値とその値の Type の設定が DataType と一致していないときに発生します。

解説

プロパティを設定するときに、 ColumnChanging イベントで例外が発生すると例外が生成されます。

直前の編集の場合、生成される可能性がある例外については EndEdit のトピックを参照してください。

使用例

[Visual Basic, C#, C++] Item プロパティを使用して、指定した列インデックスの値を取得および設定する例を次に示します。最初の例では、ユーザーが DataGrid コントロールでクリックする行にある最初の列の値を取得します。2 番目の例は、引数としてメソッドに渡される値を設定します。

 
Private Sub DataGrid1_Click(sender As Object, e As System.EventArgs)
    Dim currRow As DataRow
    ' Get the DataTable the grid is bound to.
    Dim thisGrid As DataGrid = CType(sender, DataGrid)
    Dim myTable As DataTable = CType(thisGrid.DataSource, DataTable)
    currRow = myTable.Rows(thisGrid.CurrentCell.RowNumber)
    ' Get the value of the column 1 in the DataTable.
    Console.WriteLine(currRow("FirstName"))
   ' You can also use the index:
   ' Console.WriteLine(currRow(1).ToString())
End Sub
   
Private Sub SetDataRowValue(myGrid As DataGrid, newValue As Object)
    ' Set the value of the first column in the last row of a DataGrid.
    Dim myTable As DataTable
    myTable = CType(myGrid.DataSource, DataTable)
    Dim myRow As DataRow
    myRow = myTable.Rows((myTable.Rows.Count - 1))
    myRow("FirstName") = newValue
End Sub

[C#] 
private void DataGrid1_Click(object sender, System.EventArgs e){
   DataRow currRow;
   // Get the DataTable the grid is bound to.
   DataGrid thisGrid = (DataGrid) sender;
   DataTable myTable = (DataTable) thisGrid.DataSource;
   currRow = myTable.Rows[thisGrid.CurrentCell.RowNumber];
   // Get the value of the column 1 in the DataTable.
   Console.WriteLine(currRow["FirstName"]);
   // You can also use the index:
   // Console.WriteLine(currRow[1]);
}

private void SetDataRowValue(DataGrid myGrid, object newValue){
   // Set the value of the first column in the last row of a DataGrid.
   DataTable myTable;
   myTable = (DataTable) myGrid.DataSource;
   DataRow myRow; 
   myRow = myTable.Rows[myTable.Rows.Count-1];
   myRow["FirstName"] = newValue;
}

[C++] 
private:
 void DataGrid1_Click(Object* sender, System::EventArgs* /*e*/){
    DataRow* currRow;
    // Get the DataTable the grid is bound to.
    DataGrid* thisGrid = dynamic_cast<DataGrid*> (sender);
    DataTable* myTable = dynamic_cast<DataTable*> (thisGrid->DataSource);
    currRow = myTable->Rows->Item[thisGrid->CurrentCell.RowNumber];
    // Get the value of the column 1 in the DataTable.
    Console::WriteLine(currRow->Item[S"FirstName"]);
    // You can also use the index:
    // Console.WriteLine(currRow[1]);
 }
 
 void SetDataRowValue(DataGrid* myGrid, Object* newValue){
    // Set the value of the first column in the last row of a DataGrid.
    DataTable* myTable;
    myTable = dynamic_cast<DataTable*> (myGrid->DataSource);
    DataRow* myRow; 
    myRow = myTable->Rows->Item[myTable->Rows->Count-1];
    myRow->Item[S"FirstName"] = newValue;
 }

[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 ファミリ

参照

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