DataRow.Item[] プロパティ

定義

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

オーバーロード

名前 説明
Item[DataColumn]

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

Item[Int32]

インデックスで指定された列に格納されているデータを取得または設定します。

Item[String]

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

Item[DataColumn, DataRowVersion]

指定した DataColumnに格納されているデータの指定したバージョンを取得します。

Item[Int32, DataRowVersion]

取得するデータのインデックスとバージョンによって指定された、列に格納されているデータを取得します。

Item[String, DataRowVersion]

名前付き列に格納されているデータの指定されたバージョンを取得します。

Item[DataColumn]

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

public:
 property System::Object ^ default[System::Data::DataColumn ^] { System::Object ^ get(System::Data::DataColumn ^ column); void set(System::Data::DataColumn ^ column, System::Object ^ value); };
public object this[System.Data.DataColumn column] { get; set; }
member this.Item(System.Data.DataColumn) : obj with get, set
Default Public Property Item(column As DataColumn) As Object

パラメーター

column
DataColumn

データを含む DataColumn

プロパティ値

データを含む Object

例外

列はこのテーブルに属していません。

columnは null です。

削除された行に値を設定しようとしました。

値と列のデータ型が一致しません。

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

Private Sub DataGrid1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs)
    
    Dim dataGridTable As DataTable = _
        CType(DataGrid1.DataSource, DataTable)
    ' Set the current row using the RowNumber 
    ' property of the CurrentCell.
    Dim currentRow As DataRow = _
        dataGridTable.Rows(DataGrid1.CurrentCell.RowNumber)
    Dim column As DataColumn = dataGridTable.Columns(1)

    ' Get the value of the column 1 in the DataTable.
    label1.Text = currentRow(column).ToString()
End Sub
 
Private Sub SetDataRowValue( _
    ByVal grid As DataGrid, ByVal newVal As Object)

    ' Set the value of a column in the last row of a DataGrid.
    Dim table As DataTable = CType(grid.DataSource, DataTable)
    Dim row As DataRow = table.Rows(table.Rows.Count - 1)
    Dim column As DataColumn = table.Columns("FirstName")
    row(column)= newVal
End Sub

注釈

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

これが即時編集の場合は、生成できる例外の EndEdit を参照してください。

適用対象

Item[Int32]

インデックスで指定された列に格納されているデータを取得または設定します。

public:
 property System::Object ^ default[int] { System::Object ^ get(int columnIndex); void set(int columnIndex, System::Object ^ value); };
public object this[int columnIndex] { get; set; }
member this.Item(int) : obj with get, set
Default Public Property Item(columnIndex As Integer) As Object

パラメーター

columnIndex
Int32

列の 0 から始まるインデックス。

プロパティ値

データを含む Object

例外

削除された行に値を設定しようとしたときに発生します。

columnIndex引数が範囲外です。

値を設定し、新しい値の TypeDataTypeと一致しない場合に発生します。

次の例では、 Item[] プロパティを使用して、特定の列インデックスの値を取得および設定します。 最初の例では、ユーザーが DataGrid コントロール内でクリックした行の最初の列の値を取得します。

private void DataGrid1_Click(object sender,
    System.EventArgs e)
{
    // Get the DataTable the grid is bound to.
    DataGrid thisGrid = (DataGrid) sender;
    DataTable table = (DataTable) thisGrid.DataSource;
    DataRow currentRow =
        table.Rows[thisGrid.CurrentCell.RowNumber];

    // Get the value of the column 1 in the DataTable.
    Console.WriteLine(currentRow[1]);
    // You can also use the name of the column:
    // Console.WriteLine(currentRow["FirstName"])
}

private void SetDataRowValue(DataGrid grid, object newValue)
{
    // Set the value of the last column in the last row of a DataGrid.
    DataTable table;
    table = (DataTable) grid.DataSource;
    DataRow row;

    // Get last row
    row = (DataRow)table.Rows[table.Rows.Count-1];

    // Set value of last column
    row[table.Columns.Count-1] = newValue;
}
Private Sub DataGrid1_Click _
    (ByVal sender As System.Object, ByVal e As System.EventArgs)

    ' Get the DataTable the grid is bound to.
    Dim thisGrid As DataGrid = CType(sender, DataGrid)
    Dim table As DataTable = CType(thisGrid.DataSource, DataTable)
    Dim currentRow As DataRow = _
        table.Rows(thisGrid.CurrentCell.RowNumber)

    ' Get the value of the column 1 in the DataTable.
    Console.WriteLine(currentRow(1))
    ' You can also use the name of the column:
    ' Console.WriteLine(currentRow("FirstName"))
    End Sub

    Private Sub SetDataRowValue( _
        ByVal grid As DataGrid, ByVal newValue As Object)

    ' Set the value of the last column in the last row of a DataGrid.
    Dim table As DataTable
    table = CType(grid.DataSource, DataTable)
    Dim row As DataRow 
    row = table.Rows(table.Rows.Count-1)
    row(table.Columns.Count-1) = newValue
End Sub

注釈

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

これが編集である場合は、生成できる例外の EndEdit を参照してください。

適用対象

Item[String]

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

public:
 property System::Object ^ default[System::String ^] { System::Object ^ get(System::String ^ columnName); void set(System::String ^ columnName, System::Object ^ value); };
public object this[string columnName] { get; set; }
member this.Item(string) : obj with get, set
Default Public Property Item(columnName As String) As Object

パラメーター

columnName
String

列の名前。

プロパティ値

データを含む Object

例外

columnNameで指定された列が見つかりません。

削除された行に値を設定しようとしたときに発生します。

値を設定し、その TypeDataTypeと一致しない場合に発生します。

AllowDBNullfalse に設定されている列に null 値を挿入しようとすると発生します。

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

private void DataGrid1_Click(
    object sender, System.EventArgs e)
{
    // Get the DataTable the grid is bound to.
    DataGrid thisGrid = (DataGrid) sender;
    DataTable table = (DataTable) thisGrid.DataSource;
    DataRow currentRow =
        table.Rows[thisGrid.CurrentCell.RowNumber];

    // Get the value of the column 1 in the DataTable.
    Console.WriteLine(currentRow["FirstName"]);
    // You can also use the index:
    // Console.WriteLine(currentRow[1]);
}

private void SetDataRowValue(
    DataGrid grid, object newValue)
{
    // Set the value of the first column in
    // the last row of a DataGrid.
    DataTable table = (DataTable) grid.DataSource;
    DataRow row = table.Rows[table.Rows.Count-1];
    row["FirstName"] = newValue;
}
Private Sub DataGrid1_Click( _
    sender As Object, e As System.EventArgs)
     
    ' Get the DataTable the grid is bound to.
    Dim thisGrid As DataGrid = CType(sender, DataGrid)
    Dim table As DataTable = _
        CType(thisGrid.DataSource, DataTable)
    Dim currentRow As DataRow = _
        table.Rows(thisGrid.CurrentCell.RowNumber)

    ' Get the value of the column 1 in the DataTable.
    Console.WriteLine(currentRow("FirstName"))
    ' You can also use the index:
    ' Console.WriteLine(currentRow(1).ToString())
End Sub
    
Private Sub SetDataRowValue( _
    grid As DataGrid, newValue As Object)
    ' Set the value of the first column in 
    ' the last row of a DataGrid.
    Dim table As DataTable = _
        CType(grid.DataSource, DataTable)
    Dim row As DataRow
    row = table.Rows((table.Rows.Count - 1))
    row("FirstName") = newValue
End Sub

注釈

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

これが即時編集の場合は、生成できる例外の EndEdit を参照してください。

適用対象

Item[DataColumn, DataRowVersion]

指定した DataColumnに格納されているデータの指定したバージョンを取得します。

public:
 property System::Object ^ default[System::Data::DataColumn ^, System::Data::DataRowVersion] { System::Object ^ get(System::Data::DataColumn ^ column, System::Data::DataRowVersion version); };
public object this[System.Data.DataColumn column, System.Data.DataRowVersion version] { get; }
member this.Item(System.Data.DataColumn * System.Data.DataRowVersion) : obj
Default Public ReadOnly Property Item(column As DataColumn, version As DataRowVersion) As Object

パラメーター

column
DataColumn

列に関する情報を含む DataColumn

version
DataRowVersion

目的の行のバージョンを指定する DataRowVersion 値の 1 つ。 指定できる値は、DefaultOriginalCurrent、および Proposed です。

プロパティ値

データを含む Object

例外

列はテーブルに属していません。

column引数に null が含まれています。

行には、このバージョンのデータがありません。

次の例では、 DataGrid コントロール内のクリックされたセルの現在の値を取得します。

private void DataGrid1_Click(object sender,
    System.EventArgs e)
{
    DataTable dataGridTable =
        (DataTable)DataGrid1.DataSource;

    // Set the current row using the RowNumber
    // property of the CurrentCell.
    DataRow currentRow = dataGridTable.Rows[DataGrid1.CurrentCell.RowNumber];
    DataColumn column = dataGridTable.Columns[1];

    // Get the value of the column 1 in the DataTable.
    Console.WriteLine(currentRow[column, DataRowVersion.Current]);
}
Private Sub DataGrid1_Click _
    (ByVal sender As System.Object, ByVal e As System.EventArgs)

    Dim dataGridTable As DataTable = _
        CType(DataGrid1.DataSource, DataTable)

    ' Set the current row using the RowNumber 
    ' property of the CurrentCell.
    Dim currentRow As DataRow = dataGridTable.Rows( _
        DataGrid1.CurrentRowIndex)
    Dim column As DataColumn = dataGridTable.Columns(1)

    ' Get the value of the column 1 in the DataTable.
    label1.Text = currentRow(column, _
        DataRowVersion.Current).ToString()
End Sub

注釈

versionは、RowState プロパティと混同しないでください。 version引数は、列の元の値に対する列に含まれるデータの状態を表します。

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

これが即時編集の場合は、生成できる例外の EndEdit を参照してください。

こちらもご覧ください

適用対象

Item[Int32, DataRowVersion]

取得するデータのインデックスとバージョンによって指定された、列に格納されているデータを取得します。

public:
 property System::Object ^ default[int, System::Data::DataRowVersion] { System::Object ^ get(int columnIndex, System::Data::DataRowVersion version); };
public object this[int columnIndex, System.Data.DataRowVersion version] { get; }
member this.Item(int * System.Data.DataRowVersion) : obj
Default Public ReadOnly Property Item(columnIndex As Integer, version As DataRowVersion) As Object

パラメーター

columnIndex
Int32

列の 0 から始まるインデックス。

version
DataRowVersion

目的の行のバージョンを指定する DataRowVersion 値の 1 つ。 指定できる値は、DefaultOriginalCurrent、および Proposed です。

プロパティ値

データを含む Object

例外

columnIndex引数が範囲外です。

値と列のデータ型が一致しません。

行には、このバージョンのデータがありません。

削除された行に値を設定しようとしました。

次の例では、DataRow オブジェクトの Item[] プロパティを使用して、列の現在の値を取得します。

Private Sub DataGrid1_Click _
    (ByVal sender As System.Object, ByVal e As System.EventArgs)
    
    ' Set the current row using the RowNumber property of the CurrentCell.
    Dim currentRow As DataRow = CType(DataGrid1.DataSource, DataTable). _
       Rows(DataGrid1.CurrentCell.RowNumber)

    ' Get the value of the column 1 in the DataTable.
    label1.Text = currentRow(1, DataRowVersion.Current).ToString()
End Sub

注釈

BeginEdit メソッドを呼び出した後にのみ行を作成または更新できます。同様に、EndEdit メソッドを呼び出して編集をコミットする必要があります。 EndEdit メソッドを呼び出し、AcceptChanges メソッドを呼び出す前に、元の値と新しい提案値の内部表現が格納されます。 したがって、 AcceptChangesを呼び出すまでは、 version 引数を使用して、必要な列の値のバージョン ( DataRowVersion.Original または DataRowVersion.Proposed) を指定できます。 ただし、 AcceptChanges メソッドを呼び出すとすぐに、列のバージョンは DataRowVersion.Originalに戻ります。 行が新しい場合は、パラメーターの DataRowVersion.Default を渡して列の既定値を取得することもできます。 DataRowVersion.Current渡すと、プロパティは現在の値を返します。そのバージョンが何であっても返されます。

Note

BeginEdit メソッドは、データ バインド コントロールの値を変更するとき、またはDataRow オブジェクトがDataRowCollectionに追加されたときに暗黙的に呼び出されます。EndEdit メソッドは、DataRow オブジェクトのAcceptChanges メソッド、DataTable オブジェクトのAcceptChanges メソッド、またはCancelEdit メソッドの呼び出し時に暗黙的に呼び出されます。

これに対し、 DataRowVersion 列挙 Current は、 EndEdit メソッドが呼び出された後のデータのバージョンを返します。

version引数は、RowState プロパティと混同しないでください。 version引数は、列の元の値に対する列に含まれるデータの状態を表します。 RowState プロパティは、親DataTableに対する行全体の状態を表します。

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

これが即時編集の場合は、生成できる例外の EndEdit を参照してください。

適用対象

Item[String, DataRowVersion]

名前付き列に格納されているデータの指定されたバージョンを取得します。

public:
 property System::Object ^ default[System::String ^, System::Data::DataRowVersion] { System::Object ^ get(System::String ^ columnName, System::Data::DataRowVersion version); };
public object this[string columnName, System.Data.DataRowVersion version] { get; }
member this.Item(string * System.Data.DataRowVersion) : obj
Default Public ReadOnly Property Item(columnName As String, version As DataRowVersion) As Object

パラメーター

columnName
String

列の名前。

version
DataRowVersion

目的の行のバージョンを指定する DataRowVersion 値の 1 つ。 指定できる値は、DefaultOriginalCurrent、および Proposed です。

プロパティ値

データを含む Object

例外

columnNameで指定された列が見つかりません。

値と列のデータ型が一致しません。

行には、このバージョンのデータがありません。

行が削除されました。

次の例では、 DataGrid コントロールのクリックされたセルにあるデータの現在のバージョンを取得します。

private void DataGrid1_Click(object sender, System.EventArgs e)
{
    // Set the current row using the RowNumber
    // property of the CurrentCell.
    DataRow currentRow =
        ((DataTable)(DataGrid1.DataSource)).
        Rows[DataGrid1.CurrentCell.RowNumber];

    // Print the current value of the column named "FirstName."
    Console.WriteLine(currentRow["FirstName",
        DataRowVersion.Current]);
}
Private Sub DataGrid1_Click _
    (ByVal sender As System.Object, ByVal e As System.EventArgs)
    
    ' Set the current row using the RowNumber property 
    ' of the CurrentCell.
    Dim currentRow As DataRow = _
        CType(DataGrid1.DataSource, DataTable). _
        Rows(DataGrid1.CurrentCell.RowNumber)

    ' Print the current value of the column named "FirstName."
    Console.WriteLine(currentRow("FirstName", _
        DataRowVersion.Current).ToString())
End Sub

注釈

バージョンは、 RowState プロパティと混同しないでください。 version引数は、列の元の値に対する列に含まれるデータの状態を表します。 RowState プロパティは、親DataTableに対する行全体の状態を表します。

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

これが即時編集の場合は、生成できる例外の EndEdit を参照してください。

こちらもご覧ください

適用対象